If you need a file listing to use in a document you can get it by copying and pasting from the Finder if you know a trick. But you can also get file listings as text from the Terminal and then wrap that command into a Shortcut.
You can also watch this video at YouTube.
Watch more videos about related subjects: Finder (314 videos), Shortcuts (69 videos), Terminal (43 videos).
You can also watch this video at YouTube.
Watch more videos about related subjects: Finder (314 videos), Shortcuts (69 videos), Terminal (43 videos).
Video Transcript
Hi, this is Gary with MacMost.com. Let me show you how you can get a file listing as text on your Mac.
MacMost is brought to you thanks to a great group of more than 2000 supporters. Go to MacMost.com/Patreon. There you could read more about it. Join us and get exclusive content and course discounts.
Now I'm often asked how you can get a file listing as text to put into a document. Maybe you need to do a report or wrap up a project and have a listing of all the files in folders and subfolders for that project. There are a lot of reasons you may want to do this but how to do it seems pretty elusive in the Finder. There's no obvious function for doing it. But, you can do it using some simple Copy and Paste tricks and you can also do it in the Terminal.
So to do it without using the Terminal at all, just some simple Copy and Paste, you can do by selecting the files that you want, copying them and pasting them into a document. But you have to know a trick. So let's start first an example here. Let's say I want to get the complete file listing for everything in the Projects folder here. So I open up this Projects folder and there is a lot of files in here and some subfolders as well. Now if I were to select some files like this one here and Shift Click to select this one you can see I've got that whole range and I can go to Edit and Copy eleven items. This will allow me to actually Paste the files, make a copy of them somewhere else. But if I open a Text Editor, like TextEdit, then I can paste it and I get something different.
So here I'm in TextEdit and I have a plain text document. If I go to Format it says Make Rich Text. That means I'm using Plain Text. You're going to want to use plain text for this because if you try to paste these files in a rich text it's going to do things like actually try to import the image files and such. But plain text will force it to just to have the file names. So I'm going to do Command V for Edit and Paste. Now you can see I get a listing of the files there. So this is a start. But it is not going to get us all the way. Cause here's what would happen if we were to do that and have some subfolders as well. So here we have a subfolder with more files in it and you can see there's this one here. So I can select this file and I'll Shift Click to select to here. You can see I've got this one inside of More Files. If I do Command C to Copy and then I Paste you can see I get the folder name there but I get the file in that folder and it doesn't indicate that it is actually in that folder and that then this one is not. So you can see it's not an accurate presentation of what is there.
But there is a better way to handle this. If you go to Edit and then you see where it says Copy 6 Items. If I hold the Option Key down that will change to Copy 6 Items as Path Names. So Option Command C will do that. Then I go to the Text file and I Paste, Command V. Now you can see I get the full Path Names. So I can see that this file here is in the Projects folder. But I can see that this file is in Projects More files and then there is the file name. So I get a complete picture of exactly where each thing is.
Now let's say I want to get everything into this Projects folder here. I'll dive down into it to make it easy. I can do Command A to grab All. But that doesn't grab the files inside the folders here. I have to open it up and you can see these aren't selected. So one way to open all these up is with Command A you have everything. Now I can do Command and then Right Arrow and you can see how that opens up all of the folders there. Now you've got everything opened up and another Command A selects everything. So I've got every file and every folder here. Now if I use Option Command C to copy its Path Names, I go to my Text document and I use Command V to paste in, I'll get everything. Every file and every folder. So this may be all that you need.
But if you want to take it a few steps further you can. Notice, for instance, that all of these start off with the same path. They're part of the Projects folder. So I really don't need that. I could actually select that, Copy, do Command F to Find, and find all of those. Check the Replace Box here and replace it with nothing. Click All and now I've actually removed that Path there. I still have any deeper paths. So, for instance, here's that Project Alpha folder. Here's Project Alpha Subfiles. So this gives me a little more compact view.
Now another thing you can do with Replace All here is look for every slash. So every slash is simply a new folder. Now I can replace that with a Tab to get a better view of what's going on. So with the Text cursor blinking in the Replace field I click this little magnifying glass here, this Search icon, and I select Insert Pattern. I choose Tab. So now it is going to replace every slash with a Tab. If I click All now you can see I get this nice listing here where I can much more clearly see the folders and subfolders. Not only that I can use the Option Key and click and drag. This selects a rectangular area. So I can select all of the User MacMost's Documents Projects here like that. If I press Delete now I get rid of that and I just get the folders and subfolders under Projects.
Now if you want an even better view one thing you can do is Save this file, Command S, and I'm going to Save it to the Desktop. Call it just Files.txt. You want the txt extension. Now I'm going to run Numbers. In Numbers I'm going to go to File, Open and I'm going to Open that txt file. It's going to recognize that there are tabs in there and it's going to create a spreadsheet based on that. So you can see here I've got all the files and here are the folders with files in them and down one level and everything. So you get an even clearer look. You could also go back, I'm going to Undo twice here, instead of tab I'm going to do something like dash dash and then a greater than symbol like that. Maybe a space before and a space after, like that. When I Replace All you can see I get something that looks like this which maybe easier to read. So there are a lot of different things that you can do to get this formatted like you want.
Now what about doing this in the Terminal. Let's launch the Terminal App. The first thing I want to do is go to that folder. So I can use CD for change directory and drag the folder into the Terminal window and it will paste in the path. Then Return and now I'm at that location. So now I can list the files in there. I can use LS, for instance, to list the files. It's not a great list but maybe this works for you. You can also do LS-L and it will give a file listing like this with some information about the files. But there are better ways to do it.
One way that I know people are going to mention is to use Tree. Tree is not something that comes installed on your Mac. You have to install it and I know most Mac users won't do that. So that's fine. I'm going to show you ways to do it without using tree. You can use Find. So I can do find and then an asterisk and then the dash print. What this will do is it is going to find all the files in the current directly. You can see that works. It gives us a pretty good listing here. You can see here's a folder and there's the contents of that folder with each file in it. It gives us a really complete look at what's in there. So this maybe all that you need. But we can get a little better on this because it would be nice, for instance, list this and then to indent these underneath the folder there.
We're going to add something to this command. So it is still going to use Find-print and then it is going to pipe that to this command here which is going to basically look for slash and then any characters and then another slash. In other words a folder name at the beginning there and it's going to replace it with a Tab. It does a little more than that but it is a basic substitute there. When we run this it's going to give us the same lines but we're not going to get the tabs there. We're going to get tabs instead. So, you can see here I get the folder name and then I get each file in the folder tabbed underneath it. Now a problem with this is that sometimes there are files that actually have contents inside them. So, for instance, here's Launch Calculator.workflow and that's just a file. But it's a package. So it looks like a folder to Terminal and it has a folder in it called Contents and a bunch of stuff in there and this isn't what we want. We just want this file listed not all of the stuff inside the package. It gets even worse with other things like here's this Apply Quartz Composition Filter.app that also contains a bunch of stuff inside a package and you have all this stuff in here that you don't want to see. So it would be nice to have a way to just have this as one file.
Here's a further alteration of the code. We still have this part here that puts the tabs for the path. But we also are going to use a grep here that's going to look for anytime that there is a period, a literal period, in the path. So it's not going to worry about the file extension. It's just going to worry about the path. So any period that is before the last slash. It is simply going to filter out those lines. So this will work in most cases. If you've got some folders that actually have some periods in the name then it will mess this up and you're going to have to get a lot more complex. But this is what we get when we do it this time and we can see that we don't get the listing for the Apply Quartz Composition Filter.app. It is just shown there once. The same thing here in that workflow file. It is just there that one time. So it is a little bit normal of a listing.
One issue I see here is that things are sorted alphabetically but the capital letters come first and then the lowercase letters. You can see them all here at the end. So let's make another alteration. All I've added here is another pipe into sort dash f which is going to sort everything and it's going to place the lowercase and uppercase together. So you can see here everything is properly sorted. You can see lowercase letters appearing in with the uppercase letters just fine.
I want to show one more thing here. You're going to get a lot of these dotDS Store files and they're invisible files. So it's going to be useful to maybe just get rid of those. So here's a quick way to do that. Back here, after we eliminate the lines that have a period before the last slash, we can put a or, which is the same as a pipe symbol, we'll use that and then we'll put whatever we want in there. I'm going to do capital DS underscore capital Store. So also any lines that have DS underscore Store in them are going to also be eliminated. So when I run this you now won't find that dotDS Store file. I use this as an example because you can go back in here and add anything else you want. So you can do another pipe there and then just type anything and it will not include files that have that in the name. So it's a good way to eliminate some things that you may not want in your file listing.
Now once you have this you can always copy and paste the result. So I can go back in here, grab the end, drag all the way to the top, and just start right here and do a Copy and Paste. But, you can build that into the command by using a pipe and pbcopy, pasteboard copy. Now when I run this it gives me no results. But if I go to TextEdit and then Command V to paste it pastes it in because the bcopy here at the end sent the results to the clipboard.
One final bit here because I know I'm going to get asked this. Can you make this a shortcut? Sure, you can. Let's create a new shortcut here. I want to call this Get File Listing. I want to search for Select File and what I actually want this for is not for selecting a file but if I click here I can select a folder instead. Under Show More I just want it to be one. Now I want to get the details of the file. So I'll put that next. The detail I want is the File Path. If I were to run this now it will prompt me, I can go here in my Documents Folder and here's that Projects Folder. Open it up and I can see the result. There is the path. So now I can do a Shell Script and I use the exact same shell script we've been using all along. I'll drag that in here and I want to change directories, CD, and in quotes I want to use the results of this. So I just Control Click, two-finger click, or right click here. Insert Variable and use the file path right there. So this will change directory to this directory here. Now I just need to Paste in that same Find Command we were using in the Terminal. So I'll Paste that in and now when I run this it will prompt me for folder, open, and I get the listing here just like I did before. I can Copy and Paste from here. I could simply use Copy to Clipboard here as the last thing.
So now when I run this. I can select the folder, Open, it will finish. It will give me the output but it will copy it to the Clipboard. Now I'll switch to TextEdit here and Paste in and I've got my File Listing. So that's a little handy way to do this. I can add it as Pin In Menu Bar so now I've got access to it right here. You may be asking why I didn't have it take a folder as input so you can actually use a Quick Action in the Finder and get the path from that Quick Action. That works sometimes but it definitely does not work other times. So I went with this which was more consistent to be able to get the path.
So there you go. A bunch of different techniques that you can use to get File Listings of text on your Mac. Hope you found this useful. Thanks for watching.
Thanks bunches
Wahhhhh... where were you last week??? Do you know what I had to go thru to copy the files? I think I took a screenshot, opened it in Preview so it shows text, and copied the text. Luckily it was only ONE folder, but wow.. I didn't even know that was possible!! THANK YOU!
Your timing is amazing. I have been trying to do this for the past week. Ta Da and there you are. Thanks!
I use BBEdit and drag the folder from the Finder into the BBEdit document and it does the indenting fro subfolders etc. automatically.
Thanks Gary, I love it when you get us involved in using the Terminal and Shortcuts, and this was a great application. I had one problem: Using Sonoma 14.4.1 and the zsh shell: I had to put double quotes around the SED command: sed -e "s;[^/]*/;\t;g"
Single quotes would not work.
Dear Mr. Gary;
Enough of this please try app Print Window 5.app @ App Store ; i have been using this for years and it has multiple formats for files listings on you mac.
Thank me now or later.
Note: you have been around for a number of years and appreciate your MacMost.com website
As Al LaPierre pointed out, running Sonoma 14.4.1, the single quotes do not work; but unlike him, even the double quotation marks do not work. I get the following error message:
sed: option requires an argument -- e
usage: sed script [-Ealnru] -i extension]
[file ...]
sed [-Eainu] [-i extension] [-e script] ...
[-f script_file] ... [file ...]
Akiva: Are you sure you are using zsh, not bash or something else?
Is there a reason why the input can't be right clicking a location in Finder? This seems to me to be the most logical input.
David: Go ahead and try that. I did and I got inconsistent results so I opted for this method.
I am wondering why Apple cannot implement this function that I have been asking for many years. For the money they charge, this and many other features and functions should be available and easy to use....
Good info Gary. Here's links to Homebrew and the 'Tree' formula you can easily install via Terminal - I think they're likely pertinent, from what Gary mentioned in this video (does similar things, just showing differently - another video idea, lol!):
https://brew.sh
https://formulae.brew.sh/formula/tree
Thanks for your amazing work as always!!! You are a "life" savior!!! Help me out a lot!!! Everything im learning on MacOs its with you Gary!!!
Good and useful information. I cannot seem to get the copied files as a listing in the text application. It keeps wrapping the copy. Is there a text edit setting I should have in place? I've tried a number of adjustments. Running latest version of Sequoia. All advice appreciated, thanks!
Greg: Not sure what you mean. Is it that you are not getting the file listing, or that the paths are too long for the lines so they wrap to the next line (as text would normally do)?
Yes, it was the path copied version. It is more like there is no carriage return on the pasted files. I've widened the text window but it doesn't seem to help.
Greg: Are you pasting in TextEdit? Have you tried it in plain text mode, as well as in rich text mode? How about if you paste into Pages or Notes?
Gary,
I am using textedit and checked it for plain text and the character sizing (180). It still seems to wrap all the text without a line break as it shows in the video. I checked in Pages and Notes with no luck. I've also made sure that I'm using the view option (list) to have it almost exactly as you show in the video. Is it possibly some setting on Finder or TextEdit that I'm not catching? Everything works it just is a pain for the "unwrap" cleanup. Thanks for the persistence!
Greg: Perhaps I'm not understanding your issue. Try asking in macmost.com/ask with a screenshot later.