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.

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:



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!

Top Tags





Commands tagged bash from sorted by
Terminal - Commands tagged bash - 725 results
alias tasks='grep --exclude-dir=.git -rEI "TODO|FIXME" . 2>/dev/null'
2013-10-17 16:40:10
User: joepie91
Functions: alias
Tags: bash todo

Place this in your .bashrc (or run it once) to set the `tasks` alias. Next time you enter `tasks` into a terminal, it will give you a list of all TODO and FIXME comments in the current directory and child directories, giving you a quick overview of what you still have to do!

ps -p $$
_autoOptions() { local cur=${COMP_WORDS[COMP_CWORD]} COMPREPLY=( $(compgen -W "--fooOption --barOption -f -b" -- $cur) ) ;}; complete -F _autoOptions autoOptions
tstouch() { [[ $1 =~ $2 ]] && touch -t ${BASH_REMATCH[1]} $1; }
2013-10-01 20:00:34
User: bartonski
Functions: touch
Tags: bash touch

tstouch takes two arguments: a filename containing a timestamp, and an extended regular expression with the parenthesized section matching a timestamp of the form YYYYMMDDhhmm or YYYYMMDDhhmm.ss.

It then touches the file with that timestamp.

dpkg-query -Wf '${Installed-Size}\t${Status}\t${Package}\n' | sort -n | grep installed
(IFS=,; echo "${array[*]}")
2013-09-25 10:36:38
User: aspiers
Functions: echo

This type of join is clearly documented in the bash manual. Only the first character of IFS is used for the delimiter.

wget -q -O- http://example-podcast-feed.com/rss | grep -o "<enclosure[ -~][^>]*" | grep -o "http://[ -~][^\"]*" | xargs wget -c
2013-09-24 12:38:08
User: talha131
Functions: grep wget xargs

This script can be used to download enclosed files from a RSS feed. For example, it can be used to download mp3 files from a podcasts RSS feed.

for ff in directory; do numLines=`wc -l $ff`; numLines=$(echo $numLines | sed 's/ .*//g'); min=$(sort -nrk 1 $ff | tail -1); if [ $numLines -gt 100 ]; then echo $min >> minValues; fi;done;
for f in ./*.xls; do mv "$f" "${f%.*}.ods"; done
2013-09-17 01:41:56
User: evilsoup
Functions: mv
Tags: bash Linux

An entirely shell-based solution (should work on any bourne-style shell), more portable on relying on the rename command, the exact nature of which varies from distro to distro.

2013-09-15 03:41:13
User: hackerb9

Bash's history expansion character, "!", has many features, including "!:" for choosing a specific argument (or range of arguments) from the history. The gist is any number after !: is the number of the argument you want, with !:1 being the first argument and !:0 being the command. See the sample output for a few examples. For full details search for "^HISTORY EXPANSION" in the bash(1) man page.

 Note that this version improves on the previous function in that it handles arguments that include whitespace correctly.

ln -s /BASE/* /TARGET/
2013-09-12 18:47:35
User: thehitman
Functions: ln
Tags: bash

Simple and easy to remember, if it already exists then it just ignores it.

rename 's/.xls/.ods/g' *.xls
<Meta-p> (aka <ALT+P>)
2013-09-10 17:13:02
User: hackerb9
Tags: history bash tcsh

[Click the "show sample output" link to see how to use this keystroke.]

Meta-p is one of my all time most used and most loved features of working at the command line. It's also one that surprisingly few people know about. To use it with bash (actually in any readline application), you'll need to add a couple lines to your .inputrc then have bash reread the .inputrc using the bind command:

echo '"\en": history-search-forward' >> ~/.inputrc

echo '"\ep": history-search-backward' >> ~/.inputrc

bind -f ~/.inputrc

  I first learned about this feature in tcsh. When I switched over to bash about fifteen years ago, I had assumed I'd prefer ^R to search in reverse. Intuitively ^R seemed better since you could search for an argument instead of a command. I think that, like using a microkernel for the Hurd, it sounded so obviously right fifteen years ago, but that was only because the older way had benefits we hadn't known about.

  I think many of you who use the command line as much as I do know that we can just be thinking about what results we want and our fingers will start typing the commands needed. I assume it's some sort of parallel processing going on with the linguistic part of the brain. Unfortunately, that parallelism doesn't seem to work (at least for me) with searching the history. I realize I can save myself typing using the history shortly after my fingers have already started "speaking". But, when I hit ^R in Bash, everything I've already typed gets ignored and I have to stop and think again about what I was doing. It's a small bump in the road but it can be annoying, especially for long-time command line users. Usually M-p is exactly what I need to save myself time and trouble.

  If you use the command line a lot, please give Meta-p a try. You may be surprised how it frees your brain to process more smoothly in parallel. (Or maybe it won't. Post here and let me know either way. ☺)

function garg () { tail -n 1 ${HISTFILE} | awk "{ print \$$1 }" }
2013-09-10 04:07:46
User: plasticphyte
Functions: awk tail

This gets the Nth argument in the last line of your history file. This is useful where history is being written after each command, and you want to use arguments from the previous command in the current command, such as when doing copies/moving directories etc.

I wrote this after getting irritated with having to continually type in long paths/arguments.

You could also use $_ if all you want is the last argument.

du -hd1 | sort -hr
sh -c 'url="http://youtu.be/MejbOFk7H6c"; vid="`for i in ".*youtu\.be/\([^\/&?#]\+\)" ".*youtu.\+v[=/]\([^\/&?#]\+\)" ".*youtu.\+embed/\([^\/&?#]\+\)"; do expr "${url}" : "${i}"; done`"; if [ -n "${vid}" ]; then echo ${vid}; else echo "${url}"; fi'
2013-09-04 19:33:09
User: qwertyroot
Functions: echo sh

url can be like any one of followings:

url="MejbOFk7H6c" url="http://youtu.be/MejbOFk7H6c" url="https://youtube.com/watch?feature=player_embedded&v=MejbOFk7H6c#t" url="//www.youtube.com/v/MejbOFk7H6c?hl=ru_RU&version=3&rel=0" url="http://www.youtube.com/embed/MejbOFk7H6c?feature=player_embedded"

If url mismatching, whole url will be returned.

curl ${URL} 2>/dev/null|grep "<${BLOCK}>"|sed -e "s/.*\<${BLOCK}\>\(.*\)\<\/${BLOCK}\>.*/\1/g"
2013-08-31 14:53:54
User: c3w
Functions: grep sed

