GNU/Linux Commands

The command line is the most powerful method of interacting with GNU/Linux, however if you are not used to it the learning path can be steep. The best strategy is just to start using some basic commands. Don't attempt to do all your work from the command line straight away. Learn a few commands, use them and add to your understanding of what they can do over time. Then you can slowly extend your vocabulary of commands as you need to. Below are some basic commands that you could try starting with.

Don't try and learn all of them at once. Just choose a few and practice them.

  • man
  • ls
  • cd
  • mkdir
  • mv
  • rm
  • locate / slocate
  • ping
  • cp
  • pwd
  • tab

And some others that would be good to know:

  • ldconfig
  • updatedb
  • ./configure
  • make
  • make install
  • tar
  • more
  • whereis

So, lets have a look at each. Feel free to experiment with these commands. Be a little careful as it is possible to do some damage to your files, folder,s and even the operating system if you are too casual. If there is a possibility one of the commands can accidentally create havoc then I will make a note to warn you. So try some of these out in a terminal:

man

By entering man into the command line, you will execute the man program. This program allows you to read the manual or help page for a given program or command. To use it, you generally type man followed by a space, and then the name of another command or program. For example, typing:
man ls
The above will give you a terminal window filled with information about the ls command. At first the format of manuals might be a little confusing, however, once you get the hang of it, you can expect to find similar format or layout for almost all GNU/Linux manuals. The part you need to be interested in most is the description of the command (i.e., what it does). To scroll down the manual page press your space bar, and to quit the man page press the q key. 

Try some man commands and then read about the other commands I have listed above. There is also another help system that works the same way, but instead of typing man you type info and the command like so:

info ls
Almost all of the GNU programs and utilities have longer and more in-depth info pages than they do man pages.

ls

the ls command is the 'list' command. You can use this to list the contents of any directory you are in. Try typing this command in a terminal window and see what you get. Now, one feature of GNU/Linux commands is that you can add various parameters to them. This is quite a simple thing to do, and refines the way you use the command. Usually these parameters are added to the command by typing a ' - ' directly after the command and then the parameter names or abbreviations. For example if I type the following:
ls -l

Then I am passing the l parameter to the ls command. The l parameter is short for 'long list' and refers to a type of format that the ls information should be displayed in. This format gives more information than just typing the ls command by itself... Try the two out and compare the difference.

You might well ask 'how do I know what the parameters are for each command?' This information can be found in the man pages for each command and accessing these is easy (see above).

For the ls command I suggest you get familiar with the formats using ls by itself, as well as ls -al, and ls -l

cd

cd is the most common command used to navigate the file-system on your computer. cd stands for Change Directory. Try it out by typing ls to get a list of all the files and folders in the directory you are currently in. Now try typing ls followed by the name of one of the files in the list, for example if there was a file called 'me.txt' I could type:

cd me.txt
This will give an error! Why? Because you can't change to a directory if it is a file. It's good to try this so that you understand that you can't do any damage by making a mistake with cd. To change to a directory you type cd followed by the name of a directory you want to navigate to. If there was a directory called src listed when we tried the ls command, we would type:
cd src

If that was successful then the terminal won't throw up an error. Try it with a real directory on your computer. If you fail it will be because either you don't have permissions to enter the directory, you misspelled the directory name, or the directory simply doesn't exist.

Now, a word about GNU/Linux file-systems. Generally, if the system has been set up nicely for you, you will be working in your home directory. This is normally located in a set place in GNU/Linux. To find your home directory first type the following:

cd /
This will place you in the top directory on your computer's file-system.

If you now type ls this will show the list of directories on your computer at the top-most level of the file structure.

There are some important directories here, but you need to be most concerned with the one named home. To change to this directory we can use the cd command we learned earlier:

cd home

Now if you type ls you will be shown a list of more directories, and hopefully one that is the same as your username. This is your home directory. Now, we have been navigating to this using relative positioning, that is -- if I am in the top directory and I type cd home then I will be placed in the home directory where all the user's individual home directories are kept.

If I was somewhere else on the file system and I typed cd home I would get an error. If you need to, you can use absolute paths to the directory you wish to get to. As an example if I was in some dark corner of my file-system and I need to get quickly to the home directory I would type:

