Ubuntu One Liners

From richud.com
Jump to: navigation, search

Copy hard drive contents

sudo rsync -avxHAXW --progress --preallocate /media/xxxx/source_drive_mounted_name/ /media/xxxx/target_drive_mounted_name/
  • Note the target filesystem must support preallocate (ext4 etc), if this isn't used you will end up with a horribly fragged filesystem!

Check sizes are same afterwards

  • Remember du shows usage not filesize so trying to check with that afterwards wont work.
ls -Rl | awk '{ TOTAL += $5} END { print TOTAL/1024/1024 }'

Remove old kernels

For example to remove 3.8.0-15 to 3.8.0-32 and leave 3.8.0-33

sudo apt-get purge linux-{headers,image,image-extra}-3.8.0-[1-3][1-2].*

ddrescue (gddrescue)

Assuming rescuing drive /dev/sdc

logfile will be local to where you run command from (without giving a path), needed in case anything goes wrong to be able to resume, among other uses.

#enable universe repository first
sudo add-apt-repository universe
sudo apt-get update
sudo apt-get install gddrescue
#rescue to a raw file
sudo ddrescue /dev/sdc /media/somewhere/youroutputfile.raw logfile
#direct to another disk, must be >= in size
sudo ddrescue -f /dev/sdc /dev/sdd logfile

multi-core tar gzip

This would run all as user or all as root if via sudo

tar c --use-compress-program=pigz -f /output/stuff.tar.gz /source/folders

If need root for source access but current user for destination (i.e. backup a drive contents to a network shared via gvfs).

[i.e. This runs tar as root to read the source but write permissions is via user via pigz]

sudo tar cf - /source/folders | pigz > /output/stuff.tar.gz

tar without full pathnames

Say you have lots of files in two folders...this will stick them into one archive with no paths. Bash firstly expands the dir listing from ls which tar takes as input.

tar cf /tmp/output.tar -C /tmp/x/y/z $(ls /tmp/x/y/z) -C /tmp/a/b/c $(ls /tmp/a/b/c)

tar with xz and large dictionary trick

Squishing down some almost identical files by using a dictionary somewhat more than double the size of one, which will give archive the size fractionally larger than just one.

tar cf - -C images $(ls images) -C ${additions} readme.txt | xz -0 --lzma2=dict=64MiB > images/DGND3x00_${build}.tar.xz

disconnect hdd / spin a drive down

Sping down primary drive /dev/sda

echo 1 | sudo tee /sys/block/sda/device/delete

email syslog to yourself as attachment

Using sendmail and base64 applets from busybox....

echo -e "Subject: messages\nMIME-Version: 1.0\nContent-Type: multipart/mixed; boundary=frontier\n\n--frontier\nContent-Type: application/octet-stream\nContent-Transfer-Encoding: Base64\nContent-Disposition: inline;\n\n$(base64 /var/log/messages)\n\n--frontier--\n" | sendmail -f null@yourdomain.com -S smtp.yourdomain.com you@yourdomain.com