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,039 results
find . -type f -exec sed -i 's/gw10./gw17./g' {} \;
find . -type d -name .svn -delete
findopen() { local PS3="select file: "; select file in $(find "$1" -iname "$2"); do ${3:-xdg-open} $file; break; done }
2010-02-28 02:28:59
User: quigybo
Functions: file find
-1

lists the files found by find, waits for user input then uses xdg-open to open the selected file with the appropriate program.

usage: findopen path expression [command]

With the third optional input you can specify a command to use other than xdg-open, for example you could echo the filename to stdout then pipe it to another command.

To get it to work for files with spaces it gets a bit messier...

findopen() { files=( $(find "$1" -iname "$2" | tr ' ' '@') ); select file in "${files[@]//@/ }"; do ${3:-xdg-open} "$file"; break; done }

You can replace the @ with any character that probably wont be in a file name.

find ./ -name '*.sw[op]' -delete
find . -name "*~" -exec rm {} \;
2010-02-26 10:54:02
User: ivanatora
Functions: find rm
Tags: vim find
-5

Assuming only VIM has *~ files in your current dir. If you have usefull data in a file named in the *~ pattern, DO NOT RUN this command!

find -type d -name ".svn" -print0 | xargs -0 rm -rf
2010-02-25 10:16:18
User: tuxilicious
Functions: find rm xargs
-1

man find: If no paths are given, the current directory is used. - Can anybody tell me why so many people are typing the dot?

find . -iname "FILENAME" -exec sed -i 's/SEARCH_STRING/REPLACE_STRING/g' {} \;
2010-02-24 19:52:22
User: nanopino
Functions: find sed
1

using find's exec option instead of a for loop and using sed's -i option for inplace replacement. no need to do the file swap.

find /dev/vg00 -type b -exec lvextend -m 1 {} /dev/disk/<disk> \;
2010-02-24 18:41:33
User: Glip
Functions: find
Tags: hp-ux lvm
-1

Mirror every lvol in vg00 in hp-ux 11.31

find . -name .svn -exec rm -r {} +;
find . -type f |sed "s#.*/##g" |sort |uniq -c -d
2010-02-17 11:59:54
User: shadycraig
Functions: find sed sort uniq
0

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)

find -type f -regex ".*\.\(js\|php\|inc\|htm[l]?\|css\)$" -exec grep -il 'searchstring' '{}' +
find . -type f \( -name "*.js" -o -name "*.php" -o -name "*.inc" -o -name "*.html" -o -name "*.htm" -o -name "*.css" \) -exec grep -il 'searchString' {} \;
2010-02-07 15:28:20
User: niels_bom
Functions: find grep
Tags: find grep search
-1

Use find to recursively make a list of all files from the current directory and downwards. The files have to have an extension of the ones listed. Then for every file found, grep it for 'searchString', returns the filename if searchString is found.

find . -name <pattern> -ls | awk 'BEGIN {i=0}; {i=i+$7}; END {print i}'
2010-02-05 14:47:27
User: danam
Functions: awk find
Tags: disk usage
2

Just how much space are those zillions of database logs taking up ? How much will you gain on a compression rate of say 80% ? This little line gives you a good start for your calculations.

find /dev/vg00 -type b |while read L; do lvextend -m 1 $L /dev/disk/<disk> ; done
find /path/to/dir -type f -printf "%T@|%p\n" 2>/dev/null | sort -n | tail -n 1| awk -F\| '{print $2}'
newest () { find ${1:-\.} -type f |xargs ls -lrt ; }
find . -type f |xargs -I% sed -i '/group name/s/>/ deleteMissing="true">/' %
2010-02-01 21:09:57
User: 4fthawaiian
Functions: find sed xargs
1

Changed out the for loop for an xargs. It's a tad shorter, and a tad cleaner.

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.