understandable linux tips, tricks and tutorials

In this article, we’ll look at some more advanced commands, and how these can be used in day to day tasks on your linux box.

Redirection

Before we look at specific commands, there is a "technique" that you should know about that can be very useful when using the command line. Redirection is the technique of taking the output of one command and using it as the input for another command or action. This is part of the unix philosophy of taking small single purpose programmes and stringing them together in ways that are only limited by a users imagination. The main ways to link programs together is using the | symbol and the > symbol. The | is a pipe, and takes the output of one command, and feeds it to another. The > symbol redirects output to a file rather than to the screen. If you put two >>‘s together, then the output of a command is appended to the end of the file. So, if you wanted to create a file with a directory listing in it, you can do:

ls -al > listfile

If you want to find a long command you recently typed, rather than having to try and remember it, you could search through your command history by piping the output of the history command through a parser called grep (see below for more on grep):

history | grep 'keyword'

cat

cat is a strange command, because its real purpose is to join files together. However, it is also useful for just displaying the contents of a file. So to see what’s in your /etc/X11/xorg.conf file, type:

cat /etc/X11/xorg.conf

less

less is also used to display files, but in an interactive way. less displays all of the file that it can on the current screen, and then you use the up and down arrows to navigate through the file. To exit the screen, just press q. One cool thing I found out after a long time of using less, is that you can search a file while using less. You do this by pressing the / key, entering the string that you are searching for and pressing enter.

heads or tails

You can look at the top or bottom part of a file using the head (for the top) and tail (for the bottom). tail is very useful for looking at log files. For example, if you want to look what happened when you connected your ipod to the usb port, you would type:

sudo tail /var/log/messages

This will list the last 10 lines of the main log file. If you need to see more lines, you can specify this with:

sudo tail -n 50 /var/log/messages

Please note that the sudo is not necessary, it’s just that you generally need to be root to look at /var/log/messages. If you’re looking at the end of an ordinary file, you won’t need sudo.

Another cool thing tail will do is show you the last 10 lines of a file in real time, as it is updated. So if you want to see what happens when you plug in your ipod in real time, you just type:

sudo tail -f /var/log/messages

grep

grep is a very powerful regular expression tool for searching for text either in a file, or in output. grep allows you to use the power of regular expressions to find what you’re looking for. grep prints out all the lines of a file, or output that have the word or expression you are looking for. Providing a tutorial on regular expressions is beyond the scope of this article, but there is quite a good resource here. For simple tasks like seeing if you have direct rendering for 3d effects on your graphics card, you would type:

glxinfo | grep 'direct'

To see what drivers X is using as part of your xorg.conf, you would type:

cat /etc/X11/xorg.conf | grep 'Driver'

One final thing is that grep only prints out the lines that match. Sometimes you need some context as to where in a file a line appears, or what appears above and below the matching line. Say you wanted to see 5 lines before the match, and 10 lines after, you would type:

man mplayer | grep -B 5 -A 10 'xv'

find

If you want to perform some command on a whole bunch of files in a directory, but not all the files in that directory, you can use the find command to locate the files, and then execute the command on just the files that are found. For example if you’ve got a whole lot of tarballs in a directory that you want to uncompress, you could type:

find . -maxdepth 1 -name "*.tar.bz2" -type f -exec tar -jxpf {} ;

Breaking this down, it finds all the files in the current directory (symbolised by the dot) that end in .tar.bz2. It will only search in the directory tree to a depth of 1, which means only in the current directory (-maxdepth 1). It will only find files (-type f). Once it’s found the file, it will execute the command "tar -jxpf" (-exec tar -jxpf) on each file found (symbolised by {}). Find is a very powerful tool, and explaining all its intricacies is beyond the scope of this article, but have a look at this article if you’re interested in learning more.

ps

ps is a command which shows you all the processes running on your machine. To get a listing of all processes of all users type:

ps aux

This is most useful for finding the process ID of a running programme – usually, so you can kill it (see here to learn more about killing things).

top

If your cpu is running at 100%, and you don’t know what’s doing it, or you want to find out what application has eaten all your memory, you can find out using the top command. Just type top at the command line, and you’ll get a list of the top processes ordered by various crieria.

By default top lists the processes by cpu usage by default, but by pressing shift+M, you can also list them based on memory usage. If you want to kill a process, you can press k and enter the pid number. To exit, press q.

dmesg

dmesg is a programme that displays hardware message from the kernel. It is useful for seeing what’s happening with your hardware – particularly when you connect a new piece of hardware to your computer. You can use tools like less, tail and grep to help you navigate the output.

2 Comments
  1. awesome stuff, very good for beginners

  2. gr8 command,easy language…really gr8

Leave a Reply

Site last updated October 20, 2012 @ 12:53 pm; This content last updated September 12, 2012 @ 3:19 am