A quick introduction to the Linux shell

This article hasn’t been updated since April 2005. If you found this page via a search engine and you know what you’re looking for it may still be of use. However, if you wanted a general overview of the topic you should consider finding a more up-to-date resource. (I removed all links to this page from my site, but I left the page so you wouldn’t get a “404 Not Found” error when you came across it via a search engine or link.)

The shell, or command line, is one of the most powerful tools available to the Linux user. This introduction starts with the basics, but moves on to advanced techniques.

Starting a shell

If you use a desktop environment such as KDE or Gnome, you need to launch a terminal emulator, such as Konsole or xterm. Alternativly, you might have used SSH to connect to another machine, or might not be using X at all. It makes little difference though, I'll assume you've started a shell. I'm also going to assume you're using bash, in my opinion the best shell running on Linux, and usually the default. Type ps, if you see bash in the list given, you're running bash.

The very basics – navigating the filesystem; getting help

Commands covered: ls pwd cd man. Also used: ~- ~

ls – list directory contents
Type ls at the prompt. You'll see a list of files present in the current directory. Try ls -l to see more information about the files. ls -lhS will show human-readable sizes (i.e. 10M instead of 1000000) and sort the entries by size. Try ls --help for more options.
pwd – print working directory
cd – change directory
Type pwd to see what directory you're in at the moment. Then do cd / to change to the current drectory, then ls to list it's contents. cd /etc will take you to the /etc directory, cd documents will take you to a directory called documents within the current directory, cd on it's own will take you to your home directory. cd ~- will take you to the previous directory. cd ~ will also take you to your home directory, since ~ is equivalent to your home directory.
man – manual
The manual just that – a comprehensive manual on almost every command. Type man ls to get help on a specific command. Sometimes, commands are built in to the shell, in which case you have to do man bash and find the command in there. Often easier is to try adding --help to the end of the command, for instance, ls --help.

Manipulating files

Commands covered: touch cat cp mv rm mkdir rmdir. Also used: > ^D ? * ...

touch – create empty file/change timestamps
Go to your home directory (cd), and type touch a-file. Then do ls -l. Notice the file's size: 0B, it's empty. touch simply makes blank files. It can also be used to change their last modified dates, see touch --help.
cat – concatenate files
> – redirect output
Type cat /etc/passwd. The file /etc/passwd will be written out to your terminal. cat a-file seems to do nothing – that's because the file is empty. Try cat /etc/passwd > a-file, then do cat a-file again. Notice what the > did: instead of writing the file to the screen, it wrote it to the file. We say that > redirected the output of the cat command into a file. This time just do cat. The command doesn't finish, it's waiting for you to type something. Type in a few letters and press Enter. Because we didn't give a file for cat to type out, it uses the keyboard as if it were a file: whatever the keyboard (called here standard input) gives to cat, it writes to the screen (called standard output). Press Control+D, hereafter abbreviated to ^D. The command finished, ^D is the end-of-file character, it tells the cat program that you've finished typing. This time do cat > a-different-file. Type a few lines in, and finish with ^D. This time, standard input was redirected to a file, instead of standard output. Type cat a-different-file to see the result.
cp – copy
mv – move
rm – remove
? – single character wildcard
cp is very simple, it just copies files. Try cp a-file yet-another-file, then ls. The result should be obvious! mv works in a similar way, except the file is moved instead of copied, for instance mv yet-another-file b-file. Remove, rm removes files – be careful! You may not get a warning before deleting files! Do rm yet-another-file and ls. Then do rm -i ?-file. The ? is a wildcard, that is, it matches any single letter. In this case, it should match the letters a and b and remove the two files. The -i will prompt you to check you want to remove the files, use y to respond. As usual, cp --help, mv --help and rm --help for more information!
mkdir – make directory
rmdir – remove directoty
* – multiple character wildcard
.. – parent directory
Do the following, you should be able to follow what is happening: mkdir a-directory cd a-directory touch another-file cat /etc/passwd > one-more-file ls cd ~- ls ls a-directory cp -R a-directory copy-of-dir cd copy-of-dir ls cd .. ls rm -f a-directory rm -Rf a-directory rm copy-of-dir/* rm copy-of-dir rmdir copy-of-dir We made a new directory, changed into it, put some files in it, then moved back out of it, copied it, deleted a copy (note the -R needed to remove a directory), then deleted the files in the remaining directory, and removed the empty directory. Note that .. means the parent directory, that is, the directory in which the current directory is contained.

Web page still being written!. I'll write some more soon. I aim to cover at least these commands, which I seem to use the most often, you can try adding --help to the end of them to see what they do: bg chmod curl date df du emacs find finger grep gv history host kill killall less ln netstat ping ps rename scp ssh tail top w wget which. More commands will be added later, these should be written soon (say, by the end of January).