MacMost Now 70: Command Line Basics

Gary Rosenzweig of shows you the basics of using the Terminal application. Learn how to navigate, list files, rename, copy and delete. Also learn some shortcuts that experts use.
Video Transcript / Captions
Closed captioning for this video is available on YouTube: MacMost Now 70: Command Line Basics.

Hi, this is Gary Rosenzweig. On the MacMost podcast I often show you how to do things using command line interface in the terminal window. In this episode, let's take a step back and look at some of the terminal window basics.
To get to the terminal program, simple go into applications, go into the utilities folder in there and look for terminal. When you run it, you get a window just like this. You just type things in it to make things happen. So we'll type our first command, 'ls,' which is a listing of what's in the directory.
We're going to start at the user directory, which has the typical things that you see when you look in the finder: desktop, downloads, music, that type of thing. When you're using command line interface, you're always at a location on your hard drive. So right now we're in the user folder.
If we want to change locations, we do 'cd' for change directory. So we'll do 'cd' and we'll go into the documents folder. Now when we do a listing we see files in the documents folder. Now notice that I typed a capital D for documents. That's because in most command line interfaces you need to match capitalization perfectly. You actually don't have to do that with the 'cd' command here in terminal window, but I like to do it anyway.
The 'ls' command can be modified. For instance, you can do 'ls -l' and get a long listing of what is in the directory. Instead of just listing the names of the files, you get a file in each line and you get things like its size and modification date, that type of thing.
You can also do 'ls -a' to list everything in the directory, even invisible files. So these are things that Mac OS usually hides from you in the finder because they're not your files. They're things that tell the Mac OS about that directory. And you can see everything there. You'll also always see the '.' and '..' which are the indicators of current directory and directory above this one.
So for instance, if we wanted to go up a level, we would do 'cd ..' and now we are back up at the user level. If you ever want to see exactly where you are, type 'pwd' and it will give you the full listing. So we go into documents folder again, 'pwd.'
Now manipulating files in the command line interface is pretty simple and just uses three different commands. So we'll look at what we have now. And we've got this test.txt file that I created in text settings. Now suppose I wanted to rename that. You actually don't rename, you move. You would move test.txt to newtest.txt.
And now when we list we see that it has actually changed file names. We could actually change file names to keep it in the same directory our we could actually be moving it into another directory at the same time but giving it a full file path. And the file path would look something like what we get when we do 'pwd,' which is a /Users/MacMost/documents. We can say move that to another folder there by just adding more, changing this.
So if you want to actually copy a file, you would do the 'cp' command and you would give it two file names. The first one is newtest.txt, the original file, and the next one is newnewtest.txt, the new file name.
And we list and there we go. We've got both newtest.txt and newnewtest.txt. To remove, we just type 'rm' and the name of the file. And there we go.
Now another cool thing we can do is we can use wild cards on command. So for instance, we do 'ls' we see these files. What if we only wanted files that began with the letter N? We could do 'ls n*' and we get a list of everything that begins with the letter N. The star represents anything. So you can see n* fits in with newtest.txt. You could do all sorts of different things like, for instance, removing files that began with the letter N.
Now if you ever want to find out more about a command, there is a way to get help inside the terminal - it's the manual, which you get by typing 'man,' space, and the name of the command, like 'ls,' brings up this little document viewer. You can page through it with the space bar or go line by line with return. 'q' to quit. And you can find out about all different commands like 'ls' or 'cp' or 'mv'.
And when you see experts using the command line interface, you'll see that they go pretty fast. One of the ways they do that is by using a couple shortcuts. Like for instance, say I did 'ls' and then said, 'no, I want to do 'ls -l' and then I want to actually use a command I've used before. I can use the up arrow. I'm going to press the up arrow right now and it gives me the last command. I'm going to press it again and it will give me the command before that. And I can do down arrow to go between those to go back to a blank line that I was working on.
So if I wanted to repeat a command, I just do up arrow and return. And if there was a command, say, ten line ago, I can do up arrow all the way through these different commands that we've been working with and down arrow back through them.
Another thing you can do is automatic completion. So say I wanted to actually go ahead and remove newtest. So I type 'rm ne'. Now I'm typing 'ne' - now maybe newtest.txt is not that long of a file name, but suppose it's a very long file name. Well, I could hit the tab key and what the command line interface is going to do is it's going to try to figure out what I was going to type. In this case, the only file there beginning with 'ne' was newtest.txt, so it immediately went and figured out that that's probably what I want and completed the line for me.
So that's a really quick look at the basic things you can do with the terminal window. If you know how to do this, then you can go ahead and use some more advance commands that help you do some very useful things. We'll be touching on those in future podcasts.
Until next time, this is Gary Rosenzweig with MacMost Now.