In our previous shell-related articles we have discussed a few of the very basic command line applications, in particular we discussed ls. In this article we'll go over a few more, and explain a little more about the difference between a command-line application and a shell builtin.

Shell builtins, which should you know?

Shell builtins are things which look like commands but are built into the shell rather than being sub-processes which get run. Builtins are typically the control-flow commands such a while and for (which will be covered in another article about shell scripting) but are also commands which affect the state of the shell itself, such as cd or set.

cd

The cd command changes the current-working-directory of the shell. It is the builtin command which you use to navigate the filesystem in your shell. It can be used to change to any directory (to which you have access) and can be given a relative or absolute path.

For example: cd /tmp will change to the /tmp directory (where temporary files tend to go), where as cd work will change to the work directory which is inside your current working directory, wherever that is.

In addition, if cd - is entered then the shell will typically change to the previously selected directory, and cd on its own will change to your home directory (typically something like /home/MYUSERNAME).

There's more to cd than even that, but you can read your shell's manual page to learn more.

pwd

Interestingly, pwd does not have to be a builtin, although it typically is. The pwd builtin prints the current working directory to its output. pwd is even less complex than cd and if you want to know more about it, then read your shell's manual page, it will detail the rest.

exit

The exit builtin terminates the shell which runs it. It can take an argument which is the exit code to return to the calling process. More on that in a future article about shell scripting.

More builtins

There are plenty more builtins, you should look at your shell's manual page for more information about them:

  • The 'Builtins' section of dash(1) for example.

Useful shell commands for your toolbelt

The more complex commands used from the shell are typically external applications which get run when you invoke them from the shell. We've already encountered the ls command which lists files. We won't go into more detail on that one here, so you should read ls(1) to learn more about it.

cat

The cat command 'concatenates' the files listed as arguments. However in simplistic use it can be used to display the contents of a file to your terminal. For example cat /etc/hostname will show you the name of the computer your shell is running on.

You can read more in the cat(1) manual page.

less is more

When you want to view a larger file, using a pager can help. A pager is so-called because it pages its input to the terminal, waiting for you to indicate that you're ready to read more of the file before it displays it. more is the traditional Unix pager, and in good Unix tradition, when someone wanted to replace it they wrote less because, as we all know, less is more.

less is a very capable application, but in basic use, less /etc/services is the sort of command you'll want to view a larger file such as this list of service name to port number mappings (/etc/services).

If you want to know all the things that less can do, then look at the less(1) manual page.

cp

When you need to copy a file (or collection of files) from one place to another then cp is your friend. In its most basic use, you can copy a file from one place to another with cp currentname newname. cp can cope with the newname being a directory in which case the file will be copied into the target directory using the same leafname as is in currentname, for example: cp /etc/passwd /tmp/ will copy the file /etc/passwd into /tmp/passwd.

It's possible to use cp to copy entire directory structures, or multiple input files (via a glob) to a target directory.

To learn more, read cp(1).

mv

When you want to move (or rename) a file, then mv is your friend. With a similar syntax to cp, you simply mv currentname newname and the rename will happen. mv can cope with transferring files between disks (e.g. a USB stick and your HDD) or simply renaming files within a disk.

You can learn more in the mv(1) manual page and if you want to do even more impressive renaming efforts then you could take a look at rename(1) and friends.

rm

The final command in our basic toolbelt is rm. This command removes (deletes) files from the computer. It's worth noting here that there is no traditional UNDO for this. If you rm a file, it is gone, unlike if you move to trash in a traditional desktop file manager type application.

The commandline syntax is pretty simple: rm somefile will delete somefile. It's possible to remove entire directory trees and you can learn more about recursive removal, verboseness etc, by reading the rm(1) manual page.