How commands work - Linux Nitty Gritty: Working at the Ubuntu Command-Line Prompt (2011)

Linux Nitty Gritty: Working at the Ubuntu Command-Line Prompt (2011)

How commands work

One of the beautiful things about bash is that it is entirely logical. Nowhere is this better shown than with how commands work. Once you’ve figured out how one command works, learning the rest is easy.


Some commands can be issued on their own. An example is ls, which will list files (the equivalent of dir, that you might have used under DOS/Windows). Try it now; open a terminal window if one isn’t already open, and type ls. Here’s what I see on my test PC:


Desktop Documents Examples Music Pictures Public Templates Videos

This is a listing of the files and folders in my /home folder.

While some commands are good to go on their own, most commands take arguments. In other words, they need to be told what file(s) or folder(s) to work with.

As mentioned, the cd (change directory) command is used to switch into another folder. It needs to be told what folder, so we supply this as an argument. The following will switch into the Documents folder:

cd Documents

Some commands need more than one argument. Say that we wanted to copy the file report.doc from the Documents folder to the Desktop folder. The cp command is used to copy files and folders. We tell it the location and name of the file to be copied as the first argument, and specify the destination as the second argument:

cp ~/Documents/report.doc ~/Desktop/

In this example, we’ve used the tilde symbol again as shortcut for the /home folder. From left to right, on my test system the command would translate as copy (cp) the file report.doc found at /home/keir/Documents/ to the destination folder /home/keir/Desktop/.

Once a command is entered, all that’s usually seen is the prompt again. There’s no confirmation. bash doesn’t say “OK” or “Command completed!”. bash is the silent type. It’ll only speak if it has to, usually to tell you of an error.

Command options

Most commands also have command options. These alter how the command works. Going back to the file listing command (ls), one problem is that it lists files without any additional information. What if we want to know who owns the files, or what permissions are set? For this we use the –l command option. Most command options are preceded by one or two hyphens, so that bash knows they’re not arguments.

Here’s the ls command used on its own, and then with the –l command option:


Desktop Documents Examples Music Pictures Public Templates Videos

ls –l

total 28
drwxr-xr-x 2 keir keir 4096 2008-10-24 10:07 Desktop
drwxr-xr-x 2 keir keir 4096 2008-10-24 10:07 Documents
lrwxrwxrwx 1 keir keir 26 2008-10-24 09:51 Examples -> /usr/share/example-content


Another useful command option for ls is –a, which causes all files to be listed—even hidden ones.

Command options can be combined together. To do so, simply list them one after the other. To see all details about files, and also any hidden files, we would type ls –la.

ls –la is a very common command used by many Linux users daily.

Command options are usually letters, but sometimes words are used too. In fact, to view a quick list of options, you can use the --help command option (that’s two hyphens, rather than one). For example, ls --help, or cp --help. This option works with virtually all commands.

Complex filenames

Filenames with spaces in them present problems because a space between items on a command-line is taken as an indication that command arguments or options follow.

For example, the rm command is used to delete files. If we wanted to delete a file called third quarter report.doc, and typed rm third quarter report.doc, we’d see the following series of error messages:

rm: cannot remove ‘third’: No such file or directory
rm: cannot remove ‘quarter’: No such file or directory
rm: cannot remove ‘report.doc’: No such file or directory

In other words, the rm command interprets each word in the filename as a separate argument—it thinks you want to delete a file called third, a file called quarter, and a file called report.doc.

The solution is simple—just wrap any file or folder name containing spaces in quotation marks:

rm “third quarter report.doc”

This should be done for file/folder names that include symbols too.

TIP Another method of dealing with spaces or symbols is to escape them. This involves typing a backslash (\) before each space or symbol. To delete the above file, you could type rm third\ quarter\ report.doc. Most times quotation marks work fine.

Relative and absolute paths

A path is simply the part of a file or folder listing describing where it is located in the filesystem—/home/keir/Documents/report.doc, for example. Paths can be expressed in two ways: relative and absolute.

Imagine that a user called Frank is using the command-line and is browsing his Pictures folder, whose location is /home/frank/Pictures.

NOTE Remember that the root of the filesystem, indicated by C:\ under Windows, is indicated in Linux by a single forward slash (/).

What if Frank wants to view photos from his vacation, and so wants to switch to the Disneyland folder, that is in the Pictures folder?

Frank’s been paying close attention to this book, because he knows he can type the cd command, as mentioned above. But what should he type next?

He could type the full path to the folder, like this:

cd /home/frank/Pictures/Disneyland

This works fine, but it’s a lot of typing.

In the above command, Frank has specified the absolute path to the file—the full location, from the root of the filesystem up. Of course, it would make far more sense if Frank simply typed:

cd Disneyland

After all, he’s already browsing the Pictures folder.

Frank doesn’t realize it, but here he has specified a relative path. Rather than provide the path from the bottom-up, he specified a filesystem location relative to where he’s currently browsing.

NOTE The folder a user is currently browsing is sometimes referred to as the working directory.

Once Frank has switched into the Disneyland folder, he can switch back to the Pictures folder by typing the following:

cd ..

The two periods are more command-line shorthand, this time indicating the parent folder of that being browsed.

With this in mind, what if Frank is browsing /home/frank/Pictures/Disneyland, and wants to switch to /home/frank/Music?

Frank could specify the absolute path (i.e. cd /home/frank/Music), but he can also use a relative path, like this:

cd ../../Music

In this case, Frank has specified the parent of the folder he’s browsing, and then the parent of that folder (i.e. the parent of the parent). At that point he’s back in the /home/frank folder, so the final component of the command specifies the Music folder.

It’s possible to navigate from any point in the filesystem to any other using a relative path. Of course, it’s not always the most efficient way of working, and sometimes specifying an absolute path is quickest. It’s down to the user to choose the best method each time.