Terminal for Post Production - Moving Around ( ls & cd )
We've made it to our first Bash commands! Arguably two of the most used commands in Terminal, ls and cd are used for listing the contents of a given directory and moving through directories respectively.
First, we'll take a look at ls. Without using any options, ls will list the contents of the current directory. By default, your working directory will start at the root of your user folder on a Mac. You'll likely see something like the following printed to the console:
user$ ls Applications Desktop Documents Downloads Library Movies Music Pictures Public
Without adding an option, ls will not list hidden files. To view hidden files as well, use the ls -a option. Hidden files in Bash are denoted with a period (.) before the file name. ls has many additional options to explore, all available in the man page. Here are just a few helpful options:
-l : List the contents of your working directory in long format - including ownership, permissions, modification date, and more
-S : Sort by size
-t : Sort by date modified
This command is used to change the current working directory. In the example above, we're looking at the contents of the current user folder. To change the working directory to the folder "Desktop," we'd use the command:
user$ cd Desktop/
The trailing slash on Desktop/ denotes a directory rather than a file. One extremely helpful feature of Bash is auto-completion. If we were to type "De" and then hit [TAB] on the keyboard, the rest of the folder name will automatically complete (Desktop/). If there were another folder that started with the letters "De," we would need to complete more of the name to use the auto-completion feature.
Moving around with cd and ls
Using these two commands together, we can move through directories just like we would in Finder. If we wanted to locate "example.jpg" in a folder on the Desktop named "Screenshots," we would enter the following commands:
user$ cd Desktop/ user$ ls STILLS FOR WEBSITE Screenshots user$ cd Screenshots/ user$ ls example.jpg
Using this pattern of cd and ls, we can look at the contents of a folder, move one directory deeper, then look at the contents of the next directory down.
To move up a level in the directory tree, we use the command:
user$ cd ..
To move from the "Screenshots" folder back to the User directory, we could enter the following:
user$ cd .. user$ cd .. user$ ls Applications Desktop Documents Downloads Library Movies Music Pictures Public
In addition to using cd one directory at a time, you can jump directly to a specific folder by passing a full directory path into the command:
user$ cd /Users/user/Desktop/Screenshots/
While this process may seem cumbersome at first, it becomes second nature after a little practice. In the next post, we'll be looking at the echo command.