Useful UNIX commands

seq

seq is a very useful tool for batch commands.

for i in `seq 1 24`; do echo $i; done

for i in `seq -w 1 25`; do convert -resize 35% IMG_00$i.JPG small-$i.jpg; done

locate

locate document.pdf

locate document.pdf | xargs xpdf &

The locate command works by querying a database which is continuously updated by the updatedb command. updatedb is run automatically by a cron scheduler (e.g. once a day) on many UNIX operating systems - so you won't have to run it yourself.

apropos

The apropos tool will list all the commands relating to a particularly topic. Try it with something like
apropos mail
or
apropos login.

find

This is one of the most useful command-line tools and has far too many options to mention here. The following examples show some basic usage however.

find -iname '*.mp3'
find -iname '*.mp3' -a -size +10M
find -iname '*.bak' -exec rm {} \;

whereis

The whereis command will tell you the exact location on the file system of a particular command.

whereis perl

route

The route command enables you to examine and change your computer's routing tables.

To view the routing table, type
route -n.
To set the default gateway, try something like
route add default gw 192.168.1.254
(replacing the IP address as appropriate).

netstat

Show the status of network connections with netstat. E.g.,
netstat --tcp -np
will list the IP addresses and ports of active TCP connections. The -n switch disables attempts to find the hostname corresponding to the IP address (which makes it potentially much faster), while the -p switch lists the ID of the relevant process.

The -l switch will show network processes which are in the listening state (i.e. not active).
netstat --tcp -lnp

ipconfig

ipconfig eth0
ipconfig eth0 192.168.1.1

xargs

xargs is a peculiar but useful command which is perhaps best explained by example. An example can be seen in one of the locate commands above.

ssh / scp

The general format of the ssh command is
ssh -l <user> <remote host>
or
ssh -l <user> <remote host> <command>,
where <command> is the command to run on the remote host.
Replacing the placeholders with actual values, one might have a command such as the following:
ssh -l david myhost.net mailstat $HOME/Maildir/log,
which will login to the host myhost.net, check for mail, and log straight back out again.

The scp command has the following syntax:
scp -pr <local file or directory> <remote host>:<remote_dir>
or
scp -pr <remote host>:<remote_dir> <local file or directory>.

rsync

rsync is a powerful backup and synchronization tool.

Run rsync with the -v and -n options to make sure everything is ok.
Specify a file-size limit with `--max-size` if you wish.
rsync -avn -e ssh --max-size 500k <source> <destination>

(See my separate article on rsync for more information.)

convert

convert is an extremely useful command-line tool for quick formatting of images. It's part of the ImageMagick package.

convert -resize 35% large_picture.jpg small_version.jpg
convert -rotate 90 picture.jpg picture_rotated.jpg

If you would prefer to modify the existing file rather than create a new one, use the mogrify command instead.
mogrify -resize 35% picture.jpg
Be warned that the mogrify command will irreversibly change your file however - so be sure your command is correct before running it.

sed

sed is quite a complex tool which merits an entire manual in itself. You can perform some very useful tasks with it without being an expert however.

A very useful option is the -n switch, which suppresses the printing of lines. E.g., to print only the 5th line in a file, try
cat file.txt | sed -n 5p.
This is a useless use of cat however. A more efficient way of accomplishing the same thing is
sed -n 5p file.txt.

last

This command lets you view the last users logged into the system.
last | head -n 20

grep

grep -i 'search term' *
egrep 'search term 1|search term 2' *

To list all lines except those containing a particular search term, try
egrep -v 'exclude term' *

who

The who command lets you see who is logged on to a host. Simply type
who
to run it.

wc

The wc (word count) command is useful for counting the number of characters, words, or lines in a file. Note that it only works with plain text files however. There is no point in using it with, e.g., a MS Word or Open Office document.

To print the number of lines in a document, use
wc -l document.txt;
for the number of words use
wc -w document.txt,
and for the number of characters, try
wc -c document.txt.

df

Report disk space usage with df. You might find it easier to use with the -h option (which sets for a 'human-readable' format).
df -h

du

To print the amount of disk space used by a particular directory, try
du -hs.
For more info on the switches used, try man du.

ls

ls is used to list the contents of a folder.

For example,
ls -hs *
will list and print the size of all files in a directory. The -h switch requests a 'human readable' format in the size information.

The command
ls -lt
will list print a detailed directory contents with one item per line, sorted by modification time. The -l switch turns on verbose output.

top

The top command is similar to the Task Manger on Windows operating systems. It lists processes currently running on the system, along with their CPU and memory usage, etc..

It is in fact an interactive command, so it will take over your console screen when you run it.
top
See man top for how to use it in interactive mode.

Tags: