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 . -name "*.php" -exec php -l {} \;
find ./wp-content/themes/rotce2009/ -name '*.php' -type f | xargs sed -i 's/<? /<?php /g'
tar -cvzf arch.tgz $(find /path/dir -not -type d)
2009-12-15 13:46:54
User: pysquared
Functions: find tar
3

If you give tar a list of filenames, it will not add the directories, so if you don't care about directory ownership or permissions, you can save some space.

Tar will create directories as necessary when extracting.

This command is limited by the maximum supported size of the argument list, so if you are trying to tar up the whole OS for instance, you may just get "Argument list too long".

find . -maxdepth 1 -type d -not -name . -exec du -sh {} +
2009-12-11 14:37:26
User: sorpigal
Functions: du find
-6

Parsing the output of ls is never a good idea for any reason. Using find this way:

- works with files that have spaces in their names.

- actually lists "sub folders" and not of all files and folders.

- does not break if there are a huge number of files in the current directory.

find . -iname "*.jpg" -print0 | tr '[A-Z]' '[a-z]' | xargs -0 cp --backup=numbered -dp -u --target-directory {location} &
2009-12-10 08:47:04
User: oracular
Functions: cp find tr xargs
4

Use if you have pictures all over the place and you want to copy them to a central location

Synopsis:

Find jpg files

translate all file names to lowercase

backup existing, don't overwrite, preserve mode ownership and timestamps

copy to a central location

find /path/to/dir/ -type f -exec rm {} +
mkdir myicons; find /usr/share/icons/ -type f -exec cp {} ./myicons/ \;
mkdir myicons && find /usr/share/icons/ -type f | xargs cp -t myicons
2009-12-09 17:43:28
User: rodolfoap
Functions: cp find mkdir xargs
Tags: icons
2

Today I needed to choose an icon for an app. My simpler way: put all of /usr/share/icons in myicons folder and brows'em with nautilus. Then rm -r 'ed the entire dir.

find /path/to/dir -type f -delete
2009-12-09 01:30:52
User: SlimG
Functions: find
9

Optimal way of deleting huge numbers of files

Using -delete is faster than:

find /path/to/dir -type f -print0 | xargs -0 rm find /path/to/dir -type f -exec rm {} + find /path/to/dir -type f -exec rm \-f {} \;
find . -type f -exec sed -i s/oldstring/newstring/g {} +
2009-12-09 00:46:13
User: SlimG
Functions: find sed
Tags: sed find
14

This command find all files in the current dir and subdirs, and replace all occurances of "oldstring" in every file with "newstring".

find . -type f -iname '*.mp3' -exec cp {} ~/mp3/ \;
2009-12-09 00:19:14
User: SlimG
Functions: cp find
1

This command copies all filenames in the current dir and subdirs that end in .mp3 regardless of case (also matches .MP3 .mP3 and .Mp3)

It copies all the files to the "mp3" folder in your home directory.

If you want to see the files that are beeing copied, replace "cp {}" with "cp -v {}"

find . -iname '*.mp3' -type f -print0 | xargs -I{} -0 cp {} </path>
2009-12-08 20:50:48
User: sputnick
Functions: cp find xargs
5

No problem with word splitting. That should works on many Unix likes.

find . -name '*.mp3' -type f -exec sh -c 'exec cp -f "$@" /home/user/dir' find-copy {} +
2009-12-08 19:31:16
User: mariusz
Functions: cp find sh
1

I used this command to recursively gather all mp3 files that were previously imported into their own directories (sorted by band name) in Songbird.

find . -type d -exec sh -c "normalize-audio -b \"{}\"/*.mp3" \;
2009-12-08 03:13:13
Functions: find sh
-2

Execute this in the root of your music library and this recurses through the directories and normalizes each folder containing mp3s as a batch. This assumes those folders hold an album each. The command "normalize-audio" may go by "normalize" on some systems.

find . -type f -perm +200 -print
find . -type f | perl -lne 'print if -T;' | xargs egrep "somepattern"
find . -type l | perl -lne 'print if ! -e'
find -mindepth 1 -maxdepth 1 -name .\*
2009-11-24 22:18:33
User: rodolfoap
Functions: find
1

find makes it easier, filtering . and ..

maxdepth could be removed, finding entries recursively. Removing mindepth causes . to appear

find ./ -name $1 -exec grep -H -n $2 '{}' ';'
touch -t "YYYYMMDDhhmm.ss" dummy ; find . -anewer dummy
2009-11-21 04:05:45
Functions: find touch
2

touch a dummy file with the specified date, then use find with -anewer .

find . -maxdepth 1 -type f -size +1M -printf "%f:%s\n" | sort -t":" -k2
find -amin +[n] -delete
2009-11-20 17:15:28
User: TeacherTiger
Functions: find
-2

Deletes files older than "n" minutes ago. Note the plus sign before the n is important and means "greater than n". This is more precise than atime, since atime is specified in units of days. NOTE that you can use amin/atime, mmin/mtime, and cmin/ctime for access, modification, and change times, respectively. Also, using -delete is faster than piping to xargs, since no piping is needed.

find <dir> -printf '%p : %A@\n' | awk '{FS=" : " ; if($2 < <time in epoc> ) print $1 ;}' | xargs rm --verbose -fr ;
2009-11-20 16:31:58
User: angleto
Functions: awk find rm xargs
-2

remove files with access time older than a given date.

If you want to remove files with a given modification time replace %A@ with %T@. Use %C@ for the modification time.

The time is expressed in epoc but is easy to use any other ordered format.

find ./ -type f -print0 | xargs -0 file -iNf - | grep video | cut -d: -f1
2009-11-19 06:05:36
User: jnash
Functions: cut file find grep xargs
0

Uses mime-type of files rather than relying on file extensions to find files of a certain type.

This can obviously be extended to finding files of any other type as well.. like plain text files, audio, etc..

In reference to displaying the total hours of video (which was earlier posted in command line fu, but relied on the user having to supply all possible video file formats) we can now do better:

find ./ -type f -print0 | xargs -0 file -iNf - | grep video | cut -d: -f1 | xargs -d'\n' /usr/share/doc/mplayer/examples/midentify | grep ID_LENGTH | awk -F "=" '{sum += $2} END {print sum/60/60; print "hours"}'
for F in $(find ./ -name "*.tgz") ; do tar -tvzf $F ; done
2009-11-11 00:50:52
User: alchandia
Functions: find tar
Tags: tar
-2

The magic is performed by the parameter -t