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

Commands using find from sorted by
Terminal - Commands using find - 1,022 results
find . -type f -size +1100000k |xargs -I% du -sh %
2010-01-31 22:04:07
User: 4fthawaiian
Functions: du find xargs
1

simple find -> xargs sort of thing that I get a lot of use out of. Helps find huge files and gives an example of how to use xargs to deal with them. Tested on OSX snow leopard (10.6). Enjoy.

find . -type f | parallel -j+0 grep -i foobar
2010-01-30 02:08:46
Functions: find grep
3

Parallel does not suffer from the risk of mixing of output that xargs suffers from. -j+0 will run as many jobs in parallel as you have cores.

With parallel you only need -0 (and -print0) if your filenames contain a '\n'.

Parallel is from https://savannah.nongnu.org/projects/parallel/

find directory/ -exec grep -ni phrase {} +
2010-01-28 12:15:24
User: sanmiguel
Functions: find grep
Tags: find grep
0

The difference between this and the other alternatives here using only grep is that find will, by default, not follow a symlink. In some cases, this is definitely desirable.

Using find also allows you to exclude certain files, eg

find directory/ ! -name "*.tmp" -exec grep -ni phrase {} +

would allow you to exclude any files .tmp files.

Also note that there's no need for calling grep recursively, as find passes each found file to grep.

find -type d -name ".svn" -prune -o -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type d -name ".svn" -prune -o -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
2010-01-28 09:45:29
User: 2chg
Functions: find md5sum sort uniq xargs
2

Improvement of the command "Find Duplicate Files (based on size first, then MD5 hash)" when searching for duplicate files in a directory containing a subversion working copy. This way the (multiple dupicates) in the meta-information directories are ignored.

Can easily be adopted for other VCS as well. For CVS i.e. change ".svn" into ".csv":

find -type d -name ".csv" -prune -o -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type d -name ".csv" -prune -o -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate
find -not -empty -type f -printf "%s\n" | sort | uniq -d | parallel find -type f -size {}c | parallel md5sum | sort | uniq -w32 --all-repeated=separate
2010-01-28 08:40:18
Functions: find md5sum sort uniq
Tags: xargs parallel
-1

A bit shorter and parallelized. Depending on the speed of your cpu and your disk this may run faster.

Parallel is from https://savannah.nongnu.org/projects/parallel/

find filepath -type f -iname "*.html" -o -iname "*.htm" -o -iname "*.php" | xargs grep "Exception\|LGPL\|CODE1"
find directory/ |xargs grep -i "phrase"
find . -iname "*.jar" -exec cp '{}' /tmp/ \;
2010-01-26 12:19:15
Functions: cp find
-3

Useful for Maven multimodule projects, where you want to extract all packaged jar files.

rsync -vd --files-from=<(find . -name entries -print ) . ../target_directory
2010-01-26 09:41:12
User: samyboy
Functions: find rsync
2

This command has been used to overwrite corrupted "entries" files of a corrupted subversion working copy.

Note the --files-from input format.

find . -maxdepth 1 -type d -exec 'mv "{}" "{}-old" && svnadmin create "{}" && svnadmin recover "{}-old" && svnadmin dump "{}-old" | svnadmin load "{}" && rm -rf "{}-old"' \;
find . -maxdepth 1 -type f| xargs sha1sum | sed 's/^\(\w*\)\s*\(.*\)/\2 \1/' | while read LINE; do mv $LINE; done
find -maxdepth 1 -type f -name "*.7z" -exec 7zr e '{}' ';'
2010-01-23 19:50:10
User: minnmass
Functions: find
-1

Use find's built-in ability to call programs.

Alternatively,

find -maxdepth 1 -type f -name "*.7z" -print0 | xargx -0 -n 1 7zr e

would work, too.

find . -type f -exec stat \{\} \; | grep Modify: | awk '{a[$2]++}END{for(i in a){print i " : " a[i] }}' | sort
find . -exec grep -l "sample" {} \;
2010-01-16 13:12:52
User: whoami
Functions: find grep
-8

Will find all files containing "sample" in the current directory and in the directories below.

find . -name "*.php" -exec grep -il searchphrase {} \;
2010-01-16 05:09:30
Functions: find grep
-1

This is very similar to the first example except that it employs the 'exec' argument of the find command rather than piping the result to xargs. The second example is nice and tidy but different *NIXs may not have as capable a grep command.

find . -name "*.php" | xargs grep -il searchphrase
2010-01-14 22:42:36
User: refrax
Functions: find grep xargs
2

This command will find all files recursively containing the phrase entered, represented here by "searchphrase". This particular command searches in all php files, but you could change that to just be html files or just log files etc.

find . -type d -empty -delete
find . ! -name <FILENAME> -delete
2010-01-13 23:29:51
User: haplo
Functions: find
Tags: files rm
8

it will remove everything except the file names matching you can use also use wildcards

find / \( -local -o -prune \) \( -perm -4000 -o -perm -2000 \) -type f -exec ls -l {} \;
find /lib/modules/`uname -r`/ -type f -iname '*.o' -or -iname '*.ko' |grep -i -o '[a-z0-9]*[-|_]*[0-9a-z]*\.ko$' |xargs -I {} echo '# {}' >>/etc/modules.autoload.d/kernel-2.6
2010-01-13 02:12:08
User: paragao
Functions: echo find grep xargs
-1

Whenever you compile a new kernel, there are always new modules. The best way to make sure you have the correct modules loaded when you boot is to add all your modules in the modules.autoload file (they will be commented) and uncomment all those modules you need.

Also a good way to keep track of the available modules in your system.

For other distros you may have to change the name of the file to /etc/modprobe.conf

find / -name *.conf | tee salida
i=0; for f in $(find ./ -size -10M -exec stat -c %s {} \; ); do i=$(($i + $f)); done; echo $i
find /path/to/images -name '*.JPG' -exec bash -c 'mv "$1" "${1/%.JPG/.jpg}"' -- {} \;
2010-01-07 15:41:17
User: sorpigal
Functions: bash find
Tags: bash find mv
7

Recursively rename .JPG to .jpg using standard find and mv. It's generally better to use a standard tool if doing so is not much more difficult.

sudo find /etc/rc{1..5}.d -name S99myservice -type l -exec sh -c 'NEWFN=`echo {} | sed 's/S99/K99/'` ; mv -v {} $NEWFN' \;
2010-01-03 00:56:57
User: zoomgarden
Functions: find mv sed sh sudo
0

Change run control links from start "S" to stop "K" (kill) for whatever run levels in curly braces for a service called "myservice". NEWFN variable is for the new filename stored in the in-line shell. Use different list of run levels (rc*.d, rc{1,3,5}.d, etc.) and/or swap S with K in the command to change function of run control links.

find /path/to/images -name '*.JPG' -exec rename "s/.JPG/.jpg/g" \{\} \;
2010-01-02 19:12:37
User: renich
Functions: find rename
Tags: find rename
8

This command is useful for renaming a clipart, pic gallery or your photo collection. It will only change the big caps to small ones (on the extension).