Appending strings in bash without the line feed.
To append to a stream in bash without the trailing line feed use printf to format the output before appending it to the stream.
1 i=42 2 FILE=/tmp/atm 3 printf "%s" "${i}" > $FILE
How to install sar on Debian
First install the sysstat package which includes sar:
1 apt-get install sysstat
Then run crontab -e and add the following to collect stats:
1 # Collect measurements at 10-minute intervals 2 0,10,20,30,40,50 * * * * /usr/lib/sa/sa1 3 # Create daily reports and purge old files 4 0 0 * * * /usr/lib/sa/sa2 -A
You now have access to IO and CPU usage history.
Work around for "bash: /bin/rm: Argument list too long"
Use find instead of rm:
1 find . -name '*' | xargs rm
The command deletes all files in the current directory.
Bulk resizing images with Imagemagick.
Quick and dirty, but handy one-liner that resizes all *.jpg files in the current directory.
1 for image in *.jpg; do convert $image -resize 800x600 800x600-$image; done
Bulk content download from sites that serve it through a database using attachment id's
This needs to be customized according to site. The—content-disposition is experimental. It also may cause files by the same name to be downloaded. Wget will name them like abc.jpg, abc.jpg.1, abc.jpg.2 and so on. You can always rename them afterwards :)
1 #!/bin/bash 2 destination_dir=$HOME/thecontent 3 mkdir -p $destination_dir 4 for page in 0 1 2 3 4 5 6 7 8 9; do 5 attachment_ids=$(wget -O - "http://www.xyz.com/showthread.php?page=77${page}" |grep attachmentid|cut -d'"' -f2| cut -d'=' -f3 |cut -d'&' -f1) 6 for attachment_id in $attachment_ids; do 7 wget --content-disposition --directory-prefix=$destination_dir http://www.xyz.com/attachment.php?attachmentid=${attachment_id} 8 done 9 done