cd /home
If I needed to get to a directory under the home directory (let's say I have a directory in there called adam), I would type:
cd /home/adam

mkdir

This is the command you used to create a directory and is short for Make Directory. To use this, simply type the name of the directory you want to create after the mkdir command as so:
mkdir bleep
The above command will create a directory in the current directory I am in called bleep. If a directory with this name already existed, I will get an error and the computer won't overwrite the existing directory. Try creating some directories.

pwd

If you get lost and don't know where you are in the file-system you can always type pwd and it will tell you where you are. This command gives you the location path or absolute path to where you are. For example, if I am in my adam home directory, the output of the pwd command will be:
/home/adam
Experiment with changing directories with cd then typing pwd to see where you are.

mv

This command is short for Move. It is as it sounds in that mv allows you to move files around on the file-system. This command is like the 'cut' and 'paste' actions from Mac and Windows rolled into one. To use mv you must first type the command, followed by the file you want to move (in absolute paths or relative paths including the filename) and then the place where you want to move the file to (in absolute or relative paths). For example, if I wanted to move a file "me.txt" from my current directory to the "/usr/bin" directory I would type the following:

mv me.txt /usr/bin
Note: I don't have to type the filename in the path name where I want to move the file unless I also wish to change the name of the file. If for example while I was moving 'me.txt' I wanted to change the filename to "you.txt" I would type:

mv me.txt /usr/bin/you.txt
If I just wanted to rename the file and not move it I could use mv to rename the file without moving it by typing this:
mv me.txt you.txt
Note that when you use mv you are moving the file not copying it, so the original will be moved and won't exist in the same place you moved it from. Now, also be a bit careful because you can overwrite files accidentally, if for example I moved one file to a directory with a file of the same name, then the file I am moving will overwrite that file. Then you could be in trouble... so just be a wee bit careful.

rm

On the other hand, here is a command you should be very careful about using. rm is short for Remove, and is the command you use if you wish to delete a file or directory (and its contents). To use this command type rm followed by the name of the file you wish to destroy for good. To remove a directory you can use the same command with the parameter -R like so:
rm -R directoryname

Where directoryname is of course the name of the directory you wish to remove. You can also use rmdir for this which (you guessed it) is short for remove directory. Be EXTREMELY careful when using these commands, if used unwisely it could be the end of your operating system.

locate / slocate

These commands help you find files on your file-system. The location of all files on your system are stored in a database which is updated periodically by using the updatedb command. To find a file simply type either locate or slocate (depending which is installed on your computer), followed by part of the name of the file or directory you are looking for. For example if I am looking for the file "icecast.conf" I would type:
slocate icecast.conf
If I don't get any reply from typing this it means that either the file doesn't exist on my system or it exists but my database doesn't know where it is. In this later scenario I would type updatedb and try again.

With locate and slocate you can't destroy anything so experiment as much as you like. Sometimes updatedb might take a while to run if you haven't run the command recently or if you have a slow machine, it can also use a lot of CPU power on slow machines so never use it while you are doing something else 'mission critical' on your machine.

You might also like to experiment with whereis and find to look for files on your system.

cp

This is short for copy. Use it like mv , the only difference is that it leaves the original file where it was while also creating a copy.

ping

Not usually included in the top 10 commands you need to know but its handy if you need to know if you are online. ping sends a request to any computer on the net, if that computer gets the request it will respond. Type ping followed by a URL that you know, for example it might be a good idea to try the following:

ping www.cnn.com

If that computer gets the request you will get some information coming back through the terminal... this will keep scrolling so to stop it type ctrl and c and it will halt the ping process.

If you get no response from ping then you are probably offline. However, some machines online don't answer ping requests for security and other reasons, so make sure you really know that the machine you are pinging does reply to ping requests.

Also, some internet connections won't allow ping traffic, for example, while I am writing this I am in an internet cafe in Riga--its a fast connection but I can't ping, this is perhaps because they think only evil hackers use ping so they have some paranoid network security disallowing all sorts of useful things. 

tab

tab is not so much a command as a keystroke--very keyboard has a tab key, and its a very useful thing to have in GNU/Linux. You might have used this keystroke before to indent words in a word processor. You can still do this in GNU/Linux word processors, but when you use tab in the GNU/Linux terminal it becomes such a time saver that when you master it you will be using it all the time.

Essentially the tab keystroke is like an auto-complete. If, for example, I want to move the file 'dsjkdshdsdsjhds_ddsjw22.txt' somewhere with the mv command I can either type out every letter of the stupid filename, or I can type mv (for 'move') followed by the first few letters of the filename and press tab. The rest of the filename will be automagically filled in. If the filename is not filled in it means that there are several files (or directories) that start with those first few letters I typed. To remedy this I could type a few more letters of the filename and press tab again, or to help me out I could press tab twice and it will give me a list of files that start with those letters.

tab is your friend, use it a lot.

Other Commands

At the beginning of this section I said there where a few 'other' commands that might also be good to know, they were:

  • ldconfig
  • updatedb
  • ./configure
  • make
  • make install
  • tar
  • more
  • whereis

I have already talked about some of them, namely whereis and updatedb. The others might be useful if you are installing software.

more

more is use if you want to control the overly verbose output of any command to the terminal. If for example, I am in a directory which contains 1000 files and I type ls the output of the command won't fit nicely into my little terminal window so it will go scrolling past faster than is useful. To slow it down so I can read the output we follow the command with more like so:
ls | more

If I used this in my 1000 file directory I get one page at a time of output and pressing the space-bar shows the next page. Pressing q quits more. Ok, so you might be wondering what the funny straight line is in the above command... well, this is known as the pipe command.

Pipe allows you to combine commands together to control the kind of output you get, usually its used to refine a command (which is what the command parameters also do). So, when you get really fluent with these commands you can write things that look more like equations but are really efficient ways of using standard commands... pipe will be central to enhancing your efficiency.