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 Example: Listing Files

Terminal Example: Listing Files

This is an example of know how to do basic stuff in the terminal. Sometimes we want a list of what items are in a folder for reference later or for printing. We will list our documents folder into a file for later use.

First, run Terminal and open a new window. Choose New from the Shell menu in the Terminal program (a shell is an environment that accepts and executes your commands, you can have many shells running at once). Remember to switch back to the terminal before typing a command, to do every instruction exactly as listed below, type everything exactly (including upper/lower case), but without the enclosing quotes, and press return after each command...

Before you start, use the Finder to check that you don't have a file called List.txt in your Documents folder. If you do, please rename it or it will be deleted! Your working directory will be Home. First we will change to our Documents folder, so type "cd Documents". Now we will list our files. Type "ls". You should see a list of the files in the Documents folder. Now we will send the information which appeared on the screen to a file instead. Type "ls -1 > List.txt".

Now return to the Finder and look in the Documents folder. There will be a file called "List.txt". Double click on it to open it, confirm it contains the names of the files, then close it again.

The "ls -1" part of the command creates a list one column wide of the working directory. The ">" part takes the output which would normally appear on the screen and sends it to a file instead, in this case "List.txt".

Maybe we wanted to list the contents of the folders in the Documents folder as well. Type this: "ls -1R > List.txt". This might take a while. On my machine it took 3 seconds to generate a file 22,000 lines long showing every file in my Documents folder! Note that the old List.txt was over-written without warning. Unix can be dangerous. The "R" option means do a recursive list which here has the effect of showing all the files in every folder, including folders inside folders to any number of levels.

Finally, let's be more specific. This time we will only look for files with "mac" in their name. Type "ls -1R | grep mac > List.txt". Now type "open List.txt". You should see the file displayed by the TextEdit program Oor whatever other program you might have set as the default for opening text files). Notice that the open command opens a file using a Mac (graphical) program.

The original command now uses two powerful Unix concepts: re-direction and piping. First the "ls" command generates a list of items in the folders, the "|" pipes the output to the "grep" command which looks for the text "mac" and re-directs its output to the file we specified. When we use a pipe, the output from one program is fed to another program concurrently. You can use many pipes in one command - this is a very powerful function!

We're finished with our List.txt now so let's delete it. Close the file if its open in TextEdit and type "rm -P List.txt". The "rm" means remove, the "-P" is an option that overwrites the file so it can never be recovered. If you have a confidential document you want really deleted you could delete it this way - but be careful, there's no second chance!



I usually write a blog post about once a week. The latest post can be viewed here: Garbage: Democracy dies in darkness, especially when it is buried in garbage! (posted 2024-11-01 at 12:31:14). 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 OJBOJB's BlogWhy Macs are BestMade & Served on Mac 
Site Features: Blog RSS Feeds Podcasts Feedback Log07 Jun 2024. Hits: 149,528,538
Description: Mac TipKeywords: Macintosh,Computers,Fixing Problems,TipsLoad Timer: 11ms