Site TIPS PAGE🔎 SEARCH  Ξ INDEX  MAIN MENU  UP ONE LEVEL
 OJB's Web Site. Version 2.1. Mac Tip.You are here: tip tips mac owen2 
Tip

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.



I usually write a blog post about once a week. The latest post can be viewed here: Unity Through Division: Sometimes hard decisions need to be made to make genuine progress. (posted 2024-11-18 at 19:13:00). I do podcasts too!. You can listen to my latest podcast, here: OJB's Podcast 2024-08-22 Stirring Up Trouble: Let's just get every view out there and fairly debate them..
 Site ©2024 by OJBRSS FeedMicrosoft Free ZoneMade & Served on Mac 
Site Features: Blog RSS Feeds Podcasts Feedback Log07 Jun 2024. Hits: 166,946,898
Description: Mac TipKeywords: Macintosh,Computers,Fixing Problems,TipsLoad Timer: 11ms