The command-line phrasebook


Not everyone who's into Linux is a dyed in the wool techie. While some people need to know the intimate workings of their PCs and what runs them, others are quite happy simply to use them. There will always a certain amount of crossover, but the one thing that neatly distinguishes the techies from the power users is the command line interface (CLI).

Old-school Linux users swear that it's the only real way to do things properly, while the rest of us often avoid it like the plague. But what if we gave you just enough command-line knowledge to let you do all the important things, without having to don sandals and a fake beard?

New users are often scared off the command line by its combination of fussy syntax and the need to type accurately. There's also an uncomfortable air of geekiness associated with working in a terminal that worries some folk. Despite this, many command-line phobes wish that they could do more with it, in much the same way as many British people would genuinely like to speak another language, if it wasn't just too darned hard.

When we go to a foreign country, we can fall back on the tried, tested and rather embarrassing technique of speaking slowly and loudly and hoping that someone speaks English, or we can look up some key phrases in a book to get by. It's not the same as learning the lingo, but it does mean that you can make your way between the beach, the bar and the restaurant in times of need.

You can take a similar approach to the command line. There are times when it's advantageous to ditch the graphical interface and get your hands dirty, but you don't have to be a certified technical guru in order to do so. Some key commands and variations on them can get you by, just as you can get by overseas with just a few key phrases.

Managing files in a graphical interface is easy, but there are times when you can do the same job faster at the command line.

Managing files in a graphical interface is easy, but there are times when you can do the same job faster at the command line.

Going places: cd

Most desktops environments have some form of graphical file manager (such as Nautilus in Gnome and Konqueror in KDE), and most people are familiar with directories being represented by folders in a file manager into which you can put files or other folders. You can look at the contents of a directory using a file manager, but there are times when you might need to perform actions within that directory. Often you may find it more efficient to do this via the command line. Moving between directories is pretty easy, courtesy of the cd command. For example, to move into your video folder:

cd /home/username/Videos

This assumes that you have a Videos folder in your home directory. Replace 'username' with your username. To move into your Music folder:

cd home/username/Music


cd /etc/mplayer

moves you into the mplayer configuration directory.

Take a shortcut

You can use shortcuts with cd. For example, the ~ character can be substituted for the path to your home directory. To enter your home directory in this way, just type

cd ~

or get to the Videos folder as above with

cd ~/Videos

When you successfully change your directory, you'll see the new directory displayed in the terminal at the prompt. To go back to the previous directory, you can use the shortcut cd -. This can be useful if you need to quickly jump back and forth between different directories.

Create a folder: mkdir

If you need to make a new directory, you can use the mkdir command. For example, to make one called New, try the following:

mkdir New

To make a directory within an existing directory, move to it with cd, then create a new one. So to make New2 inside New, you need to do the following:

cd New
mkdir New2

If you want to create a directory structure made up of several nested folders one inside the other, it can be fiddly and time consuming to move to each folder and make new ones. In a graphical environment this would also take several steps, but you can do it in a single command by using mkdir and the -p switch. To make folder1 inside folder2, which is in turn inside folder3, try this:

mkdir -p folder3/folder2/folder1

Where am I?: pwd

If you've been changing directory regularly and creating new ones, it's quite easy to lose track of what directory you're in. It's a good idea to check from time to time using a 'Where am I?' question. You can ask this by using the pwd command. Enter this into a terminal and you'll be told the present working directory, which is what the letters in the command stand for.

Move and copy: mv & cp

So much for navigating directories - but what about copying and moving files and folders about? In a graphical interface, you open source and destination folders and click and drag between the two. It's a simple action, but navigating to source and destination can take time, especially if they're deeply nested. Sometimes it can be easier to use the command line.

Copying and moving are very similar operations. In both cases you have a location and filename for the source and a location and filename for the destination. In the case of moving, the original file is deleted, whereas if you're copying a file, the original is retained. The command for copying is cp; the one for moving is mv. They're used in a similar way - just enter the command followed by the source and target files.

To copy the file text.txt within the same directory, do

cp text.txt text1.txt

Because you're creating a new copy within the same directory as the original, you need to give the new file a new name (text1.txt). This isn't necessary if you're changing directory - assuming text.txt is in your home directory and you want to copy it to the Documents directory, you could use

cp text.txt ~/Documents/text.txt

To move a file instead of copying it, use mv:

mv text.txt ~/Documents/text.txt

If you need to copy or move something from a different directory to the one you're currently working in, you can do so by specifying it with the source file. So to copy the text.txt file from the Documents folder to the Music folder, just do this:

cp ~/Documents/text.txt ~/Music/text.txt

