8 Comments

Summary:

Last week I posted the Tab trick when in the Terminal/command line interface. Quickly, it expands the text output that you’re typing, of both directories and commands. Tapping Tab twice will bring up commands or directories starting with the same letter you’ve already typed – or […]

terminal Last week I posted the Tab trick when in the Terminal/command line interface. Quickly, it expands the text output that you’re typing, of both directories and commands. Tapping Tab twice will bring up commands or directories starting with the same letter you’ve already typed – or even show the available items you can append to your entered command. (As an example, type: “cd” and then hit Tab Tab. It shows you a list of current directories you can move into.)

So the Tab command is a handy one. But what if you don’t really know any commands? How do you get started? I’ll give you a couple useful commands today, as well as a way to find some more of them once you’re comfortable.

The most common activities – at least for beginners – is moving around the file system, seeing what’s there, and executing simple commands such as copy/move/delete, so those are the things I’ll cover. It’s always helpful to know where you are, so let’s start there.

pwd – This is the current directory you’re viewing or working within. I think of it as standing for ‘Present Working Directory’, although it more accurately signifies ‘Path of Working Directory’. Typing ‘pwd‘ will return something like this:
/Users/nick

cd – This stands for ‘Change Directory’. The syntax looks like this:
cd Music/iTunes
The example above assumes you’re already in your home directory or path (/Users/nick). Notice that there’s no leading / in this command. The slash tells the Terminal that you want to start at the root level of the file system.
So that moves us forward, or deeper into a directory structure, but how do we dig our way back out?
cd ..
The ‘..‘ tells the cd command to move you back (up) one directory level. If you’re following along with the commands so far, you’re in the iTunes directory (as I am) which from the root has a path of /Users/nick/Music/iTunes If you’d like to return to your home directory (/Users/nick) then it’s two hops backward and would look like this:
cd ../../
A pwd command should result in:
/Users/nick

Alright. Now you know how to find out where you are, and how to move up or down the directory structure. Let’s see what is actually in these directories now.

ls – This command does a List of the files and folders in the current directory. On its own, it’s simple and does nothing more than show you what’s there. You can add arguments to it (as with just about any command in Unix) to make it a more complex argument which will return addition useful information. But I’ll touch on that a bit later.

So now you see the files and folders, how about doing something with them?

cp – This is the command to copy something. The syntax to copy a file and give it a new name looks like this:
cp file.txt copyoffile.txt
So you’re telling the Terminal, ‘copy file.txt and name the duplicate copyoffile.txt’. There’s just a space between each argument. But let’s say you want to copy a file from your home directory to your Documents directory. Assuming you are already in your home directory, that command would look like so:
cp file.txt Documents/file.txt
With these arguments you should be able to play around a bit anf discover more.

mv – This command will Move a file. The syntax is very similar to the copy commands:
mv file.txt Documents
Following what you already know, this command basically reads as ‘move file.txt to Documents directory’.

rm – This is the dangerous one, so use it carefully, and only on copies of files until you’re comfortable. This is delete, or ‘Remove’. The command is very simple:
rm file.txt
And it’s gone.

Play around with these couple commands. (Careful with ‘rm’ though – I’d suggest making copies of files and then doing a ‘rm’ on those copies…if you absolutely must see ‘rm’ in action. I won’t take responsibility if you delete something important!) They only represent a few of the many many things that are available to you at the command line.

To get a better idea of some of the arguments that you can apply to each of these commands we’ve covered, type:
man ls
Which will give you a Manual page for the List command. It gives you the description of the command, and all the arguments you can append to it to get more information than just the default response. (For instance, I prefer ‘ls -al’ because it shows me hidden files (the -a part) and gives a returns a long set of information about the files listed (the -l part). It’s a very useful command for me. But I suggest looking through the man pages to see what may be even more useful to you.

Next week I’ll show you how to take those commands with arguments and make shortcuts, or aliases out of them for quick access. As I’ve said before, leave questions in the comments below and I’ll do my best to answer them promptly for you.

  1. Just some typos: The last cp example says “cd”. Also you probably want to bold the “man ls” example.

    Share
  2. thanks Howard – taking care of that now.

    Share
  3. Another comment that can be useful is the “apropos”-command. When you want to do something, and you don’t know what the command for doing the thing you want to do is, just type

    apropos “whatYouWantToSearchFor”

    and it will give you a list with commands that could solve your problem.

    Share
  4. The mv command is also used for renaming files. Ex. mv text1.txt text1.htm will rename text1.txt to text1.htm
    Another good command is cat which is used to create files and look at files, like cat text will display the contest of the file and cat > text.txt will create the file text.txt
    But if you’re just going to creat a text file I recommend using vi, vim, nano, emacs or any other good shell editor. Like vim text.txt will let you creat that textfile for editing at once.
    Be careful when you use cp, because you can do cp text1 text2 and text2 will be overwritten with text1 if the file already exists.

    Same with rm text* can be used to delete all files where the name begins with text and rm text * will delete text file plus every other file in that directory without telling you.

    Use the who command and you will see every user logged on.

    Use the less command to browse inside files without being able to edit them. Can be handy when reading README files etc.
    less is more is a common quote(?!) and means that the command less shows more then the command more. If I remember correct, when you use more, you won’t be able to scroll back up. This works with less.

    grep is a good command to search a file for words etc, but I would rather use spotlight for this. And I never really got around to use it much in linux.
    If someone wants to continue here on tutorial of grep I’ll be glad. Seems to be a great command and probably does more then I know.

    Share
  5. I forgot to mention that if you use vi or vim (vim is just an improved vi editor, Vi IMproved or something like that) you edit the file by clicking a or i, a is before the cursor and i after the cursor. When you’re done with the file you hit Escape key and the type :wq! to write the file and to quit. If you don’t want any changes saved, you can do :q! to just quit.

    More info on vi here http://www.eng.hawaii.edu/Tutor/vi.html

    Share
  6. I would put aliases last on the list of things to teach people. Over the years I can’t tell you how many people got used to their aliases and after a while they couldn’t use the standard interface to save their backsides.

    Instead, I would get into more “obscure” but useful commands, such as file, cut, and so forth.

    Share
  7. 1) pwd actually stands for Print Working Directory.

    2) In the above examples the use of ‘..’ to “tell” cd to move a level up isn’t quite accurate. Instead, ‘..’ is a directory itself, which links to the parent directory. All directories (or folders) have ‘..’ in them, even those at the top (/). All directories have ‘.’ in them as well, which is a directory which links to the current directory. The effect is the same but the semantics aren’t, given the writing – ‘..’ is not a special case and is not tied to the ‘cd’ command but rather part of the file system.

    UNIX semantics become important as you go deeper so it’s best to learn these concepts properly from the start.

    Share
  8. [...] Now, at this point, it’s important to understand a few things about the Terminal. For one, the commands that you can type are interpreted and carried out immediately, no waiting around. So, if you tell it to remove a file, it will do it right then, with no easy way of recovering it. There isn’t a recycle bin on the command line (not without a little coaxing anyway). Secondly, since Unix was developed decades ago, many of the commands seem a bit archaic. Back when most of these utilities were written, they were all abbreviated to save space and cut down on the number of keystrokes you’d need to type. Below is a list of a few essentials, and another list from Nick’s post is here. [...]

    Share

Comments have been disabled for this post