Site TIPS PAGE🔎   UP ONE LEVEL
 OJB's Web Site. V 2.1.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.



My latest blog post: Manufactured Outrage: We might be beginning to win some battles, but the war is far from over. (posted 2024-12-17). My latest podcast: OJB's Podcast 2024-12-04 Avoid Microsoft.
 ©2024 by OJBRSS FeedMS Free ZoneMac Made
T: 11. H: 217,350,288
Features: RSS Feeds Feedback LogMod: 07 Jun 2024