set BLOCK to "title" or any other HTML / RSS / XML tag and curl URL to get everything in-between e.g. some text

function mkdcd () { mkdir "$1" && cd "$1" }
for i in *.flv *.mkv *.avi; do mplayer -ao null -vo null -ss 0 -endpos 1 >/dev/null "$i" 2> >(grep -qi error && echo >&2 "$i seems bad"); done
2013-08-20 22:23:11
User: sputnick
Functions: echo grep

If you want avoid to be annoyed when playing your favourite video files with your video player, first run this command to stash wrong files (and test tricks to play these wrong files).

diff <(sort <(md5deep -b -r /directory/1/) ) <(sort <(md5deep -b -r /directory/2/)
2013-08-19 18:20:49
Functions: diff sort
Tags: bash Linux diff

Compares the md5 checksums of the contents of two directories, outputting the checksum and filename where any files differ. Shows only the file name, not the full path.

nohup bash -c "while true; do ps -x | mail pascalv@mmmmail.com; sleep 3600; done" | mail pascalv@mmmmail.com &
2013-08-19 17:21:37
User: pascalv
Functions: bash mail nohup

Run "ps -x" (process status) in the background every hour (in this example).

The outputs of both "nohup" and "ps -x" are sent to the e-mail (instead of nohup.out and stdout and stderr).

If you like it, replace "ps -x" by the command of your choice, replace 3600 (1 hour) by the period of your choice.

You can run the command in the loop any time by killing the sleep process. For example

ps -x

2925 ? S 0:00.00 sh -c unzip E.zip >/dev/null 2>&1

11288 ? O 0:00.00 unzip E.zip

25428 ? I 0:00.00 sleep 3600

14346 pts/42- I 0:00.01 bash -c while true; do ps -x | mail pascalv; sleep 3600; done

643 pts/66 Ss 0:00.03 -bash

14124 pts/66 O+ 0:00.00 ps -x

kill 25428

You have mail in /mail/pascalv

diff <(sort <(md5deep -r /directory/1/) |cut -f1 -d' ') <(sort <(md5deep -r /directory/2/) |cut -f1 -d' ')
2013-08-18 22:13:07
Functions: cut diff sort
Tags: bash Linux diff

Compute the md5 checksums for the contents of two mirrored directories, then sort and diff the results. If everything matches, nothing is returned. Otherwise, any checksums which do not match, or which exist in one tree but not the other, are returned. As you might imagine, the output is useful only if no errors are found, because only the checksums, not filenames, are returned. I hope to address this, or that someone else will!

:(){ :|:& };:
ioreg -lw0 | grep IODisplayEDID | sed "/[^<]*</s///" | xxd -p -r | strings -6
function colorize() { c="--line-buffered --color=yes"; GREP_COLORS="mt=01;34" egrep $c '(^| 200 | 304 )' "${@}" | GREP_COLORS="mt=02;31" egrep $c '(^|"(GET|POST) .*[^0-9] 4[0-1][0-9] )' | GREP_COLORS="ms=02;37" egrep $c '(^|^[0-9\.]+) ';}
2013-08-14 21:05:34
User: mogsie
Functions: egrep

Puts a splash of color in your access logs. IP addresses are gray, 200 and 304 are green, all 4xx errors are red. Works well with e.g. "colorize access_log | less -R" if you want to see your colors while paging.

Use as inspiration for other things you might be tailing, like syslog or vmstat


tail -f access.log | colorize