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

May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!
Hide

Top Tags

Hide

Functions

Hide

Credits

All commands from sorted by
Terminal - All commands - 12,162 results
exec 5<>/dev/tcp/time.nist.gov/13; cat <&5 & cat >&5; exec 5>&-
2015-07-30 21:12:38
User: tyzbit
Functions: cat exec
Tags: bash Linux unix
9

Ever needed to test firewalls but didn't have netcat, telnet or even FTP?

Enter /dev/tcp, your new best friend. /dev/tcp/(hostname)/(port) is a bash builtin that bash can use to open connections to TCP and UDP ports.

This one-liner opens a connection on a port to a server and lets you read and write to it from the terminal.

How it works:

First, exec sets up a redirect for /dev/tcp/$server/$host to file descriptor 5.

Then, as per some excellent feedback from @flatcap, we launch a redirect from file descriptor 5 to STDOUT and send that to the background (which is what causes the PID to be printed when the commands are run), and then redirect STDIN to file descriptor 5 with the second cat.

Finally, when the second cat dies (the connection is closed), we clean up the file descriptor with 'exec 5>&-'.

It can be used to test FTP, HTTP, NTP, or can connect to netcat listening on a port (makes for a simple chat client!)

Replace /tcp/ with /udp/ to use UDP instead.

phpcs --no-colors --standard=WordPress-Core -s -- $( find . -name '*.php' ) | ack -o '(?<=\()\w+(\.\w+)+(?=\)$)' | sort | uniq -c | sort -nr
cd $JAVA_HOME && java -Xbootclasspath/a:jre/lib/deploy.jar -Djava.locale.providers=HOST,JRE,SPI -Xdock:name="Java Control Panel" com.sun.deploy.panel.ControlPanel
2015-07-30 19:24:33
Functions: cd
Tags: java jre
0

Useful if you need to make changes to the JVM on, say, 1.7 when your default installed is 1.8.

export JAVA_HOME=$(/usr/libexec/java_home -v 1.7)
2015-07-30 18:50:51
Functions: export
Tags: version java
0

Useful if you have multiple versions of Java installed. The 1.7 pkg for Mac doesn't tell you anything about where it's putting stuff.

grep -nrHi generate.*sh .i3/
2015-07-28 16:22:55
User: deterenkelt
Functions: grep
-2

Performs a case-insensitive (-i) recursive search (-r) and prints not only the match, but also the filename (-H) in which the match was found and corresponding line number (-n). All this is highlighted with different colours.

Requires GNU grep (v.2.21 was used for this example)

pyt() { youtube-dl -q --max-downloads 1 --no-playlist --default-search ${2:-ytsearch} "$1" -o - | mplayer -vo null /dev/fd/3 3<&0 </dev/tty; }
2015-07-27 15:19:59
User: snipertyler
3

Same as other command, however uses youtube-dl internal search (thanks to qoxxxx mentioning this)

It does however seem to be a little buggy and youtube-dl crashes sometimes.

##

pyt 'Stairway to heaven - Led Zeppelin'

pyt 'brain damage - Pink Floyd'

No web browser or even X needed. Just a cli and internet connection!

mplayer is pauseable and can skip ahead

This may break if youtube changes their search html.

ir() { perl -pne 's/(.)(.*)/\[\1]\2/' <<< "$@" ;}
2015-07-25 14:13:33
User: bartonski
Functions: perl
Tags: ps
0

Note that `grep "$(ir foo)"` really doesn't save any typing, but wrapping this inside a second shell function will:

psg() { grep "$(ir \"$@\")" ;}
mojo get <URL> 'a[href]' attr href
sed -i.$(date +%F@%T) 's/^LogLevel warn/LogLevel debug/g' httpd.conf
2015-07-22 14:47:26
User: zlemini
Functions: date sed
1
ls

httpd.conf httpd.conf.2015-07-22@14:43:20

