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 file from sorted by
Terminal - Commands tagged file - 75 results
sed 's/^[ \t]*//;s/[ \t]*$//' < <file> > <file>.out; mv <file>.out <file>
sed 's/^[ \t]*//' < <file> > <file>.out; mv <file>.out <file>
sed 's/[ \t]*$//' < <file> > <file>.out; mv <file>.out <file>
grep -vh '^[[:space:]]*\(#\|$\)' <file>
rename 's/\b((?!(a|of|that|to)\b)[a-z]+)/\u$1/g' *
2010-08-22 15:00:33
User: John_W
Functions: rename

All words of the filenames except "a", "of", "that" and "to" are capitalized.

To also match words which begin with a specific string, you can use this:

rename 's/\b((?!hello\b|t)[a-z]+)/\u$1/g' *

This will capitalize all words except "hello" and words beginning with "t".

rename 's/\b([a-z])/\u$1/g' *
rename 's/(^|[\s\(\)\[\]_-])([a-z])/$1\u$2/g' *
2010-08-22 11:26:20
User: l0b0
Functions: rename

Anyone know how to avoid title casing some words, like 'to', 'of', 'that', etc.?

perl -le 'chomp($w=`which $ARGV[0]`);$_=`file $w`;while(/link\b/){chomp($_=(split/`/,$_)[1]);chop$_;$w.=" -> $_";$_=`file $_`;}print "\n$w";' COMMAND_NAME
2010-07-30 19:26:35
User: dbbolton
Functions: perl

This will show you any links that a command follows (unlike 'file -L'), as well as the ultimate binary or script.

Put the name of the command at the very end; this will be passed to perl as the first argument.

For obvious reasons, this doesn't work with aliases or functions.

sudo find . -maxdepth 1 -cnewer olderFilesNameToMove -and ! -cnewer newerFileNameToMove -exec mv -v {} /newDirectory/ \;
2010-06-30 20:40:30
User: javamaniac
Functions: find mv sudo

In a folder with many files and folders, you want to move all files where the date is >= the file olderFilesNameToMove and

statt(){ C=c;stat --h|sed '/Th/,/NO/!d;/%/!d'|while read l;do p=${l/% */};[ $p == %Z ]&&C=fc&&echo ^FS:^;echo "`stat -$C $p \"$1\"` ^$p^${l#%* }";done|column -ts^; }
2010-06-11 23:31:03
User: AskApache
Functions: column read sed

This shows every bit of information that stat can get for any file, dir, fifo, etc. It's great because it also shows the format and explains it for each format option.

If you just want stat help, create this handy alias 'stath' to display all format options with explanations.

alias stath="stat --h|sed '/Th/,/NO/!d;/%/!d'"

To display on 2 lines:

( F=/etc/screenrc N=c IFS=$'\n'; for L in $(sed 's/%Z./%Z\n/'<<<`stat --h|sed -n '/^ *%/s/^ *%\(.\).*$/\1:%\1/p'`); do G=$(echo "stat -$N '$L' \"$F\""); eval $G; N=fc;done; )

For a similarly powerful stat-like function optimized for pretty output (and can sort by any field), check out the "lll" function


From my .bash_profile ->


touch file-$(date +%Y%m%d)
find ~/.mozilla/firefox/*/Cache -exec file {} \; | awk -F ': ' 'tolower($2)~/mpeg/{print $1}'
2010-04-19 06:59:55
User: sata
Functions: awk file find

Grab a list of MP3s (with full path) out of Firefox's cache

Ever gone to a site that has an MP3 embedded into a pesky flash player, but no download link? Well, this one-liner will yank the *full path* of those tunes straight out of FF's cache in a clean list.

Shorter and Intuitive version of the command submitted by (TuxOtaku)

for i in `ls ~/.mozilla/firefox/*/Cache`; do file $i | grep -i mpeg | awk '{print $1}' | sed s/.$//; done
2010-04-11 23:14:18
User: TuxOtaku
Functions: awk file grep sed

Ever gone to a site that has an MP3 embedded into a pesky flash player, but no download link? Well, this one-liner will yank the names of those tunes straight out of FF's cache in a nice, easy to read list. What you do with them after that is *ahem* no concern of mine. ;)

ls -lS
dd if=inputfile of=split3 bs=16m count=32 skip=64
2010-02-21 10:09:46
User: jearsh
Functions: dd
Tags: dd file split

bs = buffer size (basically defined the size of a "unit" used by count and skip)

count = the number of buffers to copy (16m * 32 = 1/2 gig)

skip = (32 * 2) we are grabbing piece 3...which means 2 have already been written so skip (2 * count)

i will edit this later if i can to make this all more understandable

find . -type f |sed "s#.*/##g" |sort |uniq -c -d
2010-02-17 11:59:54
User: shadycraig
Functions: find sed sort uniq

Useful for C projects where header file names must be unique (e.g. when using autoconf/automake), or when diagnosing if the wrong header file is being used (due to dupe file names)

i=0; for f in $(find ./ -size -10M -exec stat -c %s {} \; ); do i=$(($i + $f)); done; echo $i
ps -ef | grep pmon
find . -type f -printf "%h\n" | cut -d/ -f-2 | sort | uniq -c | sort -rn
2009-10-09 23:49:53
User: ivancho
Functions: cut find sort uniq
Tags: file count

counts the total (recursive) number of files in the immediate (depth 1) subdirectories as well as the current one and displays them sorted.

Fixed, as per ashawley's comment

find . | xargs file | grep ".*: .* text" | sed "s;\(.*\): .* text.*;\1;"
rsync -az /home/user/test [email protected]:/tmp/
2009-08-25 10:45:15
User: peshay
Functions: rsync
Tags: ssh file move

copy files to a ssh server with gzip compression

tar -cf - /home/user/test | gzip -c | ssh [email protected] 'cd /tmp; tar xfz -'
2009-08-24 18:35:38
User: esplinter
Functions: gzip ssh tar
Tags: ssh file move

Useful to move many files (thousands or millions files) over ssh. Faster than scp because this way you save a lot of tcp connection establishments (syn/ack packets).

If using a fast lan (I have just tested gigabyte ethernet) it is faster to not compress the data so the command would be:

tar -cf - /home/user/test | ssh [email protected] 'cd /tmp; tar xf -'

ls -S -lhr
2009-04-28 01:28:57
User: rez0r
Functions: ls

This command list and sort files by size and in reverse order, the reverse order is very helpful when you have a very long list and wish to have the biggest files at the bottom so you don't have scrool up.

The file size info is in human readable output, so ex. 1K..234M...3G

Tested with Linux (Red Hat Enterprise Edition)

command > tmp && cat logfile.txt >> tmp && tmp > logfile.txt && rm tmp
2009-04-05 22:00:32
User: akoumjian
Functions: cat command rm

Adds the stdout (standard output) to the beginning of logfile.txt. Change "command" to whatever command you like, such as 'ls' or 'date', etc. It does this by adding the output to a temporary file, then adding the previous contents of logfile.txt to the temp file, then copying the new contents back to the logfile.txt and removing the temp file.

cp ./* .[!.]* ..?* /path/to/dir
2009-03-16 13:27:36
User: ako
Functions: cp

./* is for copying files starting with -

.[!.]* is for copying hidden files and avoiding copying files from the parent directory.

..?* is for copying files starting with .. (avoids the directory ..)

/path/to/dir the path to the directory where the files should be copied

Can also be used as a script. Input argument is /path/to/dir

in tcsh, replace .[!.]* with .[^.]*