www.LinuxHowtos.org

edit this article
This is a list of some handy/common things you can do on the command line in Linux.

Commands

lynx -dump -width=999 http://www.linuxhowtos.org/Misc/commandline.htm >cmd.txt
Make a text copy of this page
apropos word
show commands pertinent to word
which command
show full path name of command
time command
see how long a command takes
nice command
run a low priority command
renice 19 -p $$
make shell (script) low priority. Useful for non interactive tasks
look prefix
quickly search dictionary
grep --color word /usr/share/dict/words
highlight occurances of words in dictionary
gpg -c file
encrypt file
gpg file.gpg
decrypt file
echo 'wget url' | at 01:00
download url at 1AM to current dir
echo "mail -s 'get the train' you@yourdomain < /dev/null" | at 17:45
email reminder
watch -n1 'cat /proc/interrupts'
watch changeable data continuously
alias hd='od -Ax -tx1z -v'
handy hexdump. (usage e.g.: hd /proc/self/cmdline | less)

disc space

ls /usr/bin | pr -T9 -W$COLUMNS
print in 9 columns to width of terminal
touch -c -t 0304050607 file
set file timestamp (YYMMDDhhmm)
pstree -hlp
show process hierarchy
lsof /dir/file
show process using file
ls -lSr
show files, biggest last
du -sh file dir
show disk usage of file and dir. See also dutop
df -h
free disk space
df -i
free inodes
fdisk -l
show disks partitions sizes (run as root)
rpm -q -a --queryformat '%10{SIZE} %{NAME}' | sort -k1,1n
list all packages by size

Dir navigation

cd -
go to previous directory
cd ~
go to home directory
(cd dir && command)
go to dir, execute command and return to current dir
pushd .
put current dir on stack so you can popd back to it

CDs

gzip < /dev/cdrom > cdrom.iso.gz
save copy of data cdrom
mkisofs -r dir | gzip > cdrom.iso.gz
create cdrom image from directory
mount -oloop cdrom.iso /mnt/dir
mount the cdrom image at /mnt/dir (for browsing)
gzip -dc cdrom.iso.gz | cdrecord dev=/dev/cdrom -
burn cdrom image (use dev=ATAPI -scanbus to confirm dev)
cdparanoia -B
rip audio tracks from CD to wav files in current dir
cdrecord dev=/dev/cdrom -audio *.wav
make audio CD from all wavs in current dir
oggenc --tracknum='track' track.cdda.wav -o 'track.ogg'
make ogg file from wav file

archives

tar c dir/ | bzip2 > dir.tar.bz2
make archive of dir/
bzip2 -dc dir.tar.bz2 | tar x
extract archive (use gzip instead of bzip2 for tar.gz files)
tar c dir/ | gzip | gpg -c | ssh user@remote 'dd of=dir.tar.gz.gpg'
make encrypted archive of dir/ on remote machine
find dir/ -name '*.txt' | tar c --files-from=- | bzip2 > dir_png.tar.bz2
make archive of subset of dir/ and below
find dir/ -name '*.txt' | xargs cp --target-directory=dir_png/ --parents
make copy of subset of dir/ and below
( tar c /dir/to/copy ) | ( cd /where/to/ && tar x )
copy (with permissions) copy/ dir to /where/to/ dir
( cd /dir/to/copy && tar c . ) | ( cd /where/to/ && tar x )
copy (with permissions) contents of copy/ dir to /where/to/ dir
( tar c /dir/to/copy ) | ssh -C user@remote 'cd /where/to/ && tar x' 
copy (with permissions) copy/ dir to remote:/where/to/ dir
dd bs=1M if=/dev/hda | gzip | ssh user@remote 'dd of=hda.gz'
backup harddisk to remote machine

rsync (Use the --dry-run option for testing)

rsync -P rsync://rsync.server.com/path/to/file file
Only get diffs. Do multiple times for troublesome downloads
rsync --bwlimit=1000 fromfile tofile
Locally copy with rate limit. It's like nice for I/O
rsync -az -e ssh --delete ~/public_html/ remote.com:'~/public_html'
Mirror web site (using compressed rsync over encrypted ssh)
rsync -auz -e ssh remote:/dir/ . && rsync -auz -e ssh . remote:/dir/
Synchronize current directory with remote one

file searching