pyt() { id=$(curl -s 'https://www.youtube.com/results?search_query='$(tr \ + <<<"$1") | grep -om3 '"[[:alnum:]]\{11\}"' | awk NR==3 | tr -d \"); youtube-dl -q 'https://www.youtube.com/watch?v='"$id" -o - | mplayer -vo null /dev/fd/3 3<&0 </dev/tty; }
2015-07-20 05:30:27
User: snipertyler
Functions: awk grep tr
3

pyt 'Stairway to heaven - Led Zeppelin'

pyt 'brain damage - Pink Floyd'

No web browser or even X needed. Just a cli and internet connection!

mplayer is pauseable and can skip ahead

This may break if youtube changes their search html.

grep -v -e '^$' -e '^[#\[]' -e '\/' some_file
2015-07-17 21:28:14
User: abhikeny
Functions: grep
Tags: grep no-match
0

Grep for:

>> non-empty lines : denoted by -v -e '^$'

>> that do not start with # (comments) or : denoted by -v -e '^[#\]'

>> and does not include : denoted by -v -e '\'

(Note that we use -v only once)

ran() { [ $((RANDOM%100)) -lt "$1" ] && shift && "$@"; }
2015-07-16 13:32:45
User: flatcap
Functions: shift
Tags: bash shell random
4

Randomly decide whether to run a command, or fail.

It's useful for testing purposes.

.

Usage: ran PERCENTAGE COMMAND [ARGS]

Note: In this version the percentage is required.

.

This is like @sesom42 and @snipertyler's commands but in a USABLE form.

.

e.g. In your complicated shell script, put "ran 99" before a crucial component.

Now, it will fail 1% of the time allowing you to test the failure code-path.

ran 99 my_complex_program arg1 arg2
sort -s -b -t' ' -k 4.9,4.12n -k 4.5,4.7M -k 4.2,4.3n -k 4.14,4.15n -k 4.17,4.18n -k 4.20,4.21n access.log*
2015-07-16 00:22:03
User: sesom42
Functions: sort
Tags: sort log apache
0

Sort Apache access logs by date and time using sort key field feature

ran() { [ $((RANDOM%100)) -lt ${1:-50} ]; }
2015-07-16 00:06:25
User: sesom42
0

Command will succeed n% of the time.

ran 75 will cause command to succeed 75% of the time.

No argument will cause it to succeed 50% of the time.

ran() { R=$((RANDOM%100)); if [ $R -gt "${1:-50}" ]; then echo FALSE; false; else echo TRUE; true; fi; }
2015-07-15 15:34:56
User: snipertyler
Functions: echo
0

Command will succeed n% of the time.

ran 75 will cause command to succeed 75% of the time.

No argument will cause it to succeed 50% of the time.

pvl() { (for i in "$@"; do youtube-dl -q --max-downloads 1 --no-playlist "$i" -o - | mplayer -vo null /dev/fd/3 3<&0 </dev/tty; sleep .5; done); }
2015-07-14 04:44:36
User: snipertyler
Functions: sleep
3

pvl 'link1' 'link2' 'link3'

Play Youtube, Vimeo, etc links without visual elements.

Great for music videos when you just want the audio.

You can control mplayer with this!

Hit Ctrl-C twice to exit (if you're playing multiple files)

echo "text to prepend" | cat - file
lsblk -do name,model,serial
2015-07-12 10:49:49
User: hackerb9
0

As of this writing, this requires a fairly recent version of util-linux, but is much simpler than the previous alternatives. Basically, lsblk gives a nice, human readable interface to all the blkid stuff. (Of course, I wouldn't recommend this if you're going to be parsing the output.) This command takes all the fun out of the previous nifty pipelines, but I felt I ought to at least mention it as an alternative since it is the most practical.

xmlpager() { xmlindent "$@" | awk '{gsub(">",">'`tput setf 4`'"); gsub("<","'`tput sgr0`'<"); print;} END {print "'`tput sgr0`'"}' | less -r; }
2015-07-12 09:22:10
User: hackerb9
Functions: awk less
0

Don't want to open up an editor just to view a bunch of XML files in an easy to read format? Now you can do it from the comfort of your own command line! :-) This creates a new function, xmlpager, which shows an XML file in its entirety, but with the actual content (non-tag text) highlighted. It does this by setting the foreground to color #4 (red) after every tag and resets it before the next tag. (Hint: try `tput bold` as an alternative). I use 'xmlindent' to neatly reflow and indent the text, but, of course, that's optional. If you don't have xmlindent, just replace it with 'cat'. Additionally, this example shows piping into the optional 'less' pager; note the -r option which allows raw escape codes to be passed to the terminal.

git diff $(git log --pretty=format:%h -2 --reverse | tr "\n" " ")
2015-07-07 17:50:50
Functions: diff tr
Tags: git
1

A great command to assign to an alias, allowing you to git diff the last two commits in git.

for file in $( git ls-files ); do echo $file; touch -t $(git --no-pager log --date=local -1 --format="%ct" $file | php -r 'echo @date( "YmdHi.s", trim( file_get_contents( "php://stdin" ) ) );') $file; done
echo "display:data?" | nc "$scope_ip_address" 5555 | dd bs=1 skip=11 2>/dev/null | convert bmp:- out_file.png
2015-06-27 06:43:43
User: ichbins
Functions: dd echo
Tags: convert rigol
1

Scope should have the Rigol Ultravision Technology otherwise it won't accept the command. ImageMagic is required. Scope sends a 1.1M BMP file and converted to PNG it's only 18-20K

ssh -o "ProxyCommand ssh user@reachable_host -W %h:%p" user@unreacheable_host
2015-06-26 06:23:14
User: renich
Functions: ssh
Tags: ssh
2

This command uses the reachable_host as a proxy; redirecting your request to the unreachable_host.

The main advantage is that you need only one ssh key. You copy the public part to both servers and you can access the unreachable_host without a problem.

Also, you can put this on ~/.ssh/config to access the unreachable_host directly:

# config

unreachable_host

ProxyCommand ssh user@reachable_host -W %h:%p

and, then, just ssh user@unreachable_host.

jhead -autorot -ta+1 *
exiftool -AllDates+=1 -{Track,Media}{Create,Modify}Date+=1 *.mov