shell

Collection of helpful shell commands.

File system

# mount a samba share
mount -t cifs //192.168.0.42/share remote

User manipulation

# interactively add a user
adduser fred

# change user's password
passwd fred

# add user to group wheel
usermod -aG wheel fred

# remove user
deluser fred

# create a group
groupadd wheel

Archives

# create a gzipped tar file with intermediary file
tar cvf foo.tar foo1.txt foo2.txt
gzip -9 foo.tar

# create a gzipped tar file without intermediary file
tar cvf - foo1.txt foo2.txt | gzip -9 > foo.tar.gz

# GNU tar command
tar czf foo.tar.gz foo1.txt foo2.txt

# create a bzipped tar file (long version)
tar cf tarfile.tar.bz2 --use-compress-program=bzip2 foo1.txt foo2.txt

# create a bzipped tar file (newer versions of GNU tar)
tar cjf foo.tar.bz2 foo1.txt foo2.txt

Awk, Sed, Grep & scripting

# join each line with the next line
awk '{if (NR % 2) {prev=$0} else {print prev, $0}}'

# print third column in file
cat mystuff.txt | awk '{print $3}'

# remove first line and all whitespace from file `question1_file`:
cat question1_file | awk '{printf "%s", (NR==1 ? "" : $0)}' | tr -d '[:space:]'

# find emails in a file and write them to another file separated with commas:
cat text_with_emails.txt | grep -o '[^ ,]\+@[^ ,]\+\.[a-zA-Z]\+' | awk '{printf "%s"(NR==1 ? "" : ", "), $0}' > emails.csv

# remove unversioned files from svn tree (akin to bzr clean-tree)
svn status --no-ignore | grep '^\?' | sed -r 's/^\?\s+//' | xargs rm -rf

# kill moin fcgi processes (find them in ps listing, then kill)
ps aux | grep moin | awk '{print $2}' | sudo xargs kill

# sort by third column in file
sort -k3 mystuff.txt

# redirect both stderr and stdout to /dev/null
foobar > /dev/null 2>&1

# print only non-blank lines:
cat foobar.txt | grep -v '^\s*$'

Find files

Find php files in current directory ignoring SVN dirs:

$ find . -path '*.svn' -prune -o -path '*.php' -print

or

$ find . -name '.svn' -prune -o -name '*.php' -print

Find all files except SVN dirs:

$ find . -name '.svn' -prune -o -print

Apply chmod 644 to all files (not directories) in /usr/share/man:

$ find /usr/share/man -type f | xargs chmod 644

Search for __init__ only in python files:

$ find . -name "*.py" -exec grep '__init__' '{}' ; -print

Locate file named exactly foobar (not foobar)

$ locate -b '\foobar'

Remote interaction

Copy a directory to remote server via rsync through SSH

$ rsync -avz -e ssh localdir user@192.0.32.10:remotedir

Recursively download an FTP directory from a remote machine

# --continue, -c continue getting partially-downloaded file
# --no-clobber, -nc if file exists on disk, do not download it
# to avoid specifying password on the command line, put it in .wgetrc or .netrc
wget -r --no-clobber --continue --ftp-user=fredfoobar --ftp-password=mypass123 --level=100 ftp://ftp.example.com/remotedir

Do a DNS zone transfer (list all DNS records for a server)

$ dig @dns.example.com example.com axfr

# copy a public key to a remote machine
cat ~/.ssh/id_dsa.pub | ssh user@remote.machine.com 'cat >> .ssh/authorized_keys'

Make a post request to a webpage:

# curl -v -F "x=y" https://30daysofdirt.dialformen.com/enter

Code

Create a patch file

$ diff -Naur olddir newdir > new-patch

Apply patch

$ patch -p0 < new-patch

Compilation

Include debugging information:

$ gcc -g hello.c

Compile programs that use OpenGL

$ gcc -lGL -lGLU -lglut -o hello hello.c

Network configuration

Useful utilities

Command Description
lshw detailed information on hardware configuration
lspci list PCI devices
lsusb list USB devices
lsmod show status of modules in the Linux Kernel
modprobe add or remove modules from the Kernel
iwconfig configure a wireless network interface
ifconfig configure a network interface
iwlist get detailed wireless information from a wireless interface
dhclient DHCP client