alias l='ls -l --color=auto'
quick dir listing
ls -lrt
list files by date. See also newest
find -name '*.[ch]' | xargs grep -E 'search string'
search *.c and *.h for 'search string' in this dir and below
find -type f -print0 | xargs -r0 grep -E 'search string'
search all regular files for 'search string' in this dir and below
find -type f -maxdepth 1 | xargs grep -E 'search string'
search all regular files for 'search string' in this dir
find -type f ! -perm -444
find files not readable by all (useful for web site)
find -type d ! -perm -111
find dirs not accessible by all (useful for web site)
locate -r 'file[^/]*.txt'
search cached index for names. This re is like glob *file*.txt

Networking

(Note ifconfig, route, mii-tool, nslookup commands are obsolete)
ip link show
list interfaces
ethtool interface
list interface status
ip link set dev eth0 name wan
rename eth0 to wan
ip addr add 1.2.3.4/24 brd + dev eth0
add ip and mask(255.255.255.0)
ip link set dev interface up
bring interface up (or down)
ip route add default via 1.2.3.254
set default gateway to 1.2.3.254
host google.com
lookup ip address for name or vice versa
netstat -lp --inet
list internet services on a system
netstat -p --inet
list active connections to/from system
echo 'message' | smbclient -M netbiosname
Send popup to windows machine (off by default as of XP sp2)

Math

echo '(1 + sqrt(5))/2' | bc -l
quick math
echo 'obase=16; ibase=10; 123' | bc
base conversion (decimal to hex)
echo 'pad=20; min=64; (100*10^6)/((pad+min)*8)' | bc
More complex (integer) e.g. This shows max FastE packet rate
echo 'pad=20; min=64; print (100E6)/((pad+min)*8)' | python
python handles scientific notation
echo 'pad=20; plot [64:1518] (100*10**6)/((pad+x)*8)' | gnuplot -persist
Plot FastE packet rate vs packet size

text manipulation

(note these use stdin and stdout, so if you want to edit files, append < oldfile > newfile)
sed 's/string1/string2/g'
replace string1 with string2
sed '/ *#/d; /^ *$/d'
remove comments and blank lines
sed ':a; /\$/N; s/\//; ta'
concatenate lines with trailing
sed 's/[ ]*$//'
remove trailing spaces from lines
sed 's/([\`\"$\\])/\1/g'
backslashify shell metacharacters active within double-quotes
sort file1 file1 file2 | uniq -u
show lines in file2 not in file1
echo 'Test' | tr '[:lower:]' '[:upper:]'
case conversion
tr -dc '[:print:]' < /dev/urandom
filter non printable characters
grep 'processor' /proc/cpuinfo | wc -l
count lines

Calendar

cal -3
display a calendar
date --date='25 Dec' +%A
what day does xmas fall on, this year
date --date '1970-01-01 UTC 1097781420 seconds'
convert number of seconds since the epoch to a date
TZ=':America/Los_Angeles' date
What time is it on West coast of US (use tzselect to find TZ)

locales

printf "%'d" 1234
print number with thousands grouping appropriate to locale
echo "I live in `locale territory`"
extract info from locale database
locale | cut -d= -f1 | xargs locale -kc | less
list fields in locale database
LANG=ga_IE locale int_prefix
lookup locale info for specific country. See also ccodes

recode

(Obsoletes iconv, dos2unix, unix2dos)
recode -l | less
show available conversions (aliases on each line)
recode windows-1252.. file_to_change.txt
windows "ansi" to local charset (auto does CRLF conversion)
recode utf-8/CRLF.. file_to_change.txt
windows utf8 to local charset
recode iso-8859-15..utf8 file_to_change.txt
latin9 (western europe) to utf8
recode ../b64 < file.txt > file.b64
Base64 encode
recode ..HTML < file.txt > file.html
text to HTML
recode -lf windows-1252
show table of characters
echo -n 0xA4 | recode latin-9/x1..dump
show what the character is in charmap
echo -n 0x20AC | recode ucs-2/x2..1252/x
show windows-1252 encoding
echo -n 0x20AC | recode ucs-2/x2..utf-8/x
show utf-8 encoding

interactive

mc
powerful filemanager that can browse rpm, tar, ftp, ssh, ...
screen
virtual terminals with detach capability, ...
lynx
web browser
gnuplot
interactive/scriptable graphing
octave
matlab like environment

From http://www.iol.ie/~padraiga/cmdline.html
rate this article:
current rating: average rating: 1.2 (65 votes) (1=very good 6=terrible)
Your rating:
Very good (1) Good (2) ok (3) average (4) bad (5) terrible (6)

back