Some useful Linux commands

Linux

Rename files / folders with spaces:

IFS=$'\n';for f in `find .`; do file=$(echo $f | tr [:blank:] '_'); [ -e $f ] && [ ! -e $file ] && mv "$f" $file;done;unset IFS

Rsync:

rsync -r -a -v rsync://ns2/storage/www-srv /storage/

^^^ from ns2 to new host – (run on new host)
Rsync (local to remote)

rsync -r -a -v /mnt/usb/data2 rsync://nas/data

Find all zip files (*.zip) and copy to ‘zips’ folder:

find . -iname "*.zip" -exec cp {} /home/zips \;

Run CRON job every 5 mins:

*/5 * * * * /usr/local/bin/command.sh > /dev/null 2>&1

The ”/dev/null 2>&1” option mutes any output.

Awk – print last word of line.

awk '{ print $NF }'

Delete lines containing text using awk

sed '/pattern to match/d' ./infile

To create a new file (rather than print to the console) you need to redirect the output either to a new file with something like sed ‘/pattern to match/d’ ./infile > ./newfile or if you want to do an in-place edit then you can add the -i flag to sed as in sed -i ‘/pattern to match/d’ ./infile

Do the same with grep

grep -v "pattern" file > temp && mv temp file

With awk

awk '!/pattern/' file > temp && mv temp file

And inverse sed (faster)

sed -n '/pattern/!p' file
sed -n 3p filename

Tune reserved blocks
List details of filesystem.

tune2fs -l /dev/sda1

Change reserved allocation to 0%

sudo tune2fs -m 0 /dev/sda1