#:1# # commandlinefu.com by David Winterbottom #:2# # commandlinefu.com by David Winterbottom #:3# # commandlinefu.com by David Winterbottom # Randomize lines (opposite of | sort) perl -wl -e '@f=<>; for $i (0 .. $#f) { $r=int rand ($i+1); @f[$i, $r]=@f[$r,$i] if ($i!=$r); } chomp @f; print join $/, @f;' try.txt # Randomize lines (opposite of | sort) random -f # Randomize lines (opposite of | sort) cat ~/SortedFile.txt | perl -wnl -e '@f=<>; END{ foreach $i (reverse 0 .. $#f) { $r=int rand ($i+1); @f[$i, $r]=@f[$r,$i] unless ($i==$r); } chomp @f; foreach $line (@f){ print $line; }}' # Output files without comments or empty lines function catv { egrep -v "^$|^#" ${*} ; } # Replace all tabs with spaces in an application grep -PL "\t" -r . | grep -v ".svn" | xargs sed -i 's/\t/ /g' # wrap long lines of a text fold -s -w 90 file.txt # Display a File with Line Number nl filename | more # Load another file in vim :split # Pipe STDOUT to vim tail -1000 /some/file | vim - # Add a line to a file using sudo echo "foo bar" | sudo tee -a /path/to/some/file # Output files without comments or empty lines grep -v "^\($\|#\)" # See which files differ in a diff diff dir1 dir2 | diffstat # Delete line number 10 from file sed -i '10d' # Efficiently print a line deep in a huge log file sed '1000000!d;q' < massive-log-file.log #:4# # commandlinefu.com by David Winterbottom # Sort all running processes by their memory & CPU usage ps aux --sort=%mem,%cpu # View Processeses like a fu, fu pstree -p # View Processeses like a fu, fu command ps -Hacl -F S -A f # Will email user@example.com when all Rsync processes have finished. $(while [ ! -z "$(pgrep rsync)" ]; do echo; done; echo "rsync done" | mailx user @example.com) > /dev/null & # Check processes runed not by you ps aux | grep -v `whoami` # Manually Pause/Unpause Firefox Process with POSIX-Signals killall -STOP -m firefox # View process statistics in realtime top # Find out current working directory of a process echo COMMAND | xargs -ixxx ps -C xxx -o pid= | xargs -ixxx ls -l /proc/xxx/cwd # Click on a GUI window and show its process ID and command used to run the proc ess xprop | awk '/PID/ {print $3}' | xargs ps h -o pid,cmd #:5# # commandlinefu.com by David Winterbottom # Simple read and write test with Iozone iozone -s 2g -r 64 -i 0 -i 1 -t 1 # locate a filename, make sure it exists and display it with full details locate -e somefile | xargs ls -l # currently mounted filesystems in nice layout column -t /proc/mounts # Which files/dirs waste my disk space du -aB1m|awk '$1 >= 100' # Check if filesystem hangs ls /mnt/badfs & # Migrate existing Ext3 filesystems to Ext4 tune2fs -O extents,uninit_bg,dir_index /dev/yourpartition # create an emergency swapfile when the existing swap space is getting tight sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024000;sudo mkswap /swapfile; s udo swapon /swapfile # List files recursively sorted by modified time find /home/fizz -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort # automount samba shares as devices in /mnt/ sudo vi /etc/fstab; Go//smb-share/gino /mnt/place smbfs defaults,username=gino,p assword=pass 0 0:wq; mount //smb-share/gino # Find all directories on filesystem containing more than 99MB du -hS / | perl -ne '(m/\d{3,}M\s+\S/ || m/G\s+\S/) && print' # currently mounted filesystems in nice layout mount | column -t # Move a file up a directory. mv file_name.extension .. #:6# # commandlinefu.com by David Winterbottom # Simple XML tag extract with sed sed -n 's/.*\([^<]*\)<\/foo>.*/\1/p' # Working random fact generator lynx -dump randomfunfacts.com | grep -A 3 U | sed 1D # Alias to edit and source your .bashrc file alias vb='vim ~/.bashrc; source ~/.bashrc' #:7# # commandlinefu.com by David Winterbottom # Write and run a quick C program cat | gcc -x c -o a.out - && ./a.out && rm a.out # List symbols from a dynamic library (.so file) nm --dynamic # display typedefs, structs, unions and functions provided by a header file cpp /usr/include/stdio.h | grep -v '^#' | grep -v '^$' | less #:8# # commandlinefu.com by David Winterbottom # List symbols from a dynamic library (.so file) nm --dynamic #:9# # commandlinefu.com by David Winterbottom #:10# # commandlinefu.com by David Winterbottom # Easily decode unix-time (funtion) utime(){ perl -e "print localtime($1).\"\n\"";} # convert unixtime to human-readable perl -e 'print scalar(gmtime(1234567890)), "\n"' # convert unixtime to human-readable echo "0t${currentEpoch}=Y" | /usr/bin/adb # Get yesterday's date or a previous time date -d '1 day ago'; date -d '11 hour ago'; date -d '2 hour ago - 3 minute'; dat e -d '16 hour' # decoding Active Directory date format ldapsearch -v -H ldap:// -x -D cn=,cn=,dc=,dc = -w -b ou=,dc=,dc= -s sub sAMAccountNam e=* '*' | perl -pne 's/(\d{11})\d{7}/"DATE-AD(".scalar(localtime($1-11644473600) ).")"/e' # print date 24 hours ago date --date=yesterday #:11# # commandlinefu.com by David Winterbottom # Show Network IP and Subnet ipcalc $(ifconfig eth0 | grep "inet addr:" | cut -d':' -f2,4 | sed 's/.+Bcast:/\ //g') | awk '/Network/ { print $2 } ' # Show Network IP and Subnet IP=`ifconfig eth0 | grep "inet addr:" | ips |cut -d ":" -f 2 | cut -d " " -f 1`; SUBNET=`ifconfig eth0 | grep "inet addr:" | ips |cut -d ":" -f 3 | cut -d " " -f 1`;RANGE=`ipcalc $IP/$SUBNET | grep "Network:" | cut -d ' ' -f 4`;echo $RANGE # Get your external IP address wget -qO - http://www.sputnick-area.net/ip;echo # Get your external IP address wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$// ' # Get your external IP address lynx --dump http://ip.boa.nu|sed -e 's/^[[:space:]]*//' -e 's/*[[:space:]]$//'|g rep -v ^$ # Get your external IP address wget -O - -q ip.boa.nu # Get your external IP address curl whatismyip.org # Get your external IP address with the best commandlinefu.com command eval $(curl -s http://www.commandlinefu.com/commands/matching/external/ZXh0ZXJuY Ww=/sort-by-votes/plaintext|sed -n '/^# Get your external IP address$/{n;p;q}') # Get your external IP address with a random commandlinefu.com command IFS=$'\n';cl=($(curl -s http://www.commandlinefu.com/commands/matching/external/ ZXh0ZXJuYWw=/sort-by-votes/plaintext|sed -n '/^# Get your external IP address$/{ n;p}'));c=${cl[$(( $RANDOM % ${#cl[@]} ))]};eval $c;echo "Command used: $c" # Get your external IP address exec 3<>/dev/tcp/whatismyip.com/80; echo -e "GET /automation/n09230945.asp HTTP/ 1.0\r\nHost: whatismyip.com\r\n" >&3; a=( $(cat <&3) ); echo ${a[${#a[*]}-1]}; # Get your external IP address echo -e "GET /automation/n09230945.asp HTTP/1.0\r\nHost: whatismyip.com\r\n" | n c whatismyip.com 80 | tail -n1 # Get your external IP address curl ip.appspot.com # List Network Tools in Linux apropos network |more # send echo to socket network echo foo | netcat 192.168.1.2 25 # send echo to socket network echo "foo" > /dev/tcp/192.168.1.2/25 # Another way to see the network interfaces ip addr show # Echo the local IP addresses of the machines on your local network for i in 192.168.1.{61..71};do ping -c 1 $i &> /dev/null && echo $i;fi;done # Resolve the "all display buffers are busy, please try later" error on a Foundr y dm display-buffer reset # Get your external IP address fetch -q -o - http://ipchicken.com | egrep -o '([[:digit:]]{1,3}\.){3}[[:digit:] ]{1,3}' # Get your external IP address curl -s 'http://checkip.dyndns.org' | sed 's/.*Current IP Address: \([0-9\.]*\). */\1/g' # Delete all aliases for a network interface on a (Free)BSD system ifconfig | grep "0xffffffff" | awk '{ print $2 }' | xargs -n 1 ifconfig em0 dele te # Get all IPs via ifconfig ifconfig -a | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1' # What is My WAN IP? curl -s checkip.dyndns.org | grep -Eo '[0-9\.]+' # Show established network connections lsof -i | grep -i estab # One-Liner to Display IP Addresses python -c "import socket; print '\n'.join(socket.gethostbyname_ex(socket.gethost name())[2])" #:12# # commandlinefu.com by David Winterbottom # Manage "legacy" service run control links sudo find /etc/rc{1..5}.d -name S99myservice -type l -exec sh -c 'NEWFN=`echo {} | sed 's/S99/K99/'` ; mv -v {} $NEWFN' \; # Run a file system check on your next boot. sudo touch /forcefsck # List all files opened by a particular command lsof -c dhcpd # List files opened by a PID lsof -p 15857 #:13# # commandlinefu.com by David Winterbottom # backup local MySQL database into a folder and removes older then 5 days backup s mysqldump -uUSERNAME -pPASSWORD database | gzip > /path/to/db/files/db-backup-`d ate +%Y-%m-%d`.sql.gz ;find /path/to/db/files/* -mtime +5 -exec rm {} \; # Get a MySQL DB dump from a remote machine ssh user@host "mysqldump -h localhost -u mysqluser -pP@$$W3rD databasename | gzi p -cf" | gunzip -c > database.sql # Transfer sqlite3 data to mysql sqlite3 mydb.sqlite3 '.dump' | grep -vE '^(BEGIN|COMMIT|CREATE|DELETE)|"sqlite_s equence"' | sed -r 's/"([^"]+)"/`\1`/' | tee mydb.sql | mysql -p mydb # Determine configure options used for MySQL binary builds grep CONFIG $(which mysqlbug) # Determine configure options used for MySQL binary builds cat `whereis mysqlbug | awk '{print $2}'` | grep 'CONFIGURE_LINE=' # Explanation of system and MySQL error codes perror NUMBER # positions the mysql slave at a specific master position slave start; SELECT MASTER_POS_WAIT('master.000088','8145654'); slave stop; # raw MySQL output to use in pipes mysql DATABASE -N -s -r -e 'SQL COMMAND' # Passwordless mysql{,dump,admin} via my.cnf file echo -e "[client]\nuser = YOURUSERNAME\npassword = YOURPASSWORD" > ~/.my.cnf # Load multiple sql script in mysql cat schema.sql data.sql test_data.sql | mysql -u user --password=pass dbname # Convert all MySQL tables and fields to UTF8 mysql --database=dbname -B -N -e "SHOW TABLES" | awk '{print "ALTER TABLE", $1, "CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;"}' | mysql --database=d bname & # Monitor server load as well as running MySQL processes watch -n 1 uptime\;myqladmin --user= --password= --verbose proce sslist # Backup a remote database to your local filesystem ssh user@host 'mysqldump dbname | gzip' > /path/to/backups/db-backup-`date +%Y-% m-%d`.sql.gz # Monitor the queries being run by MySQL watch -n 1 mysqladmin --user= --password= processlist #:14# # commandlinefu.com by David Winterbottom # Batch image resize for a in `ls`; do echo $a && convert $a -resize x $a; done # Find corrupted jpeg image files find . -name "*jpg" -exec jpeginfo -c {} \; | grep -E "WARNING|ERROR" # sync a directory of corrupted jpeg with a source directory for i in *jpg; do jpeginfo -c $i | grep -E "WARNING|ERROR" | cut -d " " -f 1 | x args -I '{}' find /mnt/sourcerep -name {} -type f -print0 | xargs -0 -I '{}' cp -f {} ./ ; done # Add a shadow to picture convert {$file_in} \( +clone -background black -shadow 60x5+10+10 \) +swap -back ground none -layers merge +repage {$file_out} # Scale,Rotate, brightness, contrast,...with Image Magick convert -rotate $rotate -scale $Widthx$Height -modulate $brightness -contrast $c ontrast -colorize $red%,$green%,$blue% $filter file_in.png file_out.png # Use ImageMagick to get an image's properties identify -ping imageName.png # Determine an image's dimensions identify -format "%wx%h" /path/to/image.jpg #:15# # commandlinefu.com by David Winterbottom # Display summary of git commit ids and messages for a given branch git log --pretty='format:%Cgreen%H %Cred%ai %Creset- %s' # Display summary of git commit ids and messages for a given branch git log master | awk '/commit/ {id=$2} /\s+\w+/ {print id, $0}' #:16# # commandlinefu.com by David Winterbottom # Display all readline binding that use CTRL bind -p | grep -F "\C" #:17# # commandlinefu.com by David Winterbottom # DVD to YouTube ready watermarked MPEG-4 AVI file using mencoder (step 2) mencoder -sub heading.ssa -subpos 0 -subfont-text-scale 4 -utf8 -oac copy -ovc l avc -lavcopts vcodec=mpeg4 -vf scale=320:-2,expand=:240:::1 -ffourcc xvid -o out put.avi dvd.avi # DVD to YouTube ready watermarked MPEG-4 AVI file using mencoder (step 1) mencoder -oac mp3lame -lameopts cbr=128 -ovc lavc -lavcopts vcodec=mjpeg -o dvd. avi dvd://0 # Convert a MOV captured from a digital camera to a smaller AVI ffmpeg -i input.mov -b 4096k -vcodec msmpeg4v2 -acodec pcm_u8 output.avi # Rip DVD to YouTube ready MPEG-4 AVI file using mencoder mencoder -oac mp3lame -lameopts cbr=128 -ovc lavc -lavcopts vcodec=mpeg4 -ffourc c xvid -vf scale=320:-2,expand=:240:::1 -o output.avi dvd://0 # Get the total length of all video / audio in the current dir (and below) in H: m:s find -type f -name "*.avi" -print0 | xargs -0 mplayer -vo dummy -ao dummy -iden tify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02 d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1 # Use heading subtitle file as watermark using mencoder mencoder -sub heading.ssa -subpos 0 -subfont-text-scale 4 -utf8 -oac mp3lame -la meopts cbr=128 -ovc lavc -lavcopts vcodec=mjpeg -vf scale=320:-2,expand=:240:::1 -o output.avi input.flv # View webcam output using GStreamer pipeline gst-launch-0.10 autovideosrc ! video/x-raw-yuv,framerate=\(fraction\)30/1,width= 640,height=480 ! ffmpegcolorspace ! autovideosink # Record audio and video from webcam using mencoder mencoder tv:// -tv driver=v4l2:width=800:height=600:device=/dev/video0:fps=30:ou tfmt=yuy2:forceaudio:alsa:adevice=hw.2,0 -ovc lavc -lavcopts vcodec=mpeg4:vbitra te=1800 -ffourcc xvid -oac mp3lame -lameopts cbr=128 -o output.avi # View webcam output using mplayer mplayer tv:// -tv driver=v4l2:width=640:height=480:device=/dev/video0:fps=30:out fmt=yuy2 # Record audio and video from webcam using ffmpeg ffmpeg -f alsa -r 16000 -i hw:2,0 -f video4linux2 -s 800x600 -i /dev/video0 -r 3 0 -f avi -vcodec mpeg4 -vtag xvid -sameq -acodec libmp3lame -ab 96k output.avi # Extract audio track from a video file using mencoder mencoder -of rawaudio -ovc copy -oac mp3lame -o output.mp3 input.avi # Substitute audio track of video file using mencoder mencoder -ovc copy -audiofile input.mp3 -oac copy input.avi -o output.avi # Remove sound from video file using mencoder mencoder -ovc copy -nosound input.avi -o output.avi # FLV to AVI with subtitles and forcing audio sync using mencoder mencoder -sub subs.ssa -utf8 -subfont-text-scale 4 -oac mp3lame -lameopts cbr=12 8 -ovc lavc -lavcopts vcodec=mpeg4 -ffourcc xvid -o output.avi input.flv # Convert a VMWare screencast into a flv file mencoder -of avi -ovc lavc movie.avi -o movie2.avi; ffmpeg -i movie2.avi -r 12 -b 100 movie.flv # Record a webcam output into a video file. ffmpeg -an -f video4linux -s 320x240 -b 800k -r 15 -i /dev/v4l/video0 -vcodec mp eg4 myvideo.avi # Encode a file to MPEG4 format mencoder video.avi lavc -lavcopts vcodec=mpeg4:vbitrate=800 newvideo.avi # Record camera's output to a avi file mencoder -tv device=/dev/video1 tv:// -ovc copy -o video.avi # Record a screencast and convert it to an mpeg ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/outputFile.mpg # extracting audio and video from a movie ffmpeg -i source_movie.flv -vcodec mpeg2video target_video.m2v -acodec copy targ et_audio.mp3 # removing syncronization problems between audio and video ffmpeg -i source_audio.mp3 -itsoffset 00:00:10.2 -i source_video.m2v target_vide o.flv # Create a video that is supported by youtube ffmpeg -i mymovie.mpg -ar 22050 -acodec libmp3lame -ab 32K -r 25 -s 320x240 -vco dec flv mytarget.flv # Copy the sound content of a video to an mp3 file ffmpeg -i source.flv -vn acodec copy destination.mp3 # Find your graphics chipset lspci |grep VGA # play high-res video files on a slow processor mplayer -framedrop -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all #:18# # commandlinefu.com by David Winterbottom #:19# # commandlinefu.com by David Winterbottom # Update Ping.fm status curl -d api_key="$api_key" -d user_app_key="$user_app_key -d body="$body" -d pos t_method="default" http://api.ping.fm/v1/user.post # Watching Command watch 'cat /proc/loadavg' #:20# # commandlinefu.com by David Winterbottom #:21# # commandlinefu.com by David Winterbottom # FAST and NICE Search and Replace for Strings in Files nice -n19 sh -c 'S=askapache && R=htaccess; find . -type f|xargs -P5 -iFF grep - l -m1 "$S" FF|xargs -P5 -iFF sed -i -e s%${S}%${R}% FF' #:22# # commandlinefu.com by David Winterbottom # Prints per-line contribution per author for a GIT repository git ls-files | while read i; do git blame $i | sed -e 's/^[^(]*(//' -e 's/^\([^[ :digit:]]*\)[[:space:]]\+[[:digit:]].*/\1/'; done | sort | uniq -ic | sort -nr # Prints per-line contribution per author for a GIT repository git ls-files | xargs -n1 -d'\n' -i git-blame {} | perl -n -e '/\s\((.*?)\s[0-9]{ 4}/ && print "$1\n"' | sort -f | uniq -c -w3 | sort -r # Awk: Perform a rolling average on a column of data awk 'BEGIN{size=5} {mod=NR%size; if(NR<=size){count++}else{sum-=array[mod]};sum+ =$1;array[mod]=$1;print sum/count}' file.dat # dstat - a mix of vmstat, iostat, netstat, ps, sar... dstat -ta #:23# # commandlinefu.com by David Winterbottom #:24# # commandlinefu.com by David Winterbottom #:25# # commandlinefu.com by David Winterbottom #:26# # commandlinefu.com by David Winterbottom # DVD to YouTube ready watermarked MPEG-4 AVI file using mencoder (step 1) mencoder -oac mp3lame -lameopts cbr=128 -ovc lavc -lavcopts vcodec=mjpeg -o dvd. avi dvd://0 # Create an audio test CD of sine waves from 1 to 99 Hz (echo CD_DA; for f in {01..99}; do echo "$f Hz">&2; sox -nt cdda -r44100 -c2 $f. cdda synth 30 sine $f; echo TRACK AUDIO; echo FILE \"$f.cdda\" 0; done) > cdrdao .toc && cdrdao write cdrdao.toc && rm ??.cdda cdrdao.toc # Get the total length of all video / audio in the current dir (and below) in H: m:s find -type f -name "*.avi" -print0 | xargs -0 mplayer -vo dummy -ao dummy -iden tify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && ($t +=$1) && printf "%02 d:%02d:%02d\n",$t/3600,$t/60%60,$t%60' | tail -n 1 # Record MP3 audio via ALSA using ffmpeg ffmpeg -f alsa -ac 2 -i hw:1,0 -acodec libmp3lame -ab 96k output.mp3 # Record audio and video from webcam using ffmpeg ffmpeg -f alsa -r 16000 -i hw:2,0 -f video4linux2 -s 800x600 -i /dev/video0 -r 3 0 -f avi -vcodec mpeg4 -vtag xvid -sameq -acodec libmp3lame -ab 96k output.avi # Extract audio from Mythtv recording to Rockbox iPod using ffmpeg ffmpeg -ss 0:58:15 -i DavidLettermanBlackCrowes.mpg -acodec copy DavidLettermanB lackCrowes.ac3 # extracting audio and video from a movie ffmpeg -i source_movie.flv -vcodec mpeg2video target_video.m2v -acodec copy targ et_audio.mp3 # removing syncronization problems between audio and video ffmpeg -i source_audio.mp3 -itsoffset 00:00:10.2 -i source_video.m2v target_vide o.flv # Create a video that is supported by youtube ffmpeg -i mymovie.mpg -ar 22050 -acodec libmp3lame -ab 32K -r 25 -s 320x240 -vco dec flv mytarget.flv # Copy the sound content of a video to an mp3 file ffmpeg -i source.flv -vn acodec copy destination.mp3 # create an mp3 with variable bitrate lame -h -V 6 track9.wav track9.mp3 # Extract track 9 from a CD mplayer -fs cdda://9 -ao pcm:file=track9.wav # Record live sound in Vorbis (eg for bootlegs or to take audio notes) rec -c 2 -r 44100 -s -t wav - | oggenc -q 5 --raw --raw-chan=2 --raw-rate=44100 --raw-bits=16 - > MyLiveRecording.ogg # covert m4a audio files to wav find . -name '*.m4a' | xargs -I audiofile mplayer -ao pcm "audiofile" -ao pcm:fi le="audiofile.wav" # Split lossless audio (ape, flac, wav, wv) by cue file cuebreakpoints | shnsplit -o