# commandlinefu.com by David Winterbottom # Run the last command as root sudo !! # Serve current directory tree at http://$HOSTNAME:8000/ python -m SimpleHTTPServer # change to the previous working directory cd - # Runs previous command but replacing ^foo^bar # mtr, better than traceroute and ping combined mtr google.com # Rapidly invoke an editor to write a long, complex, or tricky command ctrl-x e # Execute a command without saving it in the history command # Empty a file > file.txt # Salvage a borked terminal reset # Capture video of a linux desktop ffmpeg -f x11grab -s wxga -r 25 -i :0.0 -sameq /tmp/out.mpg # Place the argument of the most recent command on the shell 'ALT+.' or ' .' # currently mounted filesystems in nice layout mount | column -t # Query Wikipedia via console over DNS dig +short txt .wp.dg.cx # Execute a command at a given time echo "ls -l" | at midnight # start a tunnel from some machine's port 80 to your local post 2001 ssh -N -L2001:localhost:80 somemachine # Lists all listening ports together with the PID of the associated process netstat -tlnp # Quick access to the ascii table. man ascii # Runs previous command replacing foo by bar every time that foo appears !!:gs/foo/bar # output your microphone to a remote computer's speaker dd if=/dev/dsp | ssh -c arcfour -C username@host dd of=/dev/dsp # Get your external IP address curl ifconfig.me # Mount a temporary ram partition mount -t tmpfs tmpfs /mnt -o size=1024m # Mount folder/filesystem through SSH sshfs name@server:/path/to/folder /path/to/mount/point # Update twitter via curl curl -u user:pass -d status="Tweeting from the shell" http://twitter.com/statuses/update.xml # Compare a remote file with a local file ssh user@host cat /path/to/remotefile | diff /path/to/localfile - # Download an entire website wget --random-wait -r -p -e robots=off -U mozilla http://www.example.com # commandlinefu.com by David Winterbottom # Clear the terminal screen ctrl-l # Jump to a directory, execute a command and jump back to current dir (cd /tmp && ls) # List the size (in human readable form) of all sub folders from the current location du -h --max-depth=1 # type partial command, kill this command, check something you forgot, yank the command, resume typing. [...] # A very simple and useful stopwatch time read (ctrl-d to stop) # SSH connection through host in the middle ssh -t reachable_host ssh unreachable_host # Shutdown a Windows machine from Linux net rpc shutdown -I ipAddressOfWindowsPC -U username%password # Make 'less' behave like 'tail -f'. less +F somelogfile # Watch Star Wars via telnet telnet towel.blinkenlights.nl # Check your unread Gmail from the command line curl -u username --silent "https://mail.google.com/mail/feed/atom" | perl -ne 'print "\t" if //; print "$2\n" if /<(title|name)>(.*)<\/\1>/;' # Simulate typing echo "You can simulate on-screen typing just like in the movies" | pv -qL 10 # Set audible alarm when an IP address comes online ping -i 60 -a IP_address # Reboot machine when everything is hanging + + - - - - - # Close shell keeping all subprocess running disown -a && exit # List of commands you use most often history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head # Display the top ten running processes - sorted by memory usage ps aux | sort -nk +4 | tail # 32 bits or 64 bits? getconf LONG_BIT # Display a block of text with AWK awk '/start_pattern/,/stop_pattern/' file.txt # Set CDPATH to ease navigation CDPATH=:..:~:~/projects # Push your present working directory to a stack that you can pop later pushd /tmp # Put a console clock in top right corner while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-29));date;tput rc;done & # Watch Network Service Activity in Real-time lsof -i # Backticks are evil echo "The date is: $(date +%D)" # Create a script of the last executed command echo "!!" > foo.sh # diff two unsorted files without creating temporary files diff <(sort file1) <(sort file2) # commandlinefu.com by David Winterbottom # Sharing file through http 80 port nc -v -l 80 < file.ext # escape any command aliases \[command] # Reuse all parameter of the previous command line !* # Easy and fast access to often executed commands that are very long and complex. some_very_long_and_complex_command # label # Show apps that use internet connection at the moment. (Multi-Language) lsof -P -i -n # quickly rename a file mv filename.{old,new} # python smtp server python -m smtpd -n -c DebuggingServer localhost:1025 # Delete all files in a folder that don't match a certain file extension rm !(*.foo|*.bar|*.baz) # Google Translate translate(){ wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=$2|${3:-en}" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/'; } # save command output to image ifconfig | convert label:@- ip.png # Remove duplicate entries in a file without sorting. awk '!x[$0]++' # Extract tarball from internet without local saving wget -qO - "http://www.tarball.com/tarball.gz" | tar zxvf - # Kills a process that is locking a file. fuser -k filename # Matrix Style tr -c "[:digit:]" " " < /dev/urandom | dd cbs=$COLUMNS conv=unblock | GREP_COLOR="1;32" grep --color "[^ ]" # Rip audio from a video file. mplayer -ao pcm -vo null -vc dummy -dumpaudio -dumpfile # Display which distro is installed cat /etc/issue # Find the process you are looking for minus the grepped one ps aux | grep [p]rocess-name # Insert the last command without the last argument (bash) !:- # Inserts the results of an autocompletion in the command line ESC * # Add Password Protection to a file your editing in vim. vim -x # Stream YouTube URL directly to mplayer. i="8uyxVmdaJ-w";mplayer -fs $(curl -s "http://www.youtube.com/get_video_info?&video_id=$i" | echo -e $(sed 's/%/\\x/g;s/.*\(v[0-9]\.lscache.*\)/http:\/\/\1/g') | grep -oP '^[^|,]*') # Copy your SSH public key on a remote machine for passwordless login - the easy way ssh-copy-id username@hostname # A fun thing to do with ram is actually open it up and take a peek. This command will show you all the string (plain text) values in ram sudo dd if=/dev/mem | cat | strings # Define a quick calculator function ? () { echo "$*" | bc -l; } # Show File System Hierarchy man hier # commandlinefu.com by David Winterbottom # Easily search running processes (alias). alias 'ps?'='ps ax | grep ' # Create a CD/DVD ISO image from disk. readom dev=/dev/scd0 f=/path/to/image.iso # Graphical tree of sub-directories ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' # Monitor progress of a command pv access.log | gzip > access.log.gz # Print all the lines between 10 and 20 of a file sed -n '10,20p' # Make directory including intermediate directories mkdir -p a/long/directory/path # Multiple variable assignments from command output in BASH read day month year <<< $(date +'%d %m %y') # intercept stdout/stderr of another process strace -ff -e trace=write -e write=1,2 -p SOME_PID # Job Control ^Z $bg $disown # Show apps that use internet connection at the moment. (Multi-Language) ss -p # Copy a file using pv and watch its progress pv sourcefile > destfile # Send pop-up notifications on Gnome notify-send [""] "<body>" # Edit a file on a remote host using vim vim scp://username@host//path/to/somefile # Create a pdf version of a manpage man -t manpage | ps2pdf - filename.pdf # replace spaces in filenames with underscores rename 'y/ /_/' * # Generate a random password 30 characters long strings /dev/urandom | grep -o '[[:alnum:]]' | head -n 30 | tr -d '\n'; echo # Find Duplicate Files (based on size first, then MD5 hash) find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate # Check your unread Gmail from the command line curl -u username:password --silent "https://mail.google.com/mail/feed/atom" | tr -d '\n' | awk -F '<entry>' '{for (i=2; i<=NF; i++) {print $i}}' | sed -n "s/<title>\(.*\)<\/title.*name>\(.*\)<\/name>.*/\2 - \1/p" # Graph # of connections for each hosts. netstat -an | grep ESTABLISHED | awk '{print $5}' | awk -F: '{print $1}' | sort | uniq -c | awk '{ printf("%s\t%s\t",$2,$1) ; for (i = 0; i < $1; i++) {printf("*")}; print "" }' # Convert seconds to human-readable format date -d@1234567890 # Remove all but one specific file rm -f !(survivior.txt) # return external ip curl icanhazip.com # Display a cool clock on your terminal watch -t -n1 "date +%T|figlet" # Monitor the queries being run by MySQL watch -n 1 mysqladmin --user=<user> --password=<password> processlist # Mount a .iso file in UNIX/Linux mount /path/to/file.iso /mnt/cdrom -oloop # commandlinefu.com by David Winterbottom # Get the 10 biggest files/folders for the current direcotry du -s * | sort -n | tail # Record a screencast and convert it to an mpeg ffmpeg -f x11grab -r 25 -s 800x600 -i :0.0 /tmp/outputFile.mpg # Search commandlinefu.com from the command line using the API cmdfu(){ curl "http://www.commandlinefu.com/commands/matching/$@/$(echo -n $@ | openssl base64)/plaintext"; } # Open Finder from the current Terminal location open . # Processor / memory bandwidthd? in GB/s dd if=/dev/zero of=/dev/null bs=1M count=32768 # mkdir & cd into it as single command mkdir /home/foo/doc/bar && cd $_ # Remove a line in a text file. Useful to fix ssh-keygen -R <the_offending_host> # Attach screen over ssh ssh -t remote_host screen -r # Share a terminal screen with others % screen -r someuser/ # Create a persistent connection to a machine ssh -MNf <user>@<host> # Copy your ssh public key to a server from a machine that doesn't have ssh-copy-id cat ~/.ssh/id_rsa.pub | ssh user@machine "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys" # Makes the permissions of file2 the same as file1 chmod --reference file1 file2 # git remove files which have been deleted git add -u # directly ssh to host B that is only accessible through host A ssh -t hostA ssh hostB # (Debian/Ubuntu) Discover what package a file belongs to dpkg -S /usr/bin/ls # List all bash shortcuts bind -P # Draw kernel module dependancy graph. lsmod | perl -e 'print "digraph \"lsmod\" {";<>;while(<>){@_=split/\s+/; print "\"$_[0]\" -> \"$_\"\n" for split/,/,$_[3]}print "}"' | dot -Tpng | display - # RTFM function rtfm() { help $@ || man $@ || $BROWSER "http://www.google.com/search?q=$@"; } # Show numerical values for each of the 256 colors in bash for code in {0..255}; do echo -e "\e[38;05;${code}m $code: Test"; done # To print a specific line from a file sed -n 5p <file> # Run a command only when load average is below a certain threshold echo "rm -rf /unwanted-but-large/folder" | batch # Download Youtube video with wget! wget http://www.youtube.com/watch?v=dQw4w9WgXcQ -qO- | sed -n "/fmt_url_map/{s/[\'\"\|]/\n/g;p}" | sed -n '/^fmt_url_map/,/videoplayback/p' | sed -e :a -e '$q;N;5,$D;ba' | tr -d '\n' | sed -e 's/\(.*\),\(.\)\{1,3\}/\1/' | wget -i - -O surprise.flv # Broadcast your shell thru ports 5000, 5001, 5002 ... script -qf | tee >(nc -kl 5000) >(nc -kl 5001) >(nc -kl 5002) # Eavesdrop on your system diff <(lsof -p 1234) <(sleep 10; lsof -p 1234) # Remove security limitations from PDF documents using ghostscript gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf -c .setpdfwrite -f INPUT.pdf # commandlinefu.com by David Winterbottom # Run a file system check on your next boot. sudo touch /forcefsck # Release memory used by the Linux kernel on caches free && sync && echo 3 > /proc/sys/vm/drop_caches && free # Remove all files previously extracted from a tar(.gz) file. tar -tf <file.tar.gz> | xargs rm -r # using `!#$' to referance backward-word cp /work/host/phone/ui/main.cpp !#$:s/host/target # Binary Clock watch -n 1 'echo "obase=2;`date +%s`" | bc' # List only the directories ls -d */ # Download all images from a site wget -r -l1 --no-parent -nH -nd -P/tmp -A".gif,.jpg" http://example.com/images # Duplicate installed packages from one machine to the other (RPM-based systems) ssh root@remote.host "rpm -qa" | xargs yum -y install # Start COMMAND, and kill it if still running after 5 seconds timeout 5s COMMAND # Use tee to process a pipe with two or more processes echo "tee can split a pipe in two"|tee >(rev) >(tr ' ' '_') # Edit a google doc with vim google docs edit --title "To-Do List" --editor vim # Backup all MySQL Databases to individual files for I in $(mysql -e 'show databases' -s --skip-column-names); do mysqldump $I | gzip > "$I.sql.gz"; done # Port Knocking! knock <host> 3000 4000 5000 && ssh -p <port> user@host && knock <host> 5000 4000 3000 # Show a 4-way scrollable process tree with full details. ps awwfux | less -S # which program is this port belongs to ? lsof -i tcp:80 # Sort the size usage of a directory tree by gigabytes, kilobytes, megabytes, then bytes. du -b --max-depth 1 | sort -nr | perl -pe 's{([0-9]+)}{sprintf "%.1f%s", $1>=2**30? ($1/2**30, "G"): $1>=2**20? ($1/2**20, "M"): $1>=2**10? ($1/2**10, "K"): ($1, "")}e' # Google text-to-speech in mp3 format wget -q -U Mozilla -O output.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=hello+world # What is my public IP-address? curl ifconfig.me # Search recursively to find a word or phrase in certain file types, such as C code find . -name "*.[ch]" -exec grep -i -H "search pharse" {} \; # Bring the word under the cursor on the :ex line in Vim :<C-R><C-W> # Block known dirty hosts from reaching your machine wget -qO - http://infiltrated.net/blacklisted|awk '!/#|[a-z]/&&/./{print "iptables -A INPUT -s "$1" -j DROP"}' # Synchronize date and time with a server over ssh date --set="$(ssh user@server date)" # Recursively remove all empty directories find . -type d -empty -delete # Add timestamp to history export HISTTIMEFORMAT="%F %T " # Rapidly invoke an editor to write a long, complex, or tricky command fc # commandlinefu.com by David Winterbottom # Find out how much data is waiting to be written to disk grep ^Dirty /proc/meminfo # Remove a line in a text file. Useful to fix "ssh host key change" warnings sed -i 8d ~/.ssh/known_hosts # Search for a <pattern> string inside all files in the current directory grep -RnisI <pattern> * # Colorized grep in less grep --color=always | less -R # check site ssl certificate dates echo | openssl s_client -connect www.google.com:443 2>/dev/null |openssl x509 -dates -noout # Create a quick back-up copy of a file cp file.txt{,.bak} # Convert Youtube videos to MP3 youtube-dl -t --extract-audio --audio-format mp3 YOUTUBE_URL_HERE # Exclude multiple columns using AWK awk '{$1=$3=""}1' file # Get the IP of the host your coming from when logged in remotely echo ${SSH_CLIENT%% *} # ls not pattern ls !(*.gz) # Show apps that use internet connection at the moment. lsof -P -i -n | cut -f 1 -d " "| uniq | tail -n +2 # Compare two directory trees. diff <(cd dir1 && find | sort) <(cd dir2 && find | sort) # exit without saving history kill -9 $$ # Remind yourself to leave in 15 minutes leave +15 # make directory tree mkdir -p work/{d1,d2}/{src,bin,bak} # Control ssh connection [enter]~? # pretend to be busy in office to enjoy a cup of coffee cat /dev/urandom | hexdump -C | grep "ca fe" # run complex remote shell cmds over ssh, without escaping quotes ssh host -l user $(<cmd.txt) # GREP a PDF file. pdftotext [file] - | grep 'YourPattern' # Exclude .svn, .git and other VCS junk for a pristine tarball tar --exclude-vcs -cf src.tar src/ # delete a line from your shell history history -d # Recursively change permissions on files, leave directories alone. find ./ -type f -exec chmod 644 {} \; # Convert PDF to JPG for file in `ls *.pdf`; do convert -verbose -colorspace RGB -resize 800 -interlace none -density 300 -quality 80 $file `echo $file | sed 's/\.pdf$/\.jpg/'`; done # Prettify an XML file tidy -xml -i -m [file] # Quick access to ASCII code of a key showkey -a # commandlinefu.com by David Winterbottom # find files in a date range find . -type f -newermt "2010-01-01" ! -newermt "2010-06-01" # notify yourself when a long-running command which has ALREADY STARTED is finished <ctrl+z> fg; notify_me # easily find megabyte eating files or directories alias dush="du -sm *|sort -n|tail" # prints line numbers nl # A robust, modular log coloriser ccze # Remove blank lines from a file using grep and save output to new file grep . filename > newfilename # Create a directory and change into it at the same time md () { mkdir -p "$@" && cd "$@"; } # Save your sessions in vim to resume later :mksession! <filename> # How to establish a remote Gnu screen session that you can re-connect to ssh -t user@some.domain.com /usr/bin/screen -xRR # Take screenshot through SSH DISPLAY=:0.0 import -window root /tmp/shot.png # Lists all listening ports together with the PID of the associated process lsof -Pan -i tcp -i udp # Opens vi/vim at pattern in file vi +/pattern [file] # Alias HEAD for automatic smart output alias head='head -n $((${LINES:-`tput lines 2>/dev/null||echo -n 12`} - 2))' # Create colorized html file from Vim or Vimdiff :TOhtml # Colorful man apt-get install most && update-alternatives --set pager /usr/bin/most # Pipe stdout and stderr, etc., to separate commands some_command > >(/bin/cmd_for_stdout) 2> >(/bin/cmd_for_stderr) # Go to parent directory of filename edited in last command cd !$:h # Diff on two variables diff <(echo "$a") <(echo "$b") # Draw a Sierpinski triangle perl -e 'print "P1\n256 256\n", map {$_&($_>>8)?1:0} (0..0xffff)' | display # Create a nifty overview of the hardware in your computer lshw -html > hardware.html # Manually Pause/Unpause Firefox Process with POSIX-Signals killall -STOP -m firefox # Gets a random Futurama quote from /. curl -Is slashdot.org | egrep '^X-(F|B|L)' | cut -d \- -f 2 # Use lynx to run repeating website actions lynx -accept_all_cookies -cmd_script=/your/keystroke-file # Intercept, monitor and manipulate a TCP connection. mkfifo /tmp/fifo; cat /tmp/fifo | nc -l -p 1234 | tee -a to.log | nc machine port | tee -a from.log > /tmp/fifo # Display a list of committers sorted by the frequency of commits svn log -q|grep "|"|awk "{print \$3}"|sort|uniq -c|sort -nr # commandlinefu.com by David Winterbottom # Copy a MySQL Database to a new Server via SSH with one command mysqldump --add-drop-table --extended-insert --force --log-error=error.log -uUSER -pPASS OLD_DB_NAME | ssh -C user@newhost "mysql -uUSER -pPASS NEW_DB_NAME" # Fast, built-in pipe-based data sink <COMMAND> |: # Use file(1) to view device information file -s /dev/sd* # Find usb device diff <(lsusb) <(sleep 3s && lsusb) # Bind a key with a command bind -x '"\C-l":ls -l' # copy working directory and compress it on-the-fly while showing progress tar -cf - . | pv -s $(du -sb . | awk '{print $1}') | gzip > out.tgz # analyze traffic remotely over ssh w/ wireshark ssh root@server.com 'tshark -f "port !22" -w -' | wireshark -k -i - # View the newest xkcd comic. xkcd(){ wget -qO- http://xkcd.com/|tee >(feh $(grep -Po '(?<=")http://imgs[^/]+/comics/[^"]+\.\w{3}'))|grep -Po '(?<=(\w{3})" title=").*(?=" alt)';} # convert unixtime to human-readable date -d @1234567890 # Schedule a script or command in x num hours, silently run in the background even if logged out ( ( sleep 2h; your-command your-args ) & ) # recursive search and replace old with new string, inside files $ grep -rl oldstring . |xargs sed -i -e 's/oldstring/newstring/' # Find files that have been modified on your system in the past 60 minutes sudo find / -mmin 60 -type f # find geographical location of an ip address lynx -dump http://www.ip-adress.com/ip_tracer/?QRY=$1|grep address|egrep 'city|state|country'|awk '{print $3,$4,$5,$6,$7,$8}'|sed 's\ip address flag \\'|sed 's\My\\' # read manpage of a unix command as pdf in preview (Os X) man -t UNIX_COMMAND | open -f -a preview # Speed up launch of firefox find ~ -name '*.sqlite' -exec sqlite3 '{}' 'VACUUM;' \; # List the number and type of active network connections netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c # format txt as table not joining empty columns column -tns: /etc/passwd # Bind a key with a command bind '"\C-l":"ls -l\n"' # prevent accidents while using wildcards rm *.txt <TAB> <TAB> # Random Number Between 1 And X echo $[RANDOM%X+1] # live ssh network throughput test yes | pv | ssh $host "cat > /dev/null" # Press Any Key to Continue read -sn 1 -p "Press any key to continue..." # backup all your commandlinefu.com favourites to a plaintext file clfavs(){ URL="http://www.commandlinefu.com";wget -O - --save-cookies c --post-data "username=$1&password=$2&submit=Let+me+in" $URL/users/signin;for i in `seq 0 25 $3`;do wget -O - --load-cookies c $URL/commands/favourites/plaintext/$i >>$4;done;rm -f c;} # send echo to socket network echo "foo" > /dev/tcp/192.168.1.2/25 # Perform a branching conditional true && { echo success;} || { echo failed; } # commandlinefu.com by David Winterbottom # Resume scp of a big file rsync --partial --progress --rsh=ssh $file_source $user@$host:$destination_file # shut of the screen. xset dpms force standby # Create a single-use TCP (or UDP) proxy nc -l -p 2000 -c "nc example.org 3000" # runs a bash script in debugging mode bash -x ./post_to_commandlinefu.sh # Instead of writing a multiline if/then/else/fi construct you can do that by one line [[ test_condition ]] && if_true_do_this || otherwise_do_that # Use tee + process substitution to split STDOUT to multiple commands some_command | tee >(command1) >(command2) >(command3) ... | command4 # Retry the previous command until it exits successfully until !!; do :; done # Shell recorder with replay script -t /tmp/mylog.out 2>/tmp/mylog.time; <do your work>; <CTRL-D>; scriptreplay /tmp/mylog.time /tmp/mylog.out # A child process which survives the parent's death (for sure) ( command & ) # find all file larger than 500M find / -type f -size +500M # exclude a column with cut cut -f5 --complement # Recover a deleted file grep -a -B 25 -A 100 'some string in the file' /dev/sda1 > results.txt # April Fools' Day Prank PROMPT_COMMAND='if [ $RANDOM -le 3200 ]; then printf "\0337\033[%d;%dH\033[4%dm \033[m\0338" $((RANDOM%LINES+1)) $((RANDOM%COLUMNS+1)) $((RANDOM%8)); fi' # Listen to BBC Radio from the command line. bbcradio() { local s PS3="Select a station: ";select s in 1 1x 2 3 4 5 6 7 "Asian Network an" "Nations & Local lcl";do break;done;s=($s);mplayer -playlist "http://www.bbc.co.uk/radio/listen/live/r"${s[@]: -1}".asx";} # Monitor bandwidth by pid nethogs -p eth0 # throttle bandwidth with cstream tar -cj /backup | cstream -t 777k | ssh host 'tar -xj -C /backup' # Run a long job and notify me when it's finished ./my-really-long-job.sh && notify-send "Job finished" # List all files opened by a particular command lsof -c dhcpd # Tell local Debian machine to install packages used by remote Debian machine ssh remotehost 'dpkg --get-selections' | dpkg --set-selections && dselect install # GRUB2: set Super Mario as startup tune echo "GRUB_INIT_TUNE=\"1000 334 1 334 1 0 1 334 1 0 1 261 1 334 1 0 1 392 2 0 4 196 2\"" | sudo tee -a /etc/default/grub > /dev/null && sudo update-grub # Diff remote webpages using wget diff <(wget -q -O - URL1) <(wget -q -O - URL2) # Close a hanging ssh session ~. # List alive hosts in specific subnet nmap -sP 192.168.1.0/24 # intersection between two files grep -Fx -f file1 file2 # processes per user counter ps hax -o user | sort | uniq -c # commandlinefu.com by David Winterbottom # 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 # Show current working directory of a process pwdx pid # Quickly (soft-)reboot skipping hardware checks /sbin/kexec -l /boot/$KERNEL --append="$KERNELPARAMTERS" --initrd=/boot/$INITRD; sync; /sbin/kexec -e # Nicely display permissions in octal format with filename stat -c '%A %a %n' * # Brute force discover sudo zcat /var/log/auth.log.*.gz | awk '/Failed password/&&!/for invalid user/{a[$9]++}/Failed password for invalid user/{a["*" $11]++}END{for (i in a) printf "%6s\t%s\n", a[i], i|"sort -n"}' # convert uppercase files to lowercase files rename 'y/A-Z/a-z/' * # Check if system is 32bit or 64bit getconf LONG_BIT # Limit the cpu usage of a process sudo cpulimit -p pid -l 50 # Convert seconds into minutes and seconds bc <<< 'obase=60;299' # Repoint an existing symlink to a new location ln -nsf <TARGET> <LINK> # send a circular wall <<< "Broadcast This" # The BOFH Excuse Server telnet towel.blinkenlights.nl 666 # List files with quotes around each filename ls -Q # See udev at work udevadm monitor # Get your outgoing IP address dig +short myip.opendns.com @resolver1.opendns.com # Makes you look busy alias busy='my_file=$(find /usr/include -type f | sort -R | head -n 1); my_len=$(wc -l $my_file | awk "{print $1}"); let "r = $RANDOM % $my_len" 2>/dev/null; vim +$r $my_file' # Insert the last argument of the previous command <ESC> . # Duplicate several drives concurrently dd if=/dev/sda | tee >(dd of=/dev/sdb) | dd of=/dev/sdc # Make sure a script is run in a terminal. [ -t 0 ] || exit 1 # Get your external IP address curl ip.appspot.com # use vim to get colorful diff output svn diff | view - # A fun thing to do with ram is actually open it up and take a peek. This command will show you all the string (plain text) values in ram sudo strings /dev/mem # find files containing text grep -lir "some text" * # Quickly graph a list of numbers gnuplot -persist <(echo "plot '<(sort -n listOfNumbers.txt)' with lines") # Analyse an Apache access log for the most common IP addresses tail -10000 access_log | awk '{print $1}' | sort | uniq -c | sort -n | tail # commandlinefu.com by David Winterbottom # prevent large files from being cached in memory (backups!) nocache <I/O-heavy-command> # Print diagram of user/groups awk 'BEGIN{FS=":"; print "digraph{"}{split($4, a, ","); for (i in a) printf "\"%s\" [shape=box]\n\"%s\" -> \"%s\"\n", $1, a[i], $1}END{print "}"}' /etc/group|display # Create strong, but easy to remember password read -s pass; echo $pass | md5sum | base64 | cut -c -16 # Generate an XKCD #936 style 4 word password shuf -n4 /usr/share/dict/words | tr -d '\n' # Python version 3: Serve current directory tree at http://$HOSTNAME:8000/ python -m http.server # VI config to save files with +x when a shebang is found on line 1 au BufWritePost * if getline(1) =~ "^#!" | if getline(1) =~ "/bin/" | silent !chmod +x <afile> | endif | endif # I finally found out how to use notify-send with at or cron echo "export DISPLAY=:0; export XAUTHORITY=~/.Xauthority; notify-send test" | at now+1minute # perl one-liner to get the current week number date +%V # Execute a command with a timeout timeout 10 sleep 11 # Find files that were modified by a given command touch /tmp/file ; $EXECUTECOMMAND ; find /path -newer /tmp/file # Have an ssh session open forever autossh -M50000 -t server.example.com 'screen -raAd mysession' # pipe output of a command to your clipboard some command|xsel --clipboard # Recursively compare two directories and output their differences on a readable format diff -urp /originaldirectory /modifieddirectory # Make anything more awesome command | figlet # When feeling down, this command helps sl # List recorded formular fields of Firefox cd ~/.mozilla/firefox/ && sqlite3 `cat profiles.ini | grep Path | awk -F= '{print $2}'`/formhistory.sqlite "select * from moz_formhistory" && cd - > /dev/null # Base conversions with bc echo "obase=2; 27" | bc -l # ls -hog --> a more compact ls -l ls -hog # Start a command on only one CPU core taskset -c 0 your_command # Get all the keyboard shortcuts in screen ^A ? # Switch 2 characters on a command line. ctrl-t # Notepad in a browser (type this in the URL bar) data:text/html, <html contenteditable> # convert single digit to double digits for i in ?.ogg; do mv $i 0$i; done # Annotate tail -f with timestamps tail -f file | while read; do echo "$(date +%T.%N) $REPLY"; done # Stamp a text line on top of the pdf pages. echo "This text gets stamped on the top of the pdf pages." | enscript -B -f Courier-Bold16 -o- | ps2pdf - | pdftk input.pdf stamp - output output.pdf # commandlinefu.com by David Winterbottom # Create a file server, listening in port 7000 while true; do nc -l 7000 | tar -xvf -; done # bypass any aliases and functions for the command \foo # cat a bunch of small files with file indication grep . * # Find broken symlinks find -L . -type l # stderr in color mycommand 2> >(while read line; do echo -e "\e[01;31m$line\e[0m"; done) # Stop Flash from tracking everything you do. for i in ~/.adobe ~/.macromedia ; do ( rm $i/ -rf ; ln -s /dev/null $i ) ; done # Save a file you edited in vim without the needed permissions (no echo) :w !sudo tee > /dev/null % # Delete all empty lines from a file with vim :g/^$/d # Remote screenshot DISPLAY=":0.0" import -window root screenshot.png # your terminal sings echo {1..199}" bottles of beer on the wall, cold bottle of beer, take one down, pass it around, one less bottle of beer on the wall,, " | espeak -v english -s 140 # Define words and phrases with google. define(){ local y="$@";curl -sA"Opera" "http://www.google.com/search?q=define:${y// /+}"|grep -Po '(?<=<li>)[^<]+'|nl|perl -MHTML::Entities -pe 'decode_entities($_)' 2>/dev/null;} # Harder, Faster, Stronger SSH clients ssh -4 -C -c blowfish-cbc # Clean up poorly named TV shows. rename -v 's/.*[s,S](\d{2}).*[e,E](\d{2}).*\.avi/SHOWNAME\ S$1E$2.avi/' poorly.named.file.s01e01.avi # Backup all MySQL Databases to individual files for db in $(mysql -e 'show databases' -s --skip-column-names); do mysqldump $db | gzip > "/backups/mysqldump-$(hostname)-$db-$(date +%Y-%m-%d-%H.%M.%S).gz"; done # check open ports lsof -Pni4 | grep LISTEN # Triple monitoring in screen tmpfile=$(mktemp) && echo -e 'startup_message off\nscreen -t top htop\nsplit\nfocus\nscreen -t nethogs nethogs wlan0\nsplit\nfocus\nscreen -t iotop iotop' > $tmpfile && sudo screen -c $tmpfile # Save an HTML page, and covert it to a .pdf file wget $URL | htmldoc --webpage -f "$URL".pdf - ; xpdf "$URL".pdf & # DELETE all those duplicate files but one based on md5 hash comparision in the current directory tree find . -type f -print0|xargs -0 md5sum|sort|perl -ne 'chomp;$ph=$h;($h,$f)=split(/\s+/,$_,2);print "$f"."\x00" if ($h eq $ph)'|xargs -0 rm -v -- # 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; sudo swapon /swapfile # Relocate a file or directory, but keep it accessible on the old location throug a simlink. mv $1 $2 && ln -s $2/$(basename $1) $(dirname $1) # a short counter yes '' | cat -n # Rsync remote data as root using sudo rsync --rsync-path 'sudo rsync' username@source:/folder/ /local/ # Transfer SSH public key to another machine in one step ssh-keygen; ssh-copy-id user@host; ssh user@host # git remove files which have been deleted git rm $(git ls-files --deleted) # Copy stdin to your X11 buffer ssh user@host cat /path/to/some/file | xclip # commandlinefu.com by David Winterbottom # Get info about remote host ports and OS detection nmap -sS -P0 -sV -O <target> # List of commands you use most often history | awk '{print $2}' | sort | uniq -c | sort -rn | head # Copy a file structure without files find * -type d -exec mkdir /where/you/wantem/\{\} \; # Get list of servers with a specific port open nmap -sT -p 80 -oG - 192.168.1.* | grep open # Display current time in requested time zones. zdump Japan America/New_York # dmesg with colored human-readable dates dmesg -T|sed -e 's|\(^.*'`date +%Y`']\)\(.*\)|\x1b[0;34m\1\x1b[0m - \2|g' # Extract audio from Flash video (*.flv) as mp3 file ffmpeg -i video.flv -vn -ar 44100 -ac 2 -ab 192k -f mp3 audio.mp3 # Share your terminal session real-time mkfifo foo; script -f foo # Short and elegant way to backup a single file before you change it. cp httpd.conf{,.bk} # clean up memory of unnecessary things (Kernerl 2.6.16 or newer) sync && echo 1 > /proc/sys/vm/drop_caches # Find all the links to a file find -L / -samefile /path/to/file -exec ls -ld {} + # Recover tmp flash videos (deleted immediately by the browser plugin) for h in `find /proc/*/fd -ilname "/tmp/Flash*" 2>/dev/null`; do ln -s "$h" `readlink "$h" | cut -d' ' -f1`; done # Single use vnc-over-ssh connection ssh -f -L 5900:localhost:5900 your.ssh.server "x11vnc -safer -localhost -nopw -once -display :0"; vinagre localhost:5900 # send a circular echo "dear admin, please ban eribsskog" | wall # Visit wikileaks.com echo 213.251.145.96 wikileaks.com >>/etc/hosts # List all open ports and their owning executables lsof -i -P | grep -i "listen" # Create a single PDF from multiple images with ImageMagick convert *.jpg output.pdf # dd with progress bar and statistics sudo dd if=/dev/sdc bs=4096 | pv -s 2G | sudo dd bs=4096 of=~/USB_BLACK_BACKUP.IMG # View all date formats, Quick Reference Help Alias alias dateh='date --help|sed -n "/^ *%%/,/^ *%Z/p"|while read l;do F=${l/% */}; date +%$F:"|'"'"'${F//%n/ }'"'"'|${l#* }";done|sed "s/\ *|\ */|/g" |column -s "|" -t' # Monitor open connections for httpd including listen, count and sort it per IP watch "netstat -plan|grep :80|awk {'print \$5'} | cut -d: -f 1 | sort | uniq -c | sort -nk 1" # output your microphone to a remote computer's speaker arecord -f dat | ssh -C user@host aplay -f dat # download and unpack tarball without leaving it sitting on your hard drive wget -qO - http://example.com/path/to/blah.tar.gz | tar xzf - # Colored diff ( via vim ) on 2 remotes files on your local computer. vimdiff scp://root@server-foo.com//etc/snmp/snmpd.conf scp://root@server-bar.com//etc/snmp/snmpd.conf # Split a tarball into multiple parts tar cf - <dir>|split -b<max_size>M - <name>.tar. # Matrix Style echo -e "\e[32m"; while :; do for i in {1..16}; do r="$(($RANDOM % 2))"; if [[ $(($RANDOM % 5)) == 1 ]]; then if [[ $(($RANDOM % 4)) == 1 ]]; then v+="\e[1m $r "; else v+="\e[2m $r "; fi; else v+=" "; fi; done; echo -e "$v"; v=""; done # commandlinefu.com by David Winterbottom # Pretty Print a simple csv in the command line column -s, -t <tmp.csv # Ultimate current directory usage command ncdu # Cleanup firefox's database. find ~/.mozilla/firefox/ -type f -name "*.sqlite" -exec sqlite3 {} VACUUM \; # Terminal - Show directories in the PATH, one per line with sed and bash3.X `here string' tr : '\n' <<<$PATH # Find Duplicate Files (based on MD5 hash) find -type f -exec md5sum '{}' ';' | sort | uniq --all-repeated=separate -w 33 | cut -c 35- # Print a great grey scale demo ! yes "$(seq 232 255;seq 254 -1 233)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done # Smiley Face Bash Prompt PS1="\`if [ \$? = 0 ]; then echo \e[33\;40m\\\^\\\_\\\^\e[0m; else echo \e[36\;40m\\\-\e[0m\\\_\e[36\;40m\\\-\e[0m; fi\` \u \w:\h)" # Create a system overview dashboard on F12 key bind '"\e[24~"':"\"ps -elF;df -h;free -mt;netstat -lnpt;who -a\C-m""" # coloured tail tail -f FILE | perl -pe 's/KEYWORD/\e[1;31;43m$&\e[0m/g' # Search for commands from the command line clfu-seach <search words> # Install a Firefox add-on/theme to all users sudo firefox -install-global-extension /path/to/add-on # clear current line CTRL+u # 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=dbname & # Put readline into vi mode set -o vi # Delete all empty lines from a file with vim :g!/\S/d # 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 # Recursively remove .svn directories from the current location find . -type d -name '.svn' -print0 | xargs -0 rm -rdf # Start a new command in a new screen window alias s='screen -X screen'; s top; s vi; s man ls; # Efficiently print a line deep in a huge log file sed '1000000!d;q' < massive-log-file.log # Encrypted archive with openssl and tar tar --create --file - --posix --gzip -- <dir> | openssl enc -e -aes256 -out <file> # Remove a range of lines from a file sed -i <file> -re '<start>,<end>d' # Hide the name of a process listed in the `ps` output exec -a "/sbin/getty 38400 tty7" your_cmd -erase_all_files # Create a local compressed tarball from remote host directory ssh user@host "tar -zcf - /path/to/dir" > dir.tar.gz # convert from hexidecimal or octal to decimal echo $((0x1fe)) $((033)) # save man-page as pdf man -t awk | ps2pdf - awk.pdf # commandlinefu.com by David Winterbottom # Convert seconds into minutes and seconds echo 'obase=60;299' | bc # List by size all of the directories in a given tree. du -h /path | sort -h # All IP connected to my host netstat -lantp | grep ESTABLISHED |awk '{print $5}' | awk -F: '{print $1}' | sort -u # df without line wrap on long FS name df -P | column -t # rsync instead of scp rsync --progress --partial --rsh="ssh -p 8322" --bwlimit=100 --ipv4 user@domain.com:~/file.tgz . # Download a file and uncompress it while it downloads wget http://URL/FILE.tar.gz -O - | tar xfz - # Reuse last parameter !$ # Make sudo forget password instantly sudo -K # View user activity per directory. sudo lsof -u someuser -a +D /etc # Print a row of characters across the terminal printf "%`tput cols`s"|tr ' ' '#' # Limit bandwidth usage by apt-get sudo apt-get -o Acquire::http::Dl-Limit=30 upgrade # Convert text to picture echo -e "Some Text Line1\nSome Text Line 2" | convert -background none -density 196 -resample 72 -unsharp 0x.5 -font "Courier" text:- -trim +repage -bordercolor white -border 3 text.gif # track flights from the command line flight_status() { if [[ $# -eq 3 ]];then offset=$3; else offset=0; fi; curl "http://mobile.flightview.com/TrackByRoute.aspx?view=detail&al="$1"&fn="$2"&dpdat=$(date +%Y%m%d -d ${offset}day)" 2>/dev/null |html2text |grep ":"; } # Tune your guitar from the command line. for n in E2 A2 D3 G3 B3 E4;do play -n synth 4 pluck $n repeat 2;done # Remove executable bit from all files in the current directory recursively, excluding other directories chmod -R -x+X * # convert filenames in current directory to lowercase rename 'y/A-Z/a-z/' * # More precise BASH debugging env PS4=' ${BASH_SOURCE}:${LINENO}(${FUNCNAME[0]}) ' sh -x /etc/profile # Remove color codes (special characters) with sed sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" # redirect stdout and stderr each to separate files and print both to the screen (some_command 2>&1 1>&3 | tee /path/to/errorlog ) 3>&1 1>&2 | tee /path/to/stdoutlog # get all pdf and zips from a website using wget wget --reject html,htm --accept pdf,zip -rl1 url # bash: hotkey to put current commandline to text-editor bash-hotkey: <CTRL+x+e> # Use all the cores or CPUs when compiling make -j 4 # Prints total line count contribution per user for an SVN repository svn ls -R | egrep -v -e "\/$" | xargs svn blame | awk '{print $2}' | sort | uniq -c | sort -r # Analyze awk fields awk '{print NR": "$0; for(i=1;i<=NF;++i)print "\t"i": "$i}' # Create a list of binary numbers echo {0..1}{0..1}{0..1}{0..1} # commandlinefu.com by David Winterbottom # Run a command, store the output in a pastebin on the internet and place the URL on the xclipboard ls | curl -F 'sprunge=<-' http://sprunge.us | xclip # Get your public ip using dyndns curl -s http://checkip.dyndns.org/ | grep -o "[[:digit:].]\+" # Start screen in detached mode screen -d -m [<command>] # How to run X without any graphics hardware startx -- `which Xvfb` :1 -screen 0 800x600x24 && DISPLAY=:1 x11vnc # Cut out a piece of film from a file. Choose an arbitrary length and starting time. ffmpeg -vcodec copy -acodec copy -i orginalfile -ss 00:01:30 -t 0:0:20 newfile # iso-8859-1 to utf-8 safe recursive rename detox -r -s utf_8 /path/to/old/win/files/dir # List the files any process is using lsof +p xxxx # Pipe STDOUT to vim tail -1000 /some/file | vim - # Show biggest files/directories, biggest first with 'k,m,g' eyecandy du --max-depth=1 | sort -r -n | awk '{split("k m g",v); s=1; while($1>1024){$1/=1024; s++} print int($1)" "v[s]"\t"$2}' # Terminate a frozen SSH-session RETURN~. # change directory to actual path instead of symlink path cd `pwd -P` # Download an entire static website to your local machine wget --recursive --page-requisites --convert-links www.moyagraphix.co.za # Batch convert files to utf-8 find . -name "*.php" -exec iconv -f ISO-8859-1 -t UTF-8 {} -o ../newdir_utf8/{} \; # Get http headers for an url curl -I www.commandlinefu.com # vi a remote file vi scp://username@host//path/to/somefile # Show permissions of current directory and all directories upwards to / namei -m $(pwd) # Use top to monitor only all processes with the same name fragment 'foo' top -p $(pgrep -d , foo) # delete command line last word ctrl+w # move you up one directory quickly shopt -s autocd # Remove a line from a file using sed (useful for updating known SSH server keys when they change) ssh-keygen -R <thehost> # Show what PID is listening on port 80 on Linux fuser -v 80/tcp # Draw kernel module dependancy graph. lsmod | awk 'BEGIN{print "digraph{"}{split($4, a, ","); for (i in a) print $1, "->", a[i]}END{print "}"}'|display # Color man pages echo "export LESS_TERMCAP_mb=$'\E[01;31m'" >> ~/.bashrc # List files accessed by a command strace -ff -e trace=file my_command 2>&1 | perl -ne 's/^[^"]+"(([^\\"]|\\[\\"nt])*)".*/$1/ && print' # Protect directory from an overzealous rm -rf * cd <directory>; touch ./-i # commandlinefu.com by David Winterbottom # Watch RX/TX rate of an interface in kb/s while [ /bin/true ]; do OLD=$NEW; NEW=`cat /proc/net/dev | grep eth0 | tr -s ' ' | cut -d' ' -f "3 11"`; echo $NEW $OLD | awk '{printf("\rin: % 9.2g\t\tout: % 9.2g", ($1-$3)/1024, ($2-$4)/1024)}'; sleep 1; done # Figure out what shell you're running echo $0 # Blink LED Port of NIC Card ethtool -p eth0 # See where a shortened url takes you before click check(){ curl -sI $1 | sed -n 's/Location: *//p';} # Running scripts after a reboot for non-root users . @reboot <yourscript.sh> # run command on a group of nodes in parallel echo "uptime" | tee >(ssh host1) >(ssh host2) >(ssh host3) # run command on a group of nodes in parallel echo "uptime" | pee "ssh host1" "ssh host2" "ssh host3" # Remove Thumbs.db files from folders find ./ -name Thumbs.db -delete # Display BIOS Information # dd if=/dev/mem bs=1k skip=768 count=256 2>/dev/null | strings -n 8 # open path with your default program (on Linux/*BSD) xdg-open [path] # Copy an element from the previous command !:1-3 # use the previous commands params in the current command !!:[position] # Mirror a directory structure from websites with an Apache-generated file indexes lftp -e "mirror -c" http://example.com/foobar/ # Choose from a nice graphical menu which DI.FM radio station to play zenity --list --width 500 --height 500 --column 'radio' --column 'url' --print-column 2 $(curl -s http://www.di.fm/ | awk -F '"' '/href="http:.*\.pls.*96k/ {print $2}' | sort | awk -F '/|\.' '{print $(NF-1) " " $0}') | xargs mplayer # Quickly share code or text from vim to others. :w !curl -F "sprunge=<-" http://sprunge.us | xclip # copy from host1 to host2, through your host ssh root@host1 "cd /somedir/tocopy/ && tar -cf - ." | ssh root@host2 "cd /samedir/tocopyto/ && tar -xf -" # Change prompt to MS-DOS one (joke) export PS1="C:\$( pwd | sed 's:/:\\\\\\:g' )\\> " # View network activity of any application or user in realtime lsof -r 2 -p PID -i -a # Print a list of standard error codes and descriptions. perl -le 'print $!+0, "\t", $!++ for 0..127' # Unbelievable Shell Colors, Shading, Backgrounds, Effects for Non-X for c in `seq 0 255`;do t=5;[[ $c -lt 108 ]]&&t=0;for i in `seq $t 5`;do echo -e "\e[0;48;$i;${c}m|| $i:$c `seq -s+0 $(($COLUMNS/2))|tr -d '[0-9]'`\e[0m";done;done # grep -v with multiple patterns. grep 'test' somefile | grep -vE '(error|critical|warning)' # Identify long lines in a file awk 'length>72' file # Show directories in the PATH, one per line echo "${PATH//:/$'\n'}" # Mount the first NTFS partition inside a VDI file (VirtualBox Disk Image) mount -t ntfs-3g -o ro,loop,uid=user,gid=group,umask=0007,fmask=0117,offset=0x$(hd -n 1000000 image.vdi | grep "eb 52 90 4e 54 46 53" | cut -c 1-8) image.vdi /mnt/vdi-ntfs # Show me a histogram of the busiest minutes in a log file: cat /var/log/secure.log | awk '{print substr($0,0,12)}' | uniq -c | sort -nr | awk '{printf("\n%s ",$0) ; for (i = 0; i<$1 ; i++) {printf("*")};}' # commandlinefu.com by David Winterbottom # Display current bandwidth statistics ifstat -nt # Given process ID print its environment variables sed 's/\o0/\n/g' /proc/INSERT_PID_HERE/environ # restoring some data from a corrupted text file ( cat badfile.log ; tac badfile.log | tac ) > goodfile.log # view the system console remotely sudo cat /dev/vcs1 | fold -w 80 # Fix Ubuntu's Broken Sound Server sudo killall -9 pulseaudio; pulseaudio >/dev/null 2>&1 & # Look up the definition of a word curl dict://dict.org/d:something # Diff files on two remote hosts. diff <(ssh alice cat /etc/apt/sources.list) <(ssh bob cat /etc/apt/sources.list) # Ctrl+S Ctrl+Q terminal output lock and unlock Ctrl+S Ctrl+Q # Redirect STDIN < /path/to/file.txt grep foo # I hate `echo X | Y` base64 -d <<< aHR0cDovL3d3dy50d2l0dGVyc2hlZXAuY29tL3Jlc3VsdHMucGhwP3U9Y29tbWFuZGxpbmVmdQo= # Schedule a download at a later time echo 'wget url' | at 01:00 # Send keypresses to an X application xvkbd -xsendevent -text "Hello world" # Add calendar to desktop wallpaper convert -font -misc-fixed-*-*-*-*-*-*-*-*-*-*-*-* -fill black -draw "text 270,260 \" `cal` \"" testpic.jpg newtestpic.jpg # Browse system RAM in a human readable form sudo cat /proc/kcore | strings | awk 'length > 20' | less # Get Cisco network information tcpdump -nn -v -i eth0 -s 1500 -c 1 'ether[20:2] == 0x2000' # Quick glance at who's been using your system recently last | grep -v "^$" | awk '{ print $1 }' | sort -nr | uniq -c # Generat a Random MAC address MAC=`(date; cat /proc/interrupts) | md5sum | sed -r 's/^(.{10}).*$/\1/; s/([0-9a-f]{2})/\1:/g; s/:$//;'` # ping a range of IP addresses nmap -sP 192.168.1.100-254 # Check if system is 32bit or 64bit arch # Find the most recently changed files (recursively) find . -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort # Ask for a password, the passwd-style read -s -p"Password: " USER_PASSWORD_VARIABLE; echo # monitor memory usage watch vmstat -sSM # Content search. ff() { local IFS='|'; grep -rinE "$*" . ; } # List 10 largest directories in current directory du -hs */ | sort -hr | head # Stream YouTube URL directly to MPlayer yt () mplayer -fs -quiet $(youtube-dl -g "$1") # commandlinefu.com by David Winterbottom # List open files that have no links to them on the filesystem lsof +L1 # List of commands you use most often history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head > /tmp/cmds | gnuplot -persist <(echo 'plot "/tmp/cmds" using 1:xticlabels(2) with boxes') # find all active IP addresses in a network nmap -sP 192.168.1.0/24; arp -n | grep "192.168.1.[0-9]* *ether" # Convert all Flac in a directory to Mp3 using maximum quality variable bitrate for file in *.flac; do flac -cd "$file" | lame -q 0 --vbr-new -V 0 - "${file%.flac}.mp3"; done # Convert a Nero Image File to ISO dd bs=1k if=image.nrg of=image.iso skip=300 # Copy with progress rsync --progress file1 file2 # a shell function to print a ruler the width of the terminal window. ruler() { for s in '....^....|' '1234567890'; do w=${#s}; str=$( for (( i=1; $i<=$(( ($COLUMNS + $w) / $w )) ; i=$i+1 )); do echo -n $s; done ); str=$(echo $str | cut -c -$COLUMNS) ; echo $str; done; } # quickest (i blv) way to get the current program name minus the path (BASH) path_stripped_programname="${0##*/}" # A function to output a man page as a pdf file function man2pdf(){ man -t ${1:?Specify man as arg} | ps2pdf -dCompatibility=1.3 - - > ${1}.pdf; } # a trash function for bash trash <file> # Play music from youtube without download wget -q -O - `youtube-dl -b -g $url`| ffmpeg -i - -f mp3 -vn -acodec libmp3lame -| mpg123 - # generate a unique and secure password for every website that you login to sitepass() { echo -n "$@" | md5sum | sha1sum | sha224sum | sha256sum | sha384sum | sha512sum | gzip - | strings -n 1 | tr -d "[:space:]" | tr -s '[:print:]' | tr '!-~' 'P-~!-O' | rev | cut -b 2-11; history -d $(($HISTCMD-1)); } # Replace spaces in filenames with underscores rename -v 's/ /_/g' * # find and delete empty dirs, start in current working dir find . -empty -type d -exec rmdir {} + # Move all images in a directory into a directory hierarchy based on year, month and day based on exif information exiftool '-Directory<DateTimeOriginal' -d %Y/%m/%d dir # Follow tail by name (fix for rolling logs with tail -f) tail -F file # Colored SVN diff svn diff <file> | vim -R - # Find broken symlinks and delete them find -L /path/to/check -type l -delete # Find if the command has an alias type -all command # Show a config file without comments egrep -v "^$|^[[:space:]]*#" /etc/some/file # Download all Delicious bookmarks curl -u username -o bookmarks.xml https://api.del.icio.us/v1/posts/all # beep when a server goes offline while true; do [ "$(ping -c1W1w1 server-or-ip.com | awk '/received/ {print $4}')" != 1 ] && beep; sleep 1; done # Number of open connections per ip. netstat -ntu | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -n # Create a favicon convert -colors 256 -resize 16x16 face.jpg face.ppm && ppmtowinicon -output favicon.ico face.ppm # Quickly generate an MD5 hash for a text string using OpenSSL echo -n 'text to be encrypted' | openssl md5 # commandlinefu.com by David Winterbottom # create dir tree mkdir -p doc/{text/,img/{wallpaper/,photos/}} # Check Ram Speed and Type in Linux sudo dmidecode --type 17 | more # Run any GUI program remotely ssh -fX <user>@<host> <program> # Run the Firefox Profile Manager firefox -no-remote -P # Delete the specified line sed -i 8d ~/.ssh/known_hosts # Backup your hard drive with dd sudo dd if=/dev/sda of=/media/disk/backup/sda.backup # Add forgotten changes to the last git commit git commit --amend # Sort dotted quads sort -nt . -k 1,1 -k 2,2 -k 3,3 -k 4,4 # Press ctrl+r in a bash shell and type a few letters of a previous command ^r in bash begins a reverse-search-history with command completion # Extract audio from a video ffmpeg -i video.avi -f mp3 audio.mp3 # Get Dell Service Tag Number from a Dell Machine sudo dmidecode | grep Serial\ Number | head -n1 # Resume aborted scp file transfers rsync --partial --progress --rsh=ssh SOURCE DESTINATION # Use last argument of last command file !$ # Commandline document conversion with Libreoffice soffice --headless -convert-to odt:"writer8" somefile.docx # HourGlass hourglass(){ trap 'tput cnorm' 0 1 2 15 RETURN;local s=$(($SECONDS +$1));(tput civis;while (($SECONDS<$s));do for f in '|' '\' '-' '/';do echo -n "$f";sleep .2s;echo -n $'\b';done;done;);} # Transfer a file to multiple hosts over ssh pscp -h hosts.txt -l username /etc/hosts /tmp/hosts # Verify/edit bash history command before executing it shopt -s histverify # Resize an image to at least a specific resolution convert -resize '1024x600^' image.jpg small-image.jpg # Print without executing the last command that starts with... !ssh:p # Query well known ports list getent services <<service>> # Create .pdf from .doc oowriter -pt pdf your_word_file.doc # Diff XML files diffxml() { diff -wb <(xmllint --format "$1") <(xmllint --format "$2"); } # Gets the english pronunciation of a phrase say() { mplayer "http://translate.google.com/translate_tts?q=$1"; } # What is the use of this switch ? manswitch () { man $1 | less -p "^ +$2"; } # Save the list of all available commands in your box to a file compgen -c | sort -u > commands # commandlinefu.com by David Winterbottom # clear screen, keep prompt at eye-level (faster than clear(1), tput cl, etc.) cls(){ printf "\33[2J";} or, if no printf, cat >cls;<ctrl-v><ctrl+[>[2J<enter><ctrl+d> cls(){ cat cls;} # Down for everyone or just me? down4me() { wget -qO - "http://www.downforeveryoneorjustme.com/$1" | sed '/just you/!d;s/<[^>]*>//g' ; } # Run a program transparently, but print a stack trace if it fails gdb -batch -ex "run" -ex "bt" ${my_program} 2>&1 | grep -v ^"No stack."$ # Compare copies of a file with md5 cmp file1 file2 # backup delicious bookmarks curl --user login:password -o DeliciousBookmarks.xml -O 'https://api.del.icio.us/v1/posts/all' # Rename all .jpeg and .JPG files to have .jpg extension rename 's/\.jpe?g$/.jpg/i' * # pretend to be busy in office to enjoy a cup of coffee j=0;while true; do let j=$j+1; for i in $(seq 0 20 100); do echo $i;sleep 1; done | dialog --gauge "Install part $j : `sed $(perl -e "print int rand(99999)")"q;d" /usr/share/dict/words`" 6 40;done # Generate a Random MAC address openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//' # Purge configuration files of removed packages on debian based systems aptitude purge '~c' # Get all links of a website lynx -dump http://www.domain.com | awk '/http/{print $2}' # Find all active ip's in a subnet sudo arp-scan -I eth0 192.168.1.0/24 # Display BIOS Information dmidecode -t bios # Disassemble some shell code echo -ne "<shellcode>" | x86dis -e 0 -s intel # make, or run a script, everytime a file in a directory is modified while true; do inotifywait -r -e MODIFY dir/ && make; done; # kill process by name pkill -x firefox # ignore the .svn directory in filename completion export FIGNORE=.svn # Ping scanning without nmap for i in {1..254}; do ping -c 1 -W 1 10.1.1.$i | grep 'from'; done # Working random fact generator wget randomfunfacts.com -O - 2>/dev/null | grep \<strong\> | sed "s;^.*<i>\(.*\)</i>.*$;\1;" # Remote backups with tar over ssh tar jcpf - [sourceDirs] |ssh user@host "cat > /path/to/backup/backupfile.tar.bz2" # Pronounce an English word using Dictionary.com pronounce(){ wget -qO- $(wget -qO- "http://dictionary.reference.com/browse/$@" | grep 'soundUrl' | head -n 1 | sed 's|.*soundUrl=\([^&]*\)&.*|\1|' | sed 's/%3A/:/g;s/%2F/\//g') | mpg123 -; } # Make ISO image of a folder mkisofs -J -allow-lowercase -R -V "OpenCD8806" -iso-level 4 -o OpenCD.iso ~/OpenCD # Change pidgin status purple-remote "setstatus?status=away&message=AFK" # Grep by paragraph instead of by line. grepp() { [ $# -eq 1 ] && perl -00ne "print if /$1/i" || perl -00ne "print if /$1/i" < "$2";} # Vim: Switch from Horizontal split to Vertical split ^W-L # Numbers guessing game A=1;B=100;X=0;C=0;N=$[$RANDOM%$B+1];until [ $X -eq $N ];do read -p "N between $A and $B. Guess? " X;C=$(($C+1));A=$(($X<$N?$X:$A));B=$(($X>$N?$X:$B));done;echo "Took you $C tries, Einstein"; # commandlinefu.com by David Winterbottom # generate random password pwgen -Bs 10 1 # Change user, assume environment, stay in current dir su -- user # Testing hard disk reading speed hdparm -t /dev/sda # Identify differences between directories (possibly on different servers) diff <(ssh server01 'cd config; find . -type f -exec md5sum {} \;| sort -k 2') <(ssh server02 'cd config;find . -type f -exec md5sum {} \;| sort -k 2') # How fast is the connexion to a URL, some stats from curl URL="http://www.google.com";curl -L --w "$URL\nDNS %{time_namelookup}s conn %{time_connect}s time %{time_total}s\nSpeed %{speed_download}bps Size %{size_download}bytes\n" -o/dev/null -s $URL # move a lot of files over ssh rsync -az /home/user/test user@sshServer:/tmp/ # List programs with open ports and connections lsof -i # Show git branches by date - useful for showing active branches for k in `git branch|perl -pe s/^..//`;do echo -e `git show --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k|head -n 1`\\t$k;done|sort -r # disable history for current shell session unset HISTFILE # Share a 'screen'-session screen -x # Show all detected mountable Drives/Partitions/BlockDevices hwinfo --block --short # Monitor TCP opened connections watch -n 1 "netstat -tpanl | grep ESTABLISHED" # Fibonacci numbers with awk seq 50| awk 'BEGIN {a=1; b=1} {print a; c=a+b; a=b; b=c}' # Read the output of a command into the buffer in vim :r !command # Calculates the date 2 weeks ago from Saturday the specified format. date -d '2 weeks ago Saturday' +%Y-%m-%d # Find broken symlinks find . -type l ! -exec test -e {} \; -print # Add your public SSH key to a server in one command cat .ssh/id_rsa.pub | ssh hostname 'cat >> .ssh/authorized_keys' # ssh tunnel with auto reconnect ability while [ ! -f /tmp/stop ]; do ssh -o ExitOnForwardFailure=yes -R 2222:localhost:22 target "while nc -zv localhost 2222; do sleep 5; done"; sleep 5;done # find process associated with a port fuser [portnumber]/[proto] # cycle through a 256 colour palette yes "$(seq 232 255;seq 254 -1 233)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done # scping files with streamlines compression (tar gzip) tar czv file1 file2 folder1 | ssh user@server tar zxv -C /destination # add all files not under version control to repository svn status |grep '\?' |awk '{print $2}'| xargs svn add # Count files beneath current directory (including subfolders) find . -type f | wc -l # Discover the process start time ps -eo pid,lstart,cmd # Run the built in PHP-server in current folder php -S 127.0.0.1:8080 # commandlinefu.com by David Winterbottom # Press enter and take a WebCam picture. read && ffmpeg -y -r 1 -t 3 -f video4linux2 -vframes 1 -s sxga -i /dev/video0 ~/webcam-$(date +%m_%d_%Y_%H_%M).jpeg # Automatically find and re-attach to a detached screen session screen -D -R # ping as traceroute for i in {1..30}; do ping -t $i -c 1 google.com; done | grep "Time to live exceeded" # what model of computer I'm using? sudo dmidecode | grep Product # tail a log over ssh ssh -t remotebox "tail -f /var/log/remote.log" # du with colored bar graph t=$(df|awk 'NR!=1{sum+=$2}END{print sum}');sudo du / --max-depth=1|sed '$d'|sort -rn -k1 | awk -v t=$t 'OFMT="%d" {M=64; for (a=0;a<$1;a++){if (a>c){c=a}}br=a/c;b=M*br;for(x=0;x<b;x++){printf "\033[1;31m" "|" "\033[0m"}print " "$2" "(a/t*100)"% total"}' # sort the output of the 'du' command by largest first, using human readable output. du -h --max-depth=1 |sort -rh # Target a specific column for pattern substitution awk '{gsub("foo","bar",$5)}1' file # Discovering all open files/dirs underneath a directory lsof +D <dirname> # Substrings a variable var='123456789'; echo ${var:<start_pos>:<offset>} # Check syntax for all PHP files in the current directory and all subdirectories find . -name \*.php -exec php -l "{}" \; # "Clone" a list of installed packages from one Debian/Ubuntu Server to another apt-get install `ssh root@host_you_want_to_clone "dpkg -l | grep ii" | awk '{print $2}'` # Timer with sound alarm sleep 3s && espeak "wake up, you bastard" 2>/dev/null # A formatting test for David Winterbottom: improving commandlinefu for submitters echo "?????, these are the umlauted vowels I sing to you. Oh, and sometimes ?, but I don't sing that one cause it doesn't rhyme." # Random unsigned integer echo $(openssl rand 4 | od -DAn) # kill all process that belongs to you kill -9 -1 # Continue a current job in the background <ctrl+z> bg # Send email with curl and gmail curl -n --ssl-reqd --mail-from "<user@gmail.com>" --mail-rcpt "<user@server.tld>" --url smtps://smtp.gmail.com:465 -T file.txt # translates acronyms for you wtf is <acronym> # Create an animated gif from a Youtube video url=http://www.youtube.com/watch?v=V5bYDhZBFLA; youtube-dl -b $url; mplayer $(ls ${url##*=}*| tail -n1) -ss 00:57 -endpos 10 -vo gif89a:fps=5:output=output.gif -vf scale=400:300 -nosound # Print just line 4 from a textfile sed -n '4{p;q}' # Print just line 4 from a textfile sed -n '4p' # List bash functions defined in .bash_profile or .bashrc compgen -A function # Start a HTTP server which serves Python docs pydoc -p 8888 & gnome-open http://localhost:8888 # Do some learning... ls /usr/bin | xargs whatis | grep -v nothing | less # commandlinefu.com by David Winterbottom # Insert the last argument of the previous command <ALT> . # Typing the current date ( or any string ) via a shortcut as if the keys had been actually typed with the hardware keyboard in any application. xvkbd -xsendevent -text $(date +%Y%m%d) # recursive search and replace old with new string, inside files find . -type f -exec sed -i s/oldstring/newstring/g {} + # Clean your broken terminal stty sane # geoip information curl -s "http://www.geody.com/geoip.php?ip=$(curl -s icanhazip.com)" | sed '/^IP:/!d;s/<[^>][^>]*>//g' # Backup a local drive into a file on the remote host via ssh dd if=/dev/sda | ssh user@server 'dd of=sda.img' # Sort all running processes by their memory & CPU usage ps aux --sort=%mem,%cpu # Kill processes that have been running for more than a week find /proc -user myuser -maxdepth 1 -type d -mtime +7 -exec basename {} \; | xargs kill -9 # Save current layout of top <Shift + W> # Show directories in the PATH, one per line echo $PATH | tr \: \\n # An easter egg built into python to give you the Zen of Python python -c 'import this' # Add prefix onto filenames rename 's/^/prefix/' * # Stream YouTube URL directly to mplayer id="dMH0bHeiRNg";mplayer -fs http://youtube.com/get_video.php?video_id=$id\&t=$(curl -s http://www.youtube.com/watch?v=$id | sed -n 's/.*, "t": "\([^"]*\)", .*/\1/p') # Get all IPs via ifconfig ifconfig -a | perl -nle'/(\d+\.\d+\.\d+\.\d+)/ && print $1' # Pick a random line from a file shuf -n1 file.txt # Get all these commands in a text file with description. for x in `jot - 0 2400 25`; do curl "http://www.commandlinefu.com/commands/browse/sort-by-votes/plaintext/$x" ; done > commandlinefu.txt # Stripping ^M at end of each line for files dos2unix <filenames> # Use Cygwin to talk to the Windows clipboard cat /dev/clipboard; $(somecommand) > /dev/clipboard # Backup files incremental with rsync to a NTFS-Partition rsync -rtvu --modify-window=1 --progress /media/SOURCE/ /media/TARGET/ # Find removed files still in use via /proc find -L /proc/*/fd -links 0 2>/dev/null # Convert "man page" to text file man ls | col -b > ~/Desktop/man_ls.txt # Connect to google talk through ssh by setting your IM client to use the localhost 5432 port ssh -f -N -L 5432:talk.google.com:5222 user@home.network.com # Find last reboot time who -b # Get video information with ffmpeg ffmpeg -i filename.flv # for all flv files in a dir, grab the first frame and make a jpg. for f in *.flv; do ffmpeg -y -i "$f" -f image2 -ss 10 -vframes 1 -an "${f%.flv}.jpg"; done # commandlinefu.com by David Winterbottom # Purge configuration files of removed packages on debian based systems sudo aptitude purge `dpkg --get-selections | grep deinstall | awk '{print $1}'` # from within vi, pipe a chunk of lines to a command line and replace the chunk with the result !}sort # Using awk to sum/count a column of numbers. cat count.txt | awk '{ sum+=$1} END {print sum}' # Append stdout and stderr to a file, and print stderr to the screen [bash] somecommand 2>&1 >> logfile | tee -a logfile # quickly change all .html extensions on files in folder to .htm for i in *.html ; do mv $i ${i%.html}.htm ; done # Grep for word in directory (recursive) grep --color=auto -iRnH "$search_word" $directory # Another Curl your IP command curl -s http://checkip.dyndns.org | sed 's/[a-zA-Z<>/ :]//g' # count IPv4 connections per IP netstat -anp |grep 'tcp\|udp' | awk '{print $5}' | sed s/::ffff:// | cut -d: -f1 | sort | uniq -c | sort -n # pattern match in awk - no grep awk '/pattern1/ && /pattern2/ && !/pattern3/ {print}' # Optimal way of deleting huge numbers of files find /path/to/dir -type f -print0 | xargs -0 rm # Convert Shell Text to Upper/Lower Case ALT-U / ALT-L # grep processes list avoiding the grep itself ps axu | grep [a]pache2 # repeat a command every one second watch -n 1 "do foo" # Swap the two last arguments of the current command line <ctrl+e> <esc+t> # Daemonize nc - Transmit a file like a http server while ( nc -l 80 < /file.htm > : ) ; do : ; done & # Redirect tar extract to another directory tar xfz filename.tar.gz -C PathToDirectory # Run a command when a file is changed while inotifywait -e modify /tmp/myfile; do firefox; done # Find Malware in the current and sub directories by MD5 hashes IFS=$'\n' && for f in `find . -type f -exec md5sum "{}" \;`; do echo $f | sed -r 's/^[^ ]+/Checking:/'; echo $f | cut -f1 -d' ' | netcat hash.cymru.com 43 ; done # Execute a command, convert output to .png file, upload file to imgur.com, then returning the address of the .png. imgur(){ $*|convert label:@- png:-|curl -F "image=@-" -F "key=1913b4ac473c692372d108209958fd15" http://api.imgur.com/2/upload.xml|grep -Eo "<original>(.)*</original>" | grep -Eo "http://i.imgur.com/[^<]*";} # RDP through SSH tunnel ssh -f -L3389:<RDP_HOST>:3389 <SSH_PROXY> "sleep 10" && rdesktop -T'<WINDOW_TITLE>' -uAdministrator -g800x600 -a8 -rsound:off -rclipboard:PRIMARYCLIPBOARD -5 localhost # Rapidly invoke an editor to write a long, complex, or tricky command <ESC> v or ctrl-x ctrl-e # Remote screenshot ssh user@remote-host "DISPLAY=:0.0 import -window root -format png -"|display -format png - # List your MACs address lsmac() { ifconfig -a | sed '/eth\|wl/!d;s/ Link.*HWaddr//' ; } # ssh to machine behind shared NAT ssh -NR 0.0.0.0:2222:127.0.0.1:22 user@jump.host.com # Countdown Clock MIN=10;for ((i=MIN*60;i>=0;i--));do echo -ne "\r$(date -d"0+$i sec" +%H:%M:%S)";sleep 1;done # commandlinefu.com by David Winterbottom # Look up a unicode character by name egrep -i "^[0-9a-f]{4,} .*$*" $(locate CharName.pm) | while read h d; do /usr/bin/printf "\U$(printf "%08x" 0x$h)\tU+%s\t%s\n" $h "$d"; done # Generate MD5 hash for a string md5sum <<<"test" # Ask user to confirm Confirm() { read -sn 1 -p "$1 [Y/N]? "; [[ $REPLY = [Yy] ]]; } # Make a file not writable / immutable by root sudo chattr +i <file> # prevent accidents and test your command with echo echo rm *.txt # Get My Public IP Address curl ifconfig.me # Empty a file :> file # Emptying a text file in one shot :%d # computes the most frequent used words of a text file cat WAR_AND_PEACE_By_LeoTolstoi.txt | tr -cs "[:alnum:]" "\n"| tr "[:lower:]" "[:upper:]" | awk '{h[$1]++}END{for (i in h){print h[i]" "i}}'|sort -nr | cat -n | head -n 30 # Quick HTML image gallery from folder contents find . -iname '*.jpg' -exec echo '<img src="{}">' \; > gallery.html # check the status of 'dd' in progress (OS X) killall -INFO dd # Restrict the bandwidth for the SCP command scp -l10 pippo@serverciccio:/home/zutaniddu/* . # List all authors of a particular git project git log --format='%aN' | sort -u # live ssh network throughput test pv /dev/zero|ssh $host 'cat > /dev/null' # Carriage return for reprinting on the same line while true; do echo -ne "$(date)\r"; sleep 1; done # What is my ip? curl http://www.whatismyip.org/ # Empty a file truncate -s0 file # List Network Tools in Linux apropos network |more # Function that outputs dots every second until command completes sleeper(){ while `ps -p $1 &>/dev/null`; do echo -n "${2:-.}"; sleep ${3:-1}; done; }; export -f sleeper # Search back through previous commands Ctrl-R <search-text> # Print text string vertically, one character per line. echo "vertical text" | grep -o '.' # Find running binary executables that were not installed using dpkg cat /var/lib/dpkg/info/*.list > /tmp/listin ; ls /proc/*/exe |xargs -l readlink | grep -xvFf /tmp/listin; rm /tmp/listin # Cleanup firefox's database. pgrep -u `id -u` firefox-bin || find ~/.mozilla/firefox -name '*.sqlite'|(while read -e f; do echo 'vacuum;'|sqlite3 "$f" ; done) # vim easter egg $ vim ... :help 42 # Find the process you are looking for minus the grepped one pgrep command_name # commandlinefu.com by David Winterbottom # VIM: Replace a string with an incrementing number between marks 'a and 'b (eg, convert string ZZZZ to 1, 2, 3, ...) :let i=0 | 'a,'bg/ZZZZ/s/ZZZZ/\=i/ | let i=i+1 # Get the canonical, absolute path given a relative and/or noncanonical path readlink -f ../super/symlink_bon/ahoy # Create a Multi-Part Archive Without Proprietary Junkware tar czv Pictures | split -d -a 3 -b 16M - pics.tar.gz. # wrap long lines of a text fold -s -w 90 file.txt # Display last exit status of a command echo $? # Enable ** to expand files recursively (>=bash-4.0) shopt -s globstar # Command Line to Get the Stock Quote via Yahoo curl -s 'http://download.finance.yahoo.com/d/quotes.csv?s=csco&f=l1' # Convert camelCase to underscores (camel_case) sed -r 's/([a-z]+)([A-Z][a-z]+)/\1_\l\2/g' file.txt # Set your profile so that you resume or start a screen session on login echo "screen -DR" >> ~/.bash_profile # play high-res video files on a slow processor mplayer -framedrop -vfm ffmpeg -lavdopts lowres=1:fast:skiploopfilter=all # Grep colorized grep -i --color=auto # List your largest installed packages. wajig large # Monitor dynamic changes in the dmesg log. watch "dmesg |tail -15" # Generate a list of installed packages on Debian-based systems dpkg --get-selections > LIST_FILE # find the process that is using a certain port e.g. port 3000 lsof -P | grep ':3000' # Pause Current Thread ctrl-z # Merge *.pdf files gs -q -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=out.pdf `ls *.pdf` # Convert .wma files to .ogg with ffmpeg find -name '*wma' -exec ffmpeg -i {} -acodec vorbis -ab 128k {}.ogg \; # Generate Random Passwords < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c6 # Grep without having it show its own process in the results ps aux | grep "[s]ome_text" # Find recursively, from current directory down, files and directories whose names contain single or multiple whitespaces and replace each such occurrence with a single underscore. find ./ -name '*' -exec rename 's/\s+/_/g' {} \; # Files extension change rename .oldextension .newextension *.oldextension # archive all files containing local changes (svn) svn st | cut -c 8- | sed 's/^/\"/;s/$/\"/' | xargs tar -czvf ../backup.tgz # Just run it ;) echo SSBMb3ZlIFlvdQo= | base64 -d # Show which programs are listening on TCP and UDP ports netstat -plunt # commandlinefu.com by David Winterbottom # vim easter egg $ vim ... :help 42 # copy with progress bar - rsync rsync -rv <src> <dst> --progress # find and replace tabs for spaces within files recursively find ./ -type f -exec sed -i 's/\t/ /g' {} \; # List your MACs address cat /sys/class/net/eth0/address # diff current vi buffer edits against original file :w !diff -u % - # Send data securly over the net. cat /etc/passwd | openssl aes-256-cbc -a -e -pass pass:password | netcat -l -p 8080 # pretend to be busy in office to enjoy a cup of coffee for i in `seq 0 100`;do timeout 6 dialog --gauge "Install..." 6 40 "$i";done # JSON processing with Python curl -s "http://feeds.delicious.com/v2/json?count=5" | python -m json.tool | less -R # Get your external IP address without curl wget -qO- icanhazip.com # cycle through a 256 colour palette yes "$(seq 1 255)" | while read i; do printf "\x1b[48;5;${i}m\n"; sleep .01; done # Find all files larger than 500M and less than 1GB find / -type f -size +500M -size -1G # extract email adresses from some file (or any other pattern) grep -Eio '([[:alnum:]_.-]+@[[:alnum:]_.-]+?\.[[:alpha:].]{2,6})' # command line calculator calc(){ awk "BEGIN{ print $* }" ;} # Plays Music from SomaFM read -p "Which station? "; mplayer --reallyquiet -vo none -ao sdl http://somafm.com/startstream=${REPLY}.pls # infile search and replace on N files (including backup of the files) perl -pi.bk -e's/foo/bar/g' file1 file2 fileN # Create an SSH SOCKS proxy server on localhost:8000 that will re-start itself if something breaks the connection temporarily autossh -f -M 20000 -D 8000 somehost -N # Delete all files found in directory A from directory B for file in <directory A>/*; do rm <directory B>/`basename $file`; done # Block an IP address from connecting to a server iptables -A INPUT -s 222.35.138.25/32 -j DROP # Record microphone input and output to date stamped mp3 file arecord -q -f cd -r 44100 -c2 -t raw | lame -S -x -h -b 128 - `date +%Y%m%d%H%M`.mp3 # loop over a set of items that contain spaces ls | while read ITEM; do echo "$ITEM"; done # Extract tar content without leading parent directory tar -xaf archive.tar.gz --strip-components=1 # Function to split a string into an array read -a ARR <<<'world domination now!'; echo ${ARR[2]}; # Update twitter via curl curl -u user -d status="Tweeting from the shell" http://twitter.com/statuses/update.xml # Check your spelling aspell -a <<< '<WORDS>' # View ~/.ssh/known_hosts key information ssh-keygen -l -f ~/.ssh/known_hosts # commandlinefu.com by David Winterbottom # Binary digits Matrix effect perl -e '$|++; while (1) { print " " x (rand(35) + 1), int(rand(2)) }' # print multiplication formulas seq 9 | sed 'H;g' | awk -v RS='' '{for(i=1;i<=NF;i++)printf("%dx%d=%d%s", i, NR, i*NR, i==NR?"\n":"\t")}' # Rapidly invoke an editor to write a long, complex, or tricky command <ESC> v # add the result of a command into vi !!command # Super Speedy Hexadecimal or Octal Calculations and Conversions to Decimal. echo "$(( 0x10 )) - $(( 010 )) = $(( 0x10 - 010 ))" # bash shortcut: !$ !^ !* !:3 !:h and !:t echo foo bar foobar barfoo && echo !$ !^ !:3 !* && echo /usr/bin/foobar&& echo !$:h !$:t # Traceroute w/TCP to get through firewalls. tcptraceroute www.google.com # benchmark web server with apache benchmarking tool ab -n 9000 -c 900 localhost:8080/index.php # move a lot of files over ssh tar -cf - /home/user/test | gzip -c | ssh user@sshServer 'cd /tmp; tar xfz -' # sends a postscript file to a postscript printer using netcat cat my.ps | nc -q 1 hp4550.mynet.xx 9100 # the same as [Esc] in vim Ctrl + [ # open a screenshot of a remote desktop via ssh xloadimage <(ssh USER@HOSTNAME DISPLAY=:0.0 import -window root png:-) # Displays the attempted user name, ip address, and time of SSH failed logins on Debian machines awk '/sshd/ && /Failed/ {gsub(/invalid user/,""); printf "%-12s %-16s %s-%s-%s\n", $9, $11, $1, $2, $3}' /var/log/auth.log # Create a bunch of dummy files for testing touch {1..10}.txt # Quickly get summary of sizes for files and folders du -sh * # Temporarily ignore mismatched SSH host key ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no username@host # back up your commandlinefu contributed commands curl http://www.commandlinefu.com/commands/by/<your username>/rss|gzip ->commandlinefu-contribs-backup-$(date +%Y-%m-%d-%H.%M.%S).rss.gz # Find the package a command belongs to on Debian dpkg -S $( which ls ) # Replace spaces in filenames with underscorees ls | while read f; do mv "$f" "${f// /_}";done # Google text-to-speech in mp3 format say(){ mplayer -user-agent Mozilla "http://translate.google.com/translate_tts?tl=en&q=$(echo $* | sed 's#\ #\+#g')" > /dev/null 2>&1 ; } # Terminal redirection script /dev/null | tee /dev/pts/3 # Converts to PDF all the OpenOffice.org files in the directory for i in $(ls *.od{tp}); do unoconv -f pdf $i; done # Print info about your real user. who loves mum # Update twitter via curl (and also set the "from" bit) curl -u twitter-username -d status="Hello World, Twitter!" -d source="cURL" http://twitter.com/statuses/update.xml # Escape potential tarbombs atb() { l=$(tar tf $1); if [ $(echo "$l" | wc -l) -eq $(echo "$l" | grep $(echo "$l" | head -n1) | wc -l) ]; then tar xf $1; else mkdir ${1%.tar.gz} && tar xf $1 -C ${1%.tar.gz}; fi ;}