Linux
Convert Videos to MP4 Playable on iTunes/iPhone/iPad
ffmpeg -i original-file.avi -r 30 -vcodec libx264 -threads 0 -ar 48000 -ab 128k -ac 2 -y -crf 21 converted_file.mp4
Split A Big File Into Multiple TAR Files
So you have a big archive or big file that you can’t fit on a single storage (are there still such things that does not fit onto a USB these days?
). How do you split them up on Linux?
tar czvfp - /your/one/big/file-or-archive | split -d -b 102400000 - multifile-prefix
The result would be files like multifile-prefix00 .. multifile-prefixNN.
You can then put them back together with:
cat multifile-prefix* | tar xzvfp -
Nevertheless, go buy yourself bigger USB drives!
Process Monitor and Email Alert In One Line
ps aux|grep PROCESSNAME > /dev/null; if [ $? -ne 0 ]; then echo "Process is not running!" | mail -s "ALERT: Process is not running" alert@yourdomain.com; fi
Substitute PROCESSNAME with the name of the daemon or script you want to monitor. This is good if you have a long running script that you want to be alerted of when it finishes (you can do more within the then clause of the if construct). It is best run from cron, perhaps every 5 mins, something like:
*/5 * * * * ps aux|grep PROCESSNAME > /dev/null; if [ $? -ne 0 ]; then echo "Process is not running!" | mail -s "ALERT: Process is not running" alert@yourdomain.com; fi
Copy and Uncompress (ssh, gunzip, tar) from Remote Server
So you have a remote backup and you want to quickly have it restored locally or to another server without transferring, then uncompressing as 2 or 3 steps.
ssh user@remote-server "cat /path/to/compressed/backup.tar.gz" | tar xzvf -
Find Out Which Repository an RPM is Installed From
You might be wondering which an rpm package came from. When yum info
will list “installed” instead of which repository, well here is a one liner that can help:
rpm -q gpg-pubkey --provides | grep `rpm -qi
Here is a sample:
[root@vm-linux-01 ~]# rpm -q gpg-pubkey --provides | grep `rpm -qi mysql-server|grep Signature|awk '{print $(NF)}'|tail -1`
gpg(Atomic Rocket Turtle
gpg(5ebd2744) = 4:32a951145ebd2744-418ffac9
So we know that mysql-server came from the Atomic repo
Copy Large Files Over the Network with Netcat + tar + Qpress compression
On the target host, change directory to the folder you want to copy the files to:
nc -l 9999 | qpress -vdio | tar xvf -
On the source host, change directory to the folder containing the files to be copied:
tar -cf - . | qpress -vio - . | nc [TARGET_HOST_IP] 9999
Convert Videos to AVI Using mencoder 3-pass Method
Have you downloaded a video file you can’t play or you want to share but others will not be able to play? Convert them to AVI:
for i in 1 3 3; do mencoder /path/to/original-video -forceidx -ovc lavc -lavcopts vcodec=mpeg4:vpass=$i -lameopts ratio=75 -oac mp3lame -o /path/to/desired-output.avi; done
List Distinct IPs on Your Apache Access Log
If you ever need to look at the IPs on your access_log at some point here is a quick reference.
cat access_log|awk '{print $1}'|sort|uniq -c
This will list all IP, sort them ascending and count their occurrence and prepend that count for each IP found.
Find Large Files using `find` Command
Oftentimes you will be maintaining disk space on your servers or nearing your account’s disk space quota. Quickly you want to know which files are taking up your disk space, here’s a quick rundown to find files greater than 10MB.
find . -type f -size +10240k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
This will find files from your current working directory and list them alongside their corresponding sizes.
List and Count HTTP Connections
If you are investigating how many connections your HTTP server is getting probably if you suspect a Denial of Service attack or just plain interested, here is a simple command.
netstat -plant|grep :80|awk '{print $5}'|cut -d: -f1|sort|uniq -c|sort -n
It can be interchanged with any other services i.e. SMTP, just change :80 to :25.