And to move it instead of copying it, substitute mv for cp.


Copying and moving single files this way is admittedly a bit of a hassle, but you can speed the process up no end when you bring in wildcards. For example, to move all the JPEG images hanging around in your home folder to a subdirectory for pictures, you can use *.jpg.

mv ~/*.jpg ~/Pictures

If you want to move or copy files into the current directory (that is, the directory you're in), you can use another shortcut. A dot (.)on its own is interpreted as the current working directory. So to move all the MP3s from a Music directory to the current one, use

mv ~/Music/*.mp3 . 

You can also use mv to rename a file. This may seem a little counter intutitive at first - you're actually 'moving' it from the source directory, back into the source directory, and giving it a different name.

mv text.txt words.txt

What's here?: ls

You can find out the contents of a directory at any time by using the ls command. At its simplest, you just type ls and press Enter and you'll get a list (hence the name, ls) to see all the files and folders in the current directory.

You don't have to stick with the working directory though - you can specify any folder by using a relative or absolute path. For example, if you're in the home directory and want to find out what's in the Pictures subdirectory, you can use.

ls Pictures

If you're not in your home folder and want to list the contents of a subdirectory of your home folder, use

ls ~/Pictures
The ls command displays all directories and files in a particular directory.

The "ls" command displays all directories and files in a particular directory.

More wildcards

By inserting a wildcard, you can list files of any particular type.

ls ~/Pictures/*.png

This displays all the PNG files in the pictures folder. If you've got several subdirectories in the Pictures directory, you can list them as well, by using the -R switch. To see all the files in the Pictures folder and all its subfolders, enter:

ls ~/Pictures -R

You'll see separate lists for each of the subfolders found in the Pictures folder. If you need to see hidden files, use the -a switch.

ls ~ -a

This lists all files and folders, including hidden ones, found in your home folder.

The -X flag

Some graphical file managers enable you to sort files and folders by name, type, when they were last modified and so on. You can also achieve this on the command line using the ls command and suitable switches. To sort by type, simply opt to list by file with

ls -X

This provides the contents of the current directory sorted by extension starting with any subdirectories and then displaying files by extension alphanumerically. To sort by time and date, substitute the switch -t; and to sort by size, use -S instead. You can use multiple switches at the same time, but remember that some will undo the work of previous ones. For example, ls -t -a sorts the contents of the current directory by time, including hidden files, while ls -S -X results in the contents simply being listed by type and not by size, followed by type.

Where is that file?: locate & grep

There are several commands that you can use to search for files. The first, locate, uses a database of filenames to search for your file. It returns a list of all files that match the search, including their full paths.

locate text

This returns all files with the string 'text' in the filename. By default the search is case sensitive, so it wouldn't return a file called Text.txt, for example. However, using the -i switch makes it case insensitive. When you get a lot of results, as in this example, you might want to limit the number of returns to make them more manageable. You can do this by using the -n switch, followed by the number of returns that you want.

locate -n 5 text

Will return the first five results with 'text' in the filename or path.


Sometimes you need to dig a little deeper than the filename, and look at the contents of the file. You can do this by using the grep command. To search for 'Hello' in Text.txt, use the following:

grep Hello Text.txt

The command returns the line in the file containing the string 'Hello', if present. Grep is case sensitive, so it wouldn't turn up 'hello'. If you want to search a number of files for your term, you can always use a wildcard:

grep Hello *

This searches all files for 'Hello'. The results include the filename and the line that includes your search string. If you're searching for a string that includes spaces, enclose it in single quotes. To search for 'Linux Format' in all the files in a directory, use the following:

grep 'Linux Format' *

To make your search case insensitive, add the -i switch. If you want to search through subdirectories as well, make the search recursive by adding the -R option.

grep -i 'linux format' *

Will search all files in the current directory for 'linux format' ignoring case. Remember that making a search recursive or ignoring case will add a large number of results and extend the time it takes to carry out the search. It's always best to be as precise as possible if you want a speedy response.

View files: head, tail & cat

Having found the file you've been looking for, you'll probably want to take a look at it. If it's a text file you can do this in the terminal you're using to enter the commands. If you only want to look at the first 10 lines of a file - to check you've got the right one after a search, for example, you can use head. Move to the directory that includes the file you want to examine, then type head followed by the filename. To see the first 10 lines of the Text.txt file use

head Text.txt. 
Use the head command to view the first 10 lines of a text file.

Use the "head" command to view the first 10 lines of a text file.

To change the number of lines displayed, use the -n option, like this:

head -n 7 Text.txt

This returns the first seven lines of the Text.txt file. If you'd rather see the last lines of the file, you can use tail instead of head, like so

tail -n 5 Text.txt

This provides the last five lines of the Text.txt file. This can be handy for quickly examining the end of log files after a particular event. If you need to look at the whole file, the cat command will do this for you:

cat Text.txt

This will display the whole of Text.txt, but if it's longer than one screen, the first part of the file will whizz by and you'll need to scroll back to see the rest of it. To look at a file one screen at a time, use the less command instead.

less Text.txt

You can then use Page Down to move through the file one page at a time, or navigate using the arrow keys. Unlike head and tail, less doesn't return you to the command line until you press Q to quit.

You can examine a long file screen by screen with less.

You can examine a long file screen by screen with "less".

Archiving: zip

Large files can be compressed and added to an archive using the zip command. To turn a file called image.tif into an archive called, use the following:

zip image.tif

Having created the zip archive called, you can add other files to it. To add image2.tif and image3.tif to the archive, try the following:

zip image2.tif image3.tif


If you need to password-protect and encrypt the zip archive, use the -e option. For example, in the original example above, enter:

zip -e image.tif

Once you've hit Enter, you'll be asked to provide a password and repeat it for verification (this will also be needed when you come to extract the archive). To extract this, use unzip.


If the archive is password-protected you'll be prompted for the password before you can extract the contents.

Look it up...

When you're abroad you'll inevitably come across words that you don't understand. To find out what they mean you can always look them up in a translating dictionary, but what about Linux commands? You'll need to find out what they do and how to apply them. Thankfully there are several ways to find information about different commands.

The command line equivalent of looking up a word in a French-English dictionary is to use the man command. This is short for manual, and gives you an overview of a command plus some examples of how to use it. To try it on the mkdir command, do:

man mkdir

You should get a page of information giving brief details of how to use mkdir and its switches. Use the arrow keys to move through the information, and press Q to quit when you're done.

Man pages do vary in quality somewhat, as they're generally written by the author of the command concerned. Some have more detailed pages than others. If you're after more information, try using the info command. Info pages are usually organised into sections. Page Down and Page Up enable you to move forwards and backwards through a section. Press Q to quit info and if you need help navigating info pages, press ? at any time. This gives you a full list of keys and commands for using info. Some info pages can be quite complex, so remember these navigational commands - and good luck!

Info gives full information about a command in an easy-to-read form.

"Info" gives full information about a command in an easy-to-read form.

First published in Linux Format

First published in Linux Format magazine

You should follow us on or Twitter

Your comments

Very usefull

Very useful info.
I remember it taking me a while to get my head around CLI commands when I first started using Linux, years of DOS commands are hard to shift!

Great compilation. I suggest

Great compilation. I suggest including the -l option of ls, since its often needed to see more information about the files in a directory.

The command-line phrasebook

This is of assistance to a new arrival in Linux but having been steeped in using graphical interfaces does not mean too much to them. I can see when someone requests information in a post to seek information from you in order to assist that this knowledge will reassure that you are not being lead astray.

The bigger problem which you do not address is when having followed the various requests to provide information you are then asked to use the terminal to amend a file. Too often it is assumed that the individual knows where the instetion point is and what possibly has to be deleted to effect the required change.

Tail - watch logfiles

most useful is using tail with the option "-f": this starts continously prompting the last lines of a file (e.g. from /va/log) to the console dynamically adding the newly created lines - so watch what happens on your system!



I'm a very recent Linux convert, quite pleased to see that some of the basic terminal commands are very similar to MS DOS, only more powerful!

Little suggestion

As this article seems geared toward complete beginners, I suggest you could add the meaning of each command. For exemple, cd is for "Change Directory", mkdir is for "MaKe DIRectory" etc...
Myself, I recall more easily things when I understand the meaning. Usually, if I don't understand what things mean, I don't recall them.
Just my 2 cents ;)

File gobling

You actually don't need the '.' in '*.jpg*'(although this would select files named blalallajpg as well).

"While some people need to

"While some people need to know the intimate workings of their PCs and what runs them, others are quite happy simply to use them."

That one sentence explains a lot. It perhaps should be reworded as "While some people need to know the intimate workings of their PCs and what runs them, others are simply too lazy to learn anything other than how to push a mouse around and click on silly little pictures. Etch-A-Sketch, anyone?"

As for the command line being fussy, so what? We older UNIX jocks don't have a problem with it and all of us are suffering from arthritis due to years of typing ls -1, cd, mv, etc. In my case, I'm just a BigDumbDinosaur (uh, sorry...that's Big Dumb Dinosaur for those who can't read unless there are spaces between words) and if I was able to learn how to type and tell the computer what I want to do, why can't you young mouse-maulers do the same?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Username:   Password: