Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Universal configuration monitoring and system of record for IT.
Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Top Tags

Hide

Functions

All commands from sorted by
Terminal - All commands - 12,062 results
DATE=`date +"%H:%M" --date '-1 min'`; egrep "\ $DATE\:..\ " /var/log/dhcpd.log |awk '/DHCPREQUEST/ {split($3,t,":"); printf("%02d:%02d\n",t[1],t[2]);}' |uniq -c;
hexdump -C file
(date "+%d-%m-%Y %H:%M:%S";curl -s --interface lo:1 ifconfig.me| xargs -t geoiplookup 2>&1)|sed -e 's/geoiplookup/IP:/g' -e 's/GeoIP Country Edition/Country/g'|tr -s "\n" " "|sed 'a\ '
2013-10-07 15:28:55
User: guerito
Functions: date sed tr xargs
0

My first command :) I made this command to log public addresses of a virtual interface who connects random VPN servers around the world.

command_line 2>&1 | tee -a output_file
git log --graph --decorate --pretty=oneline --abbrev-commit
for f in input/*; do BN=$(basename "$f"); ffmpeg -i "$f" -vn "temp/$BN.flac"...
2015-03-01 02:48:19
Functions: basename
0

Full command:

for f in input/*; do BN=$(basename "$f"); ffmpeg -i "$f" -vn "temp/$BN.flac"; sox "temp/$BN.flac" "temp/$BN-cleaned.flac" noisered profile 0.3; ffmpeg -i "$f" -vcodec copy -an "temp/$BN-na.mp4"; ffmpeg -i "temp/$BN-na.mp4" -i "temp/$BN-cleaned.flac" "output/$BN"; done

This was over the 255 character limit and I didn't feel like deliberately obfuscating it.

1. Create 'input', 'output' and 'temp' directories.

2. Place the files that you want to remove the hiss/static/general noise from in the input directory.

3. Generate a noise reduction profile with sox using 'sox an_input_file.mp4 -n trim x y noiseprof profile', where x and y indicates a range in seconds that only the sound you want to eliminate is present in.

4. Run the command.

files=(/usr/share/cowsay/cows/*); cowsay -f `echo ${files[$((RANDOM%${#files}))]}` `fortune` | toilet -F gay -f term
2011-02-20 05:55:33
User: lkjoel
-1

This command will tell you your fortune, with a nice Ascii Art animal, and it is colorized

You need cowsay, toilet and fortune installed.

On Ubuntu this is:

sudo apt-get install cowsay toilet fortune

Debian:

aptitude install cowsay toilet fortune
#!/bin/bash find | grep -P -v "(class)|(zip)|(png)|(gz)|(gif)|(jpeg)|(jpg)" | xargs -I @ grep -H $1 @
2011-10-14 13:28:49
User: kirdie
Functions: find grep xargs
Tags: find grep
-1

calls grep on all non-binary files returned by find on its current working directory

if [[ $EUID -ne 0 ]]; then echo 'Root permissions required! Exiting.'; exit; fi
2011-11-19 23:06:52
User: lordtoran
Functions: echo
-1

This example code is intended to be used as a root permissions check in a script. It makes use of the $EUID (effective user ID) environment variable which is fully su- and sudo-safe.

cd /System/Library/LaunchDaemons; sudo launchctl load -w com.apple.metadata.mds.plist
for i in `cat /proc/mounts | awk '{print $2}' | grep ${CDIR} |sort -r` ; do umount $i; done
tar -cvf /path/dir.tar /path/dir* --exclude "/path/dir/name" --exclude "/path/dir/opt"
get-ipsw(){ curl -s -L http://phobos.apple.com/version | sed -rn "s|[\t ]*<string>(http://appldnld\.apple\.com\.edgesuite\.net/content\.info\.apple\.com/iPhone[0-9]?/[^/]*/$1$2_$3_[A-Z0-9a-z]*_Restore\.ipsw)</string>|\1|p" | uniq; }
2010-06-22 02:34:15
User: matthewbauer
Functions: get sed
-1

Usage:

get-ipsw device-name generation-string firmware-version

For example:

get-ipsw iPod 2,1 4.0

Different generation strings:

iPhone 3G: iPhone 1,2

iPhone 3GS: iPhone 2,1

iPod touch 2G: iPod 2,1

iPod touch 3G: iPod 3,1

This can be used with idevicerestore (I haven't tried it though).

http://github.com/posixninja/idevicerestore

Based on:

http://www.tuaw.com/2010/06/21/ios-4-0-firmware-release-expected-momentarily-quick-terminal-ti/

file <filename>
2011-11-19 23:39:29
User: lordtoran
Functions: file
-1

Some shell newbies don't know this very handy file management related command so I decided to include it here.

You need to have the "file" package installed.

ls | xargs -I{} du -sh {}
<CTRL+Z>; fg
2014-04-06 14:21:08
User: kbrotheridge
-1

Saves opening another console terminal (eg. CTRL+ALT+F[n]) or opening another remote terminal.

Ctrl+Z pauses the current task and pushed it to the background, leaving you with a command prompt for those "Oh crap I forgot to change xyz before I ran that and it'll take forever if I Ctrl+C and start again..." situations. Typing 'fg' (shorthand for foreground, that's how I remember it) will resume the paused task.

if [ "$(ping -q -c1 google.com)" ];then wget -mnd -q http://www.google.com/intl/en_ALL/images/logo.gif ;fi
2010-03-23 04:15:03
User: alf
Functions: wget
-1

Bash scrip to test if a server is up, you can use this before wget'ing a file to make sure a blank one isn't downloaded.

for i in *.wma; do mplayer -vo null -vc dummy -af resample=44100 -ao pcm:waveheader:file="${i%.wma}.wav" "$i" ; done
echo $(( $( date +%s ) - $( stat -c %Y * | sort -nr | head -n 1 ) ))
alias oports="echo -e "User:\tCommand:\tPort:\n----------------------------" && lsof -i 4 -P -n | awk '/LISTEN/ {print $3, $1, $9}' | sed 's/ [a-z0-9\.\*]*:/ /' | sort -u -k 3 -n | xargs printf '%-10s %-10s %-10s\n'"
wget http://somesite.com/somestream.pls; cvlc somestream.pls&sleep 5; rm somestream.pls*
2011-08-04 19:24:18
User: tomjrace
Functions: rm wget
-1

I wanted to play a song from the shell and get the shell back, I also dont want to store the file if it is not needed.

edit, not sure if I need to mention it... killall vlc to stop it

slow () { [ -n $1 ] && while kill -STOP $1; do sleep 1; kill -CONT $1; sleep 1; done }
2012-05-16 19:16:50
User: makidoko
Functions: kill sleep
Tags: io
-1

compare to alternative :

- directly tests the -STOP of the process to continue or stop loop,

- background operator should be set (or not) at the call of the function

For extension i suggest a slowPID() based on kill like above and a slowCMD based on killall.

PLAYLIST=$(ls -1) ; mpg123 -C $PLAYLIST
2009-03-19 17:20:28
Functions: ls mpg123
-1

On my music directory, I create variable that contains all mp3s files, then I play them with mpg123. -C options enable terminal control key, s for stop, p for pause, f for forward to next song.

find . -type d | tac | xargs rmdir 2> /dev/null
2010-03-23 11:54:38
User: drmaciver
Functions: find rmdir tac xargs
-1

Remove all empty directories below the current directory. If directories become empty as the results of this, remove those too.

host foo.com|grep " has address "|cut -d" " -f4
2010-10-29 17:01:37
User: dinomite
Functions: cut grep host
-1

Get just the IP address for a given hostname. For best results, make this a function in your shell rc file so that it can be used for things like traceroute:

Titus:~$ traceroute `getip foo.com`

traceroute to 64.94.125.138 (64.94.125.138), 64 hops max, 52 byte packets