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,040 results
find . -type f -exec md5sum {} \;
find . -maxdepth 1 -type d -exec sh -c "printf '{} ' ; find '{}' -type f -ls | wc -l" \;
2013-07-29 19:46:35
User: HerbCSO
Functions: find sh
2

For each directory from the current one, list the counts of files in each of these directories. Change the -maxdepth to drill down further through directories.

find (); { ls $1 | while read line; do [[ -d $1/$line ]] && find $1/$line $2 || echo $1/$line | grep $2; done; }
2013-07-28 22:21:39
User: a8ksh4
Functions: echo find grep ls read
3

basic find implementation for systems that don't actually have find, like an android console without busybox installed.

find $(YourPATH) -type f -exec dos2unix '{}' \;
find /var/scripts -name 'backup*' -exec {} ';'
2013-07-24 21:25:21
User: piscue
Functions: find
Tags: backup
0

It eases the way of creating cron jobs of backup scripts.

Just put this line as cron job, and all your backups are called sequentially.

Allows you to forget, when in time, call this backup, just focus on your scripting. Also maintains the way of calling a single backup script when It's needed.

find /logs -ls -xdev | sort -nrk 7 | head -10
2013-07-22 08:36:03
User: vidya99343
Functions: find head sort
0

bash-3.2$ find /logs -ls -xdev | sort -nrk 7 | head -10

1761905 205380 -rwxrwxr-x 1 wsadmin logadmin 210095353 Jul 22 01:33 /logs/intlpymt/Trace.log

652689 187360 -rwxrwxr-x 1 wsadmin logadmin 191663182 Jul 21 23:00 /logs/websphere/wsfpp1lppwa1213omsecureServer/SystemOut_13.07.21_23.00.12.log

2380449 186536 -rwxrwxr-x 1 wsadmin logadmin 190819939 Jul 16 14:03 /logs/omset/traceIntl.log.201307161403.lppwa1213.gz

2119524 183888 -rwxrwxr-x 1 wsadmin logadmin 188110111 Jul 22 01:33 /logs/intlpymt/intlpymtria/Trace.log

652816 160332 -rwxrwxr-x 1 wsadmin logadmin 164011871 Aug 14 2012 /logs/websphere/wsfpp1lppwa1213omsecureServer/SystemOut.log_08142012.gzip

653312 128916 -rwxrwxr-x 1 wsadmin logadmin 131873943 Jul 18 10:49 /logs/websphere/heapdump.20130718.104150.27592.0006.phd.201307181406.lppwa1213.gz

653320 128916 -rwxrwxr-x 1 wsadmin logadmin 131873735 Jul 18 10:40 /logs/websphere/heapdump.20130718.104012.27592.0002.phd.201307181406.lppwa1213.gz

653309 128912 -rwxrwxr-x 1 wsadmin logadmin 131867602 Jul 18 10:46 /logs/websphere/heapdump.20130718.104008.27592.0001.phd.201307181405.lppwa1213.gz

653323 128872 -rwxrwxr-x 1 wsadmin logadmin 131828157 Jul 18 10:41 /logs/websphere/heapdump.20130718.104109.27592.0004.phd.201307181407.lppwa1213.gz

652783 120288 -rwxrwxr-x 1 wsadmin logadmin 123047750 Aug 13 2012 /logs/websphere/wsfpp1lppwa1213omsecureServer/SystemOut.log_0813.2012.gzip

bash-3.2$

find /logs -ls -xdev | sort -nrk 7 | head -10
find ./ -type f -print0 | xargs -0 md5sum
find ./ -type f | sed "s:[\ \',\"]:\\\&:g" | xargs md5sum
2013-07-17 18:54:14
User: crazedsanity
Functions: find sed xargs
0

Recursively list all files in the current directory & get their md5sum, even if the filename has bad characters.

find . -type d | sed -e "s/[^-][^\/]*\// |/g" -e "s/|\([^ ]\)/|-\1/"
2013-07-16 10:08:34
User: opexxx
Functions: find sed
Tags: sed find
-4

show directory three

find . | sort | awk 'NR%2==0' | xargs rm $1
2013-07-11 07:36:18
User: sucotronic
Functions: awk find rm sort xargs
-1

If you have a directory with lot of backups (full backups I mean), when it gets to some size, you could want to empty some space. With this command you'll remove half of the files. The command assumes that your backup files starts with YYYYMMDD or that they go some alphabetical order.

find . -name ".DS_Store" -print0 | xargs -0 rm -rf
diff <(cd A; find -type f|xargs md5sum ) <(cd B; find -type f | xargs md5sum )
2013-07-02 18:02:05
User: glaudiston
Functions: cd diff find md5sum xargs
6

This is usefull to diff 2 paths in branches of software, or in different versions of a same zip file. So you can get the real file diff.

find . -empty -type d -print0 | xargs -0 rmdir -p
2013-07-01 02:44:57
User: rafar
Functions: find rmdir xargs
0

It starts in the current working directory.

It removes the empty directory and its ancestors (unless the ancestor contains other elements than the empty directory itself).

It will print a failure message for every directory that isn't empty.

This command handles correctly directory names containing single or double quotes, spaces or newlines.

If you do not want only to remove all the ancestors, just use:

find . -empty -type d -print0 | xargs -0 rmdir
find . -type f ! -path \*CVS\* -exec rm {} \; -exec cvs remove {} \;
2013-06-28 20:17:40
User: jasonsydes
Functions: cvs find rm
Tags: bash cvs delete rm
0

This command removes and then cvs removes all files in the current directory recursively.

find . -type d -print0 | xargs -0 du -s | sort -n | tail -10 | cut -f2 | xargs -I{} du -sh {} | sort -rn
find . -name '*.[Jj][Pp][Gg]' -exec mogrify -resize 1024">" -quality 40 {} \;
2013-06-21 13:27:25
User: hugme
Functions: find
0

The find command can do this on it's own. This is a shorter faster version, it also includes more advanced regex (it will find .Jpg etc). Find doesn't need a pipe, you can run it directly from the command.

find . -name '*.jpg' -o -name '*.JPG' -print0 | xargs -0 mogrify -resize 1024">" -quality 40
2013-06-20 16:09:41
User: minnmass
Functions: find xargs
0

The "find $stuff -print0 | xargs -0 $command" pattern causes both find and xargs to use null-delineated paths, greatly reducing the probability of either hiccuping on even the weirdest of file/path names.

It's also not strictly necessary to add the {} at the end of the xargs command line, as it'll put the files there automatically.

Mind, in most environments, you could use find's "-exec" option to bypass xargs entirely:

find . -name '*.jpg' -o -name '*.JPG' -exec mogrify -resize 1024">" -quality 40 {} +

will use xargs-like "make sure the command line isn't too long" logic to run the mogrify command as few times as necessary (to run once per file, use a ';' instead of a '+' - just be sure to escape it properly).

find . -name '*.jpg' -o -name '*.JPG' | xargs -I{} mogrify -resize 1024">" -quality 40 {}
2013-06-20 15:20:29
Functions: find xargs
-2

First use find to find all the images that end with jpg or JPG in the current dir and all its children.

Then pipe that to xargs. The -I{} makes it so spaces in filenames don't matter.

The 1024">" makes it so it takes any image greater in dimension than 1024 and resizes them to 1024 width, but keeping aspect ratio on height.

Then it sets the image quality to 40.

Piping it through xargs means you avoid the file count limit, and you could run this on your entire file system if you wanted.

find -printf "%C@ %p\n"|sort
2013-06-19 10:42:49
User: oivvio
Functions: find
Tags: sort find
5

This uses the ability of find (at least the one from GNU findutils that is shiped with most linux distros) to display change time as part of its output. No xargs needed.

find /usr/include/ -name '*.[c|h]pp' -o -name '*.[ch]' -print0 | xargs -0 cat | grep -v "^ *$" | grep -v "^ *//" | grep -v "^ */\*.*\*/" | wc -l
2013-06-17 08:37:37
Functions: cat find grep wc xargs
1

Count your source and header file's line numbers. This ignores blank lines, C++ style comments, single line C style comments.

This will not ignore blank lines with tabs or multiline C style comments.

find -name '*.mp3' -exec mp3info {} -p "%F: %r kbps\n" \; | sort | sed '/320 kbps/d'
find . -type f -name filename.exe -exec sed -i "s/oldstring/oldstring/g" {} +;
find -iname "MyCProgram.c" -exec md5sum {} \;
find . -name .git -print0 | while read -d $'\0' g; do echo "$g"; cd "$g"; git gc --aggressive; cd -; done
2013-05-09 08:03:23
User: Tungmar
Functions: cd echo find read
0

git gc should be run on all git repositories every 100 commits. This will help do do so if you have many git repositories ;-)