Unix Tip: Commands

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:

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:

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.


Comments have been disabled for this post