Getting started with the Linux command line interface
If you ever used DOS or the command prompt on Windows machines then just forget everything you’ve seen. The linux command line interface (CLI) is amazingly flexible and powerful. Here are some essential commands to get you started:
List files in a directory
The handiest form is:
ls -l
which gives something like this:
-rw-r--r-- 1 james james 31908 2010-06-02 20:30 exploits_of_a_mom.png
The information here is: files permissions, number of links to this entry (ie files in a sub-directory), the file’s owner, the file’s group, the file size in bytes, date modified and filename.
cd
Change directory
To go up a directory:
cd ..
To go to your home directory:
cd ~
To go down into the test directory:
cd test
To go to the /var/logs directory:
cd /var/logs
And if you’re lost then the pwd command will tell you where you are
pwd
Show the present working directory
whoami
Tells you which user you are logged in as (I mainly included this because it makes me giggle). Can be useful – if your program is running on a web server and it can’t access a file then this might make things clearer.
ifconfig
Displays information about your network interfaces.
Useful for: finding your IP address, MAC address
grep
Search for a phrase
Search all .php files for the string “add_filter”:
grep add_filter *.php
Same but recurse all sub-directories:
grep -r add_filter *.php
Same but case insensitive (ie pick up “Add_Filter” or “ADD_FILTER” as well):
grep -r -i add_filter *.php
mv
Move a file
mv i_am_here.txt another_directory/now_i_am_over_here.txt
cp
Copy a file
cp lovelyfile copyoflovelyfile
rm
Delete a file
rm rubbishfile.txt
cat
Output a file contents as a stream. Useful when piped with “|” into another command, eg search the file myfile.txt for the string “frogs” (case insensitive):
cat myfile.txt | grep -i frogs
tail
Displays the end of a file (ususally used for following log files). As new lines are added they are displayed.
tail -f myfile
See find and backticks below for useful tricks.
Press Control-C (^C) to leave.
find
Find a file
As seen in my article on following your apache webserver logs you can find all files called “access-log” in the /var directory:
find /var -name access.log
unfortunately this gives errors for directories which couldn’t be opened (user has no permissions) so this is better:
find /var -name access.log 2>/dev/null
Linux commands give output streams called standard output (given number 1) and errors (given number 2). Here we send the error stream off to the device /dev/null (which basically throws everything away). We could do this:
find /var -name access.log 2>error.txt 1>out.txt
And now the file “out.txt” contains the output, in this case the filename and “error.txt” contains the error output.
Hints and tips: Backticks
You can send the results of one command to another using backticks (`), as in this example from my article on following your apache webserver logs:
tail -f `find /var -name access.log 2>/dev/null`
This does the find command first to get the location of the file “access.log” and then uses that filename for the tail command.
We could even pipe this result through grep to search for a phrase:
tail -f `find /var -name access.log 2>/dev/null` | grep login.php
This will give a realtime display of all requests to “login.php” on your web server. Neat huh?
wget
Retreive a URL (eg web page)
Download a file to the current directory:
wget http://www.somebodieswebserver/an_image.jpg
