Basic Linux Commands
grep. ls. history. Are all these commands confusing you? Don't know your tac from your cat? Then perhaps this article might be of some use.
Introduction
First of all, this isn’t going to be a comprehensive tutorial on Linux command line tools, rather a quick introduction to some of the more common ones.
And now, in pretty much random order, some useful commands.
Basic directory navigation
cd
Most people will have used the ‘cd’ command at some point in their lives, it’s one of the most simple to use.
To enter a directory you just type something like the following:
1 2 |
cd videos cd /home/joe/home/videos |
The difference between the two command about is that the bottom one defines an absolute path (the path starts with a trailing slash, /) and the top one uses a relative path. That is a path relative to the current directory (in this case we’d already be in the /home/joe/home directory).
Two other useful cd commands are:
1 2 |
cd .. cd ~ |
The first of these takes you to the previous directory, and the second one will take you to the current user’s (i.e. your) home directory.
ls
‘ls’ is the command use to list the contents of a directory.
1 2 |
ls ls /home/joe/home |
By default ‘ls’ will list the contents of the current directory, but you can also specify either an absolute or relative path to list (as in the second example above).
1 2 |
ls -lh ls -A |
Like most CLI (command line interface) commands, ‘ls’ has quite a lot of options you can use. a couple of useful ones are shown above.
‘ls -A’ will also list hidden files (i.e. files and folders whose names begin with a dot, .).
‘ls -lh’ shows additional information about the files, including their owners and file sizes.
pwd
pwd |
‘pwd’ simply shows which directory you are in currently.
history
history |
The ‘history’ command will show all the command line commands that the current user has executed. The output will be similar to what you see below (of course your history itself will be completely different).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
[matt@foo ~]$ history
1 rake setup
2 rake db:populate
3 thin start
4 thin -d start
5 git commit public/images/tango/16px/* public/images/tango/32px/*
6 git commit public/images/tango/22px/*
7 git commit public/images/tango/*
8 git add .
9 git commit public/images/tango/about_16.png
10 git rm public/images/tango/*.png
11 git rm *.png
12 git rm *.png -f
13 git reset --hard ddfc6f3a88ab4427d407b9fb47bea26f01b0b4a1
14 cd tango/
15 gcc --version |
A useful feature of the BASH history is that you can execute any command in the history by simply writing an exclamation mark and the number of the command.
For example:
1 2 3 4 5 6 |
[matt@foo ~]$ !15 gcc --version gcc (GCC) 4.4.0 Copyright (C) 2009 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
locate & updatedb
The ‘locate’ command is used to search for files and folders on your file system. The ‘updatedb’ command is used to update the database for ‘locate’. Most Linux systems will automatically run ‘updatedb’ at least daily, but sometimes it may be necessary to run it manually.
NB. ‘updatedb’ requires superuser privileges.
1 2 |
sudo updatedb locate assignment1 |
‘locate’ will find any file or folder that has the specified string in its name. Sometimes the output can be very long, so the next command will come in useful
less
‘less’ is used to show only one screenful of text at a time. It allows for both backward and forward navigation. ‘less’ can be used to paginate text files and the output of commands.
To read a text file you could write:
less text.txt |
This will show the contents of the text file one screen at a time. You can navigate up and down using your arrow keys (for one line at a time) or Page up and down (for a screen at a time). To quite type q.
To paginate the results of another command, you have to use a pipe character, |. For example, to paginate the results of the history command you’d type:
history | less |
cat and tac
‘cat’ is short for concatenate. As you might guess this can be used to join several text files together.
For example, the following command will output both file1 and file2:
cat file1 file2 |
This will not save the output anywhere, just display it on screen. For this reason you can use cat to display short files on screen by just giving it a single file:
cat file1 |
If you wish to save the joined file, you have to enter something similar to the following:
cat file1 file2 > joinedFile |
‘tac’ does exactly the same as ‘cat’, but in reverse. This is especially useful for log files (you see the most recent entries first) and can be used together with the less command:
tac httpd.log | less |
grep
‘grep’ is a powerful text search tool and I’ll only cover the most basic use here.
Probably the most common use of the ‘grep’ command is to filter the output from another command.
Thus you might run the following to find only those entries in the history log which involved the git command.
history | grep git |
man
‘man’ is probably one of the most useful and important command you have on Linux. The ‘man’ command will display the manual entry for almost any program or command that you have. This means that whenever you feel unsure of what options you can use, or what you can do with a particular command, you should use the ‘man’ command.
For example to display the manual entry for ‘ls’, you’d type:
man ls |