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 ~/.thumbnails/ -type f -atime +30 -print0 | xargs -0 rm
2009-03-30 04:23:07
User: alperyilmaz
Functions: find xargs
1

By time thumbnail images in ~/thumbnails take up too much space, this command will help deleting old ones.

Find options explained:

-type f : find files only, not directories

-atime +30 : last accessed more than 30 days ago

find /home/user/doc/ -type d -printf "mkdir -vp '/home/user/Dropbox%p'\n" -o -type f -printf "ln -vs '%p' '/home/user/Dropbox%p'\n" | sh
2009-03-29 09:25:12
User: jnash
Functions: find
0

Extremely useful to maintain backups if you're using Dropbox. This mirrors the entire directory structure and places symlinks in each to the original file. Instead of copying over the data again to the ~/Dropbox folder creating a symbolic link tree is much more sensible in terms of space usage.

This has to be supplemented by another script that removes dead symlinks in the Dropbox folder which point to files that have been moved/removed.

find -L ./ -type l -delete

And then removing empty directories

find ./ -type d -exec rmdir 2>/dev/null {} \;

**Actually after some finding I found lndir which creates symbolic trees but it wasn't in the Arch repos so.. ;)

find ./ -name '*.JPG' -type f -execdir rename -f 'y/A-Z/a-z/' {} \+
2009-03-27 13:49:56
User: pronoiaque
Functions: find rename
2

Change files case, without modify directories, recursively.

... fucking vfat

find ./ -iname "*.mp3" -type f -printf "mv '%p' '%p'\n" | sed -e "s/mp3'$/mp3'/I" | sh
2009-03-27 13:42:40
User: jnash
Functions: find sed
-1

Extensible to other ugly extensions like *.JPG, *.Jpg etc..

Leave out the last pipe to sh to perform a dry run.

find . -name 'junkfiles-*' -print0 | xargs -0 rm
2009-03-26 15:38:42
User: kancer
Functions: find xargs
Tags: find xargs print rm
1

Can be used for other commands as well, replace rm with ls.

It is easy to make this shorter but if the filenames involved have spaces, you will need to do use find's "-print0" option in conjunction with xargs's "-0" option. Otherwise the shell that xargs uses to execute the "rm" command line will treat the space as a token separator, thereby treating the name as two (or more) names.

find . -type f -depth -3 -mtime -5
2009-03-25 19:54:06
User: totoro
Functions: find
-2

Ever wanted to find the most recently modified files, but couldn't remember exactly where they were in a project directory with many subdirectories? The "find" command, using a combination of "-mtime -N" and "-depth -D" can be used to find those files. If your directory structure isn't very deep, just omit the "-depth -D", but if your directory structure is very deep, then you can limit the depth of the traversal using "-depth -D", where "D" is the maximum number of directory levels to descend.

find /proc -regex '/proc/[0-9]+/smaps' -exec grep -l "$PATH_REGEX" {} \; | cut -d'/' -f2
2009-03-25 16:33:44
User: juddmaltin
Functions: cut find grep
1

faster than lsof by at least x2 on my box.

find . -wholename './.snapshot' -prune -o -print
2009-03-25 13:52:01
User: jeffaz
Functions: find
3

This can be useful for those who have mounted NetApp file-systems with snapshot activated.

find -depth . | (while read FULLPATH; do BASENAME=`basename "${FULLPATH}"`; DIRNAME=`dirname "${FULLPATH}"`; mv "${DIRNAME}/${BASENAME}" "${DIRNAME}/${BASENAME// /_}"; done)
2009-03-24 21:04:32
User: mohan43u
Functions: find mv read
-9

Takes filenames and directory names and replace space to '_'.

date -d "@$(find dir -type f -printf '%C@\n' | sort -n | sed -n "$(($(find dir -type f | wc -l)/2))p")" +%F
2009-03-24 18:48:49
User: allengarvin
Functions: date dir find wc
-1

I needed to get a feel for how "old" different websites were, based on their directories.

