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 tagged find from sorted by
Terminal - Commands tagged find - 360 results
files -type f | xargs -n100 | while read l; do mkdir $((++f)); cp $l $f; done
2011-02-15 23:15:16
User: flatcap
Functions: cp mkdir read xargs
-2

Take a folder full of files and split it into smaller folders containing a maximum number of files. In this case, 100 files per directory.

find creates the list of files

xargs breaks up the list into groups of 100

for each group, create a directory and copy in the files

Note: This command won't work if there is whitespace in the filenames (but then again, neither do the alternative commands :-)

find /src/dir/ -mtime -10 -printf %P\\0|rsync --files-from=- --from0 /src/dir/ /dst/dir/
2011-01-18 22:23:47
User: pauli
Functions: find rsync
Tags: find rsync
1

'-mtime -10' syncs only files newer 10 days (-mtime is just one example, use whatever find expressions you need)

printf %P: File's name with the name of the command line argument under which it was found removed.

this way, you can use any src directory, no need to cd into your src directory first.

using \\0 in printf and a corresponding --from0 in rsync ensures that even filenames with newline characters work (thanks syssyphus for #3808).

both, #1481 and #3808 just work if you either copy the current directory (.) , or the filesystem root (/), otherwise the output from find and the source dir from rsync just don't match. #7685 works with an arbitrary source directory.

find . -xdev -path ./junk_dir -prune -o -type d -name "dir_name" -a -print
2011-01-16 10:08:10
Functions: find
Tags: find
1

Consider using this cmd when:

1. You are planning to traverse a big directory.

2. There is a subdir you don't want find to decend to. (entirely ignore)

3. You don't want find to decend to any mounted filesystems under this dir.

* The -xdev flag tells find do not go to other filesystems.

* -path ./junk_dir -prune is the pattern to ignore ./junk_dir entirely.

* The rest is the typical search and print.

To ignore multiple subdirs, you can just iterate the pattern, e.g.

find . -path ./junk1 -prune -o -path ./junk2 -prune ...

If you do want to include other filesystems, then remove -xdev flag.

If you want to search files, then change -type d to -type f.

find . -type f -name "*.tar" -printf [%f]\\n -exec tar -tf {} \; | grep -iE "[\[]|<filename>"
2011-01-06 13:01:38
Functions: find grep tar
Tags: find grep tar
1

A quick find command to identify all TAR files in a given path, extract a list of files contained within the tar, then search for a given string in the filelist. Returns to the user as a list of TAR files found (enclosed in []) followed by any matching files that exist in that archive. TAR can easily be swapped for JAR if required.

find /name/of/dir/ -name '*.txt' | xargs grep 'text I am searching for'
2011-01-05 15:20:40
User: erickb
Functions: find grep xargs
Tags: find xargs grep
1

recursively search dir for a a particular file type, search each file for a particular text.

find -type f -exec du -sh {} + | sort -rh | head
find -type f | xargs -I{} du -s "{}" | sort -rn | head | cut -f2 | xargs -I{} du -sh "{}"
2011-01-04 11:10:56
User: glaudiston
Functions: cut du find head sort xargs
-1

Show the top file size in human readable form

find -type f | xargs -I{} du -sk "{}" | sort -rn | head
du -sk * | sort -rn | head
2011-01-03 10:49:40
User: EBAH
Functions: du sort
3

Also:

* find . -type f -exec ls -s {} \; | sort -n -r | head -5

* find . -type f -exec ls -l {} \; | awk '{print $5 "\t" $9}' | sort -n -r | head -5

find /deep/tree/ -type f -print0|xargs -0 -n1 -I{} ln -s '{}' .
2010-12-21 13:00:33
User: dinomite
Functions: find ln xargs
Tags: find xargs links
1

If you want to pull all of the files from a tree that has mixed files and directories containing files, this will link them all into a single directory. Beware of filesystem files-per-directory limits.

find . -depth -name .svn -type d -exec rm -fr {} \;
2010-12-16 17:16:23
User: tebeka
Functions: find rm
Tags: find rm
5

-depth argument will cause find to do a "depth first" tree search, this will eliminate the "No such file or directory" error messages

find . -name "*.php" -type f -exec sed -i "\$d" '{}' \;
2010-12-09 16:42:22
User: tedkulp
Functions: find sed
Tags: sed find text
2

Used this command recently to remove the trailing ?> from all the files in a php project, which has having some unnecessary whitespace issues. Obviously, change *.php to whatever you'd like.

find . -name '*.part1.rar' -exec unrar e \{\} -pPASSWORD \;
2010-12-01 10:21:15
User: arcanis
Functions: find
Tags: find unrar
0

Extract in the current directory the content of all parted archives.

Use the same password for each one.

rm **/*.htm
2010-11-25 17:28:55
Functions: rm
Tags: find rm
-2

expands through shell and not find

but may hits the limit of max argument size for rm

(thus: for f in **/*.htm;do rm $f;done

but then I prefer the find command ;)

rm -rf *.htm
find . -type f -print0 | xargs -0 perl -pi.save -e 'tr/A-Z/a-z/'
2010-11-25 13:55:34
User: depesz
Functions: find perl xargs
Tags: perl find regex
1

In this way it doesn't have problems with filenames with spaces.

find . -type f -name '*.htm' -delete
perl -e "tr/[A-Z]/[a-z]/;" -pi.save $(find . -type f)
rm -rf `find . -type f -name *.htm`
find | egrep "\.(ade|adp|bat|chm|cmd|com|cpl|dll|exe|hta|ins|isp|jse|lib|mde|msc|msp|mst|pif|scr|sct|shb|sys|vb|vbe|vbs|vxd|wsc|wsf|wsh)$"
2010-11-23 16:53:55
User: poulter7
Functions: egrep find
-1

Returns any file in the folder which would be rejected by Gmail, if you were to send zipped version.

(Yes, you could just zip it and knock the extension off and put it back on the other side, but for some people this just isn't a solution)

find . -type f | while read line; do NEW_TS=`date -d@$((\`stat -c '%Y' $line\` + <seconds> )) '+%Y%m%d%H%M.%S'`; touch -t $NEW_TS ${line}; done
2010-11-18 14:03:32
User: angleto
Functions: find read touch
1

Increase the modification date for the files selected with the find command.

find . -type f -size +500M -exec du {} \; | sort -n
2010-11-09 18:15:44
Functions: du find sort
Tags: size find
1

Greater than 500M and sorted by size.

find / -type f -size +500M
find / -type f -size +548576 -printf "%s:%h%f\n"
find -iname '*mp3' -exec mid3iconv {} \;
2010-10-29 05:35:46
User: schlaegel
Functions: find
5

Some MP3s come with tags that don't work with all players. Also, some good tag editors like, EasyTAG output tags that don't work with all players. For example, EasyTAG saves the genre as a numeric field, which is not used correctly in Sansa MP3 players.

This command corrects the ID3 tags in MP3 files using mid3iconv, which comes with mutagen. To install Mutagen on Fedora use "yum install python-mutagen"