Here I will show you some useful commands that will make your life a lot easier, some of them are specific to Arch Linux while the majority will run on any distro.

# = means execute as root or with sudo

 

Get a list of installed packages

# comm -23 <(pacman -Qeq|sort) <(pacman -Qmq|sort) > pkglist.txt

This gets all explicitly installed top-level packages, i.e. those that are not required by other packages, and sorts them from a to z and then outputs the result to a file.

Store this package list somewhere safe and handy (you can also set up a cron job so you always have the latest version of the file), and when you need it, just copy the pkglist.txt file to the new installation, and navigate to the directory containing it.

This command comes extremely handy when you need or want to reinstall Arch Linux, because pacman can take a list of packages from a text file and use that as input, so by doing:

# pacman -S $(< pkglist.txt)

you can tell pacman to install everything you had before formatting, or to replicate the package list of another machine saving you hours (if not days).

Command Line shortcuts (distro agnostic)

This shortcuts are totally awesome and easy to learn, the web is filled with pages explaining how to use them, but for reference, here is a quick explanation:

ctrl + u = cuts a command from the cursor position until de start of the line

ctrl + y = recovers a command previously cut with ctrl + u

ctrl + r = search a command you have typed before (very useful for long one-liner commands that are hard to remember). If the first command returned is not the one you are looking for, press ctrl + r again and it will get to the next result. To execute the command one you have found it, just press enter.

Wipe a hard drive or USB/Flash media (including the MBR)

  • Filling the disk with all zeros (This may take a while, as it is making every bit of data 0) :
    # dd if=/dev/zero of=/dev/sdX bs=1M
  • If you are wiping your hard drive for security, you can populate it with random data rather than zeros (This is going to take even longer than the first example.) :
    # dd if=/dev/urandom of=/dev/sdX bs=1M

Replace sdX with the drive you want to wipe, you can check with a tool such as Gparted so you know the exact location of the drive (it can be different from sdX in some circunstances).

Incremental backup using rsync

You don’t need to have expensive or bloated tools to have a nice backup of your system, this command:

# rsync --delete --backup --backup-dir=/media/usborexternaldrive/backup-`date +%A` -avz /home/ /media/usborexternaldrive/current-backup


Will backup all your users home directories in an incremental way, with the latest backup (current-backup) being a complete file snapshot, while the others contain just the differences between the current-backup folder and the day of the week you performed the backup.

You can also mount a network folder or give it your dropbox directory so backups will land there. At the end of the week you will have one backup for each day of the week + a full backup without wasting any hard drive space, very neat!

Download an entire website

wget –random-wait -r -p -e robots=off -U mozilla http://www.example.com

-p parameter tells wget to include all files, including images.

-e robots=off you don’t want wget to obey by the robots.txt file

-U mozilla as your browsers identity.

–random-wait to let wget chose a random number of seconds to wait, avoid get into black list.

Other Useful wget Parameters:

–limit-rate=20k limits the rate at which it downloads files.

-b continues wget after logging out.

-o $HOME/wget_log.txt logs the output

Remove spaces from all file names in a folder

for file in *; do mv "$file" `echo $file | sed -e 's/  */_/g' -e 's/_-_/-/g'`; done

Windows users like to put spaces in filenames (which is a pain on the linux console because
you have to "escape" every space), this command will replace all spaces with dashes and
underscores, saving you lots of time!