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.

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

Tags

Hide

Functions

All commands from sorted by
Terminal - All commands - 11,485 results
cat $(ls -tr | tail -1) | awk '{ a[$1] += 1; } END { for(i in a) printf("%d, %s\n", a[i], i ); }' | sort -n | tail -25
2009-03-06 17:50:29
User: oremj
Functions: awk cat ls sort tail
7

This command is much quicker than the alternative of "sort | uniq -c | sort -n".

du -sh *
tail -f *[!.1][!.gz]
2009-03-06 16:24:44
User: piscue
Functions: tail
5

with discard wilcards in bash you can "tail" newer logs files to see what happen, any error, info, warn...

hdiutil makehybrid -udf -udf-volume-name DVD_NAME -o MY_DVD.iso /path/
2009-03-06 15:45:59
User: occam
Tags: Os X macosx
1

/path/ is the root folder of the DVD, not the VIDEO_TS folder.

echo 1 2 3 > FILE; while read -a line; do echo ${line[2]}; done < FILE
2009-03-06 15:32:40
User: occam
Functions: echo read
Tags: bash
-2

This will print out the third column of every line in FILE. Useful for many files in /proc or *csv data.

vifind() { vi `find . -name "$1"` }
cd() { builtin cd "${@:-$HOME}" && ls; }
2009-03-05 22:37:35
User: haivu
Functions: cd
6

Often, the very next command after the cd command is 'ls', so why not combine them?. Tested on a Red Hat derivative and Mac OS X Leopard

Update: changed ${1:-$HOME} to "${@:-$HOME}" to accomodate directories with spaces in the names

find . -type d \( -name DIR1 -o -name DIR2 \) -prune -o -type f -print0 | xargs -r0 md5sum
2009-03-05 21:26:24
User: starchox
Functions: find xargs
Tags: bash
4

Useful if you want get all the md5sum of files but you want exclude some directories. If your list of files is short you can make in one command as follow:

find . -type d \( -name DIR1 -o -name DIR2 \) -prune -o -type f -exec md5sum {} \;

Alternatively you can specify a different command to be executed on the resulting files.

alias lh='ls -a | egrep "^\."'
/usr/proc/bin/pfiles $PID
2009-03-05 17:26:57
User: axelabs
4

Report fstat(2) and fcntl(2) information for all open files in each process.

SUM=0; for FILESIZE in `find /tmp -type f -iname \*pdf -exec du -b {} \; 2>/dev/null | cut -f1` ; do (( SUM += $FILESIZE )) ; done ; echo "sum=$SUM"
2009-03-05 17:16:52
User: alcik
Functions: cut du echo
Tags: find du
0

This example summarize size of all pdf files in /tmp directory and its subdirectories (in bytes).

Replace "/tmp" with directory path of your choice and "\*pdf" or even "-iname \*pdf" with your own pattern to match specific type of files. You can replace also parameter for du to count kilo or megabytes, but because of du rounding the sum will not be correct (especially with lot of small files and megabytes counting).

In some cases you could probably use sth like this:

du -cb `find /tmp -type f -iname \*pdf`|tail -n 1

But be aware that this second command CANNOT count files with spaces in their names and it will cheat you, if there are some files matching the pattern that you don't have rights to read. The first oneliner is resistant to such problems (it will not count sizes of files which you cant read but will give you correct sum of rest of them).

touch /tmp/$$;for N in `seq -w 0 7777|grep -v [89]`; do chmod $N /tmp/$$; P=`ls -l /tmp/$$ | awk '{print $1}'`; echo $N $P; done;rm /tmp/$$
grep 'HOME.*' data.txt | awk '{print $2}' | awk '{FS="/"}{print $NF}' OR USE ALTERNATE WAY awk '/HOME/ {print $2}' data.txt | awk -F'/' '{print $NF}'
2009-03-05 07:28:26
User: rommelsharma
Functions: awk grep
-3

grep 'HOME.*' data.txt | awk '{print $2}' | awk '{FS="/"}{print $NF}'

OR

awk '/HOME/ {print $2}' data.txt | awk -F'/' '{print $NF}'

In this example, we are having a text file that is having several entries like:

---

c1 c2 c3 c4

this is some data

HOME /dir1/dir2/.../dirN/somefile1.xml

HOME /dir1/dir2/somefile2.xml

some more data

---

for lines starting with HOME, we are extracting the second field that is a 'file path with file name', and from that we need to get the filename only and ignore the slash delimited path.

The output would be:

somefile1.xml

somefile2.xml

(In case you give a -ive - pls give the reasons as well and enlighten the souls :-) )

mogrify -resize 800\> *
echo Print text vertically|sed 's/\(.\)/\1\n/g'
grep -r --exclude-dir=.svn PATTERN PATH
2009-03-04 23:21:50
User: patko
Functions: grep
Tags: svn
8

exclude-dir option requires grep 2.5.3

stty -echo; ssh -t HOSTNAME "sudo some_command"; stty echo
2009-03-04 19:44:36
User: jmcantrell
Functions: ssh stty
Tags: ssh sudo
3

The ssh command alone will execute the sudo command remotely, but the password will be visible in the terminal as you type it. The two stty commands disable the terminal from echoing the password back to you, which makes the remote sudo act as it does locally.

find . -type f -print0 | xargs -r0 stat -c %Y\ %n | sort -rn | gawk '{sub(/.\//,"",$2); print $2}' > /tmp/playlist.m3u
2009-03-04 16:41:02
User: microft
Functions: find gawk sort stat xargs
3

I use this to generate a playlist with all the podcasts I listen to.

Ordered from most recent to older.

du | sort -n | tail -11 | head
2009-03-04 16:06:34
User: phage
Functions: du sort tail
-3

The pipe to head removes the listing of . as the largest directory.

somecommand 2>&1 >> logfile | tee -a logfile
2009-03-04 15:15:05
User: operon
Functions: tee
12

Useful for cron jobs -- all output will be logged but only errors will cause email to be sent. NB the order of "2>&1" and ">> logfile" is important, it doesn't work if you reverse them (everything goes to the logfile, nothing left for tee).

find . -type d -name DIR -print0 | xargs -r0 rm -r
2009-03-04 14:49:21
User: miguelbaldi
Functions: find rm xargs
Tags: svn cvs
-2

You can use this command to delete CVS/svn folders on given project.

find . -type f | wc -l
wget -c -v -S -T 100 --tries=0 `curl -s http://ms1.espectador.com/ podcast/espectador/la_venganza_sera_terrible.xml | grep -v xml | grep link | sed 's/]*>//g'`
2009-03-04 13:12:28
User: fmdlc
Functions: grep link sed wget
-3

This download a complete audio podcast

logs=$(find . -name *.log);for log in $logs; do cat /dev/null > $log;done
2009-03-04 10:05:48
Functions: cat find
-5

This find files of name like *.log and truncates them.