[Index] [Menu] [Up] Title[Header]
Terminal Tips

(Up to OJB's Mac Terminal Tips List Page)


Listing Directory Contents

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 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]". 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.

More information: Getting Started, Pipes.


[Up]

[Contact][Server Blog][AntiMS Apple][Served on Mac]

Comment on this page: Very UsefulQuite UsefulUseless or: View Results