find . -regex "[^.]*" -depth -empty -type d -mtime +1 -exec rmdir -v {} \;
find / -nouser -o -nogroup -print
find -iname '*.flac' | sed 's:/[^/]*$::' | uniq
2009-03-24 13:26:31
User: ar
Functions: find sed
-2

Run this in your music folder, or give the path directly after "find".

The sed pattern filters away the basename.

find ./ -mtime -5 | xargs rm -f
find . -name *.php | xargs grep -i -n 'TERM'
find . -name "*.EXT" | xargs grep -n "TODO" | wc -l
find -maxdepth 1 -type d -print0 | xargs -0 -I {} tar -cvzf {}.tar.gz {}
2009-03-17 11:12:53
User: piemme
Functions: find tar xargs
2

Create backup (.tar.gz) for all first-level directory from current dir.

find ~/project -mtime -1 -type f -print | tar jcvf myfiles.tar.bz2 -T -
2009-03-13 13:03:11
User: voyeg3r
Functions: find tar
0

create tar.bz2 package from files "-type f" modificated today "-mtime -1" in ~/project

rsync -avz -e ssh --files-from=<(find -mtime +30 -mtime -60) source dest
2009-03-13 12:58:28
User: voyeg3r
Functions: find rsync ssh
6

rsync from source to dest all between >30

find . -name "*.pdf" -print0 | xargs -0 cp -t downloads/
2009-03-13 03:15:27
User: abcde
Functions: cp find xargs
1

-t, --target-directory=DIRECTORY (copy all SOURCE arguments into DIRECTORY).

find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
2009-03-12 22:25:26
Functions: find sed
-1

NOT MINE! Taken from hackzine.com blog.

It creates a tree-style output of all the (sub)folders and (sub)files from the current folder and down(deeper)

Quoting some of hackzine's words

"Murphy Mac sent us a link to a handy find/sed command that simulates the DOS tree command that you might be missing on your Mac or Linux box. [..split...] Like most things I've seen sed do, it does quite a bit in a single line of code and is completely impossible to read. Sure it's just a couple of substitutions, but like a jack in the box, it remains a surprise every time I run it."

find . -maxdepth 1 -type f -print0 | xargs -0 -i cp ./{}{,.bak}
2009-03-12 16:02:13
User: voyeg3r
Functions: cp find xargs
-2

"." is current dir, maxdepth is the level, -print0 | xargs -0 fix spaces in names, -i interactive , ./ is the current dir {} actual name , and {,.bak} is the atual name + bak

find . -name *DS_Store -exec echo rm {} \;
2009-03-11 11:30:55
User: dgomes
Functions: echo find rm
-3

This is quite usefull in Unix system share via NFS or AppleTalk with OSX clients that like to populate your filesystem with these pesky files

sudo find / -iname "*.lproj" -and \! -iname "en*" -print0 | tee /dev/stderr | sudo xargs -0 rm -rfv
2009-03-09 22:08:45
User: asmoore82
Functions: find rm sudo tee xargs
3

This will get the job done in the most efficient way -

spawning only one `rm` process.

"On-the-fly" find data is displayed through `tee` and

you should have plenty of time to ctrl-c if needed before it's too late.

You may need to re-run this after major Software Updates.

To leave more languages in, add more ``-and \! -iname "lang*"'' statements:

sudo find / -iname "*.lproj" -and \! -iname "en*" -and \! -iname "spanish*" -print0 | tee /dev/stderr | sudo xargs -0 rm -rfv

**Edit: note the 2nd sudo near the end of the pipeline - this is necessary.

find . -iname \*.mp3 -print0 | xargs -0 mp3gain -krd 6 && vorbisgain -rfs .
2009-03-09 18:11:35
User: Viaken
Functions: find xargs
9

Run this in the directory you store your music in.

mp3gain and vorbisgain applies the ReplayGain normalization routine to mp3 and ogg files (respectively) in a reversible way.

ReplayGain uses psychoacoustic analysis to make all files sound about the same loudness, so you don't get knocked out of your chair by loud songs after cranking up the volume on quieter ones.