Site TIPS PAGE🔎 SEARCH  Ξ INDEX  MAIN MENU  UP ONE LEVEL
 OJB's Web Site. Version 2.4. Mac Tip.Location: tip98 tips mac owen2 
Tip

Discuss   Up to OJB's Mac Tips List

Terminal: Listing Directory

These commands can be used to list the files in a folder (or in multiple folders) in various ways. They give a lot more flexibility than using the standard tools such as the Finder.

The "ls" command is used to list the contents of a directory (a directory is Unix terminology for a folder). There are many options for this command. For example, to list the directory contents with one file per line use "ls -1" (options are preceeded with a dash, please use lower case and watch for the difference between the lower case L and the number one). To list the files in folders (and folder in folder to any number of levels) in the current directory use "ls -1R".

One of the coolest things about the Unix command line is how commands can work together using a process called "piping". The output from one command can be piped to the input of another. For example to sort the directory we could use "ls -1R | sort". Where the "|" (vertical bar) is a pipe. If you do this you will see upper case stuff gets sorted before lower case stuff. Unix is case sensitive - this is very important to remember! To fix the upper/lower problem use the -f option on the sort command. Our command is now "ls -1R | sort -f".

Another problem is the blank lines (part of the directory list output) that appear at the start of the list. To remove these use GREP. Try this: "ls -1R | sort -f | grep "^[0-9A-Za-z]"" (note that the whole command is enclosed in quotes, which you should not type, but there are quotes round the string to be grepped which you should type!) The GREP looks for a line beginning with a letter or number and ignores any lines which don't start with these.

Finally, lets save the output of the commands above into a text file. Here's the final command which will do this: "ls -1R | sort -f | grep "^[0-9A-Za-z]" > ~/Files.txt". This uses another powerful feature called "re-direction".

The ">" symbol takes what would normally be output and sends it to a file instead. The "~" is shorthand for the user's home directory.

Related Tips

Terminal Example: Listing Files
Terminal Example: Shell Script
Terminal Tips: Deleting Files
Terminal: Change Priority
Terminal: Changing Privileges
Terminal: Fixing a Hang
Terminal: Manual and Help
Terminal: Remote Secure Login
Terminal: Terminate a Process
Terminal: Top Processes





I usually write a blog post about once a week. The latest post can be viewed here: Think for Yourself: In the end, everything is just an opinion. Be skeptical, and think for yourself! (posted 2026-04-25 at 21:12:02). Or go to the Home Page of My Blog to choose a post to view.

I do podcasts too! You can listen to my latest podcast, here: OJB's Podcast 2026-04-14 How Far is that Star?: How would we really know how big the universe is? Or subscribe to my podcast RSS feed, on my RSS Feeds page.

If you're not sure what to view from the thousands of pages on my site, here are some suggestions: My Latest Airshow Report (photos and movies from Warbirds Over Wanaka, 2026), My Favourite Wines and Beers (tasting notes for some of my favourites), An Interesting Astronomical Observation (learn a bit about astronomy from these observing notes), See Some Photos (A short rail journey through the Taieri Gorge), Read Some Mac Tips (Learn about how to use your Mac, and fix some problems).


Latest Site News and Notifications (Desktop): You are currently viewing OJB's web site, version 2.4 which has some major changes, and possibly errors! Please report any problems to ojb@mac.com.

 Site ©2026 by OJBOJB's BlogMicrosoft Free ZoneMade & Served on Mac 
Site Features: Blog RSS Feeds Podcasts Feedback Log07 Jun 2024. Hits: 1,001,510,242
Description: Mac TipLogged: nothingLoad Timer: 200ms