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 - 357 results
find -type f -printf "%S\t%p\n" 2>/dev/null | gawk '{if ($1 < 1.0) print $1 $2}'
2011-07-02 19:22:49
Functions: find gawk
1

Prints the path/filename and sparseness of any sparse files (files that use less actual space than their total size because the filesystem treats large blocks of 00 bytes efficiently).

find . -name '*.java' -o -name '*.xml' | grep -v '\.svn' | xargs wc -l
2011-06-30 12:45:40
User: ewilson
Functions: find grep wc xargs
Tags: find grep wc
-1

There's nothing particularly novel about this combination of find, grep, and wc, I'm just putting it here in case I want it again.

ack "search pharse" *.[ch]
find . -type f -printf %s\\n | numsum
2011-06-27 12:39:16
User: Strawp
Functions: find
Tags: find numsum
-2

pipe into

| sed "s/$/\/(1024\*1024\*1024)/" | bc

to get size in GB

find . -type f -newermt "2010-01-01" ! -newermt "2010-06-01"
2011-06-26 09:52:26
User: flatcap
Functions: find
Tags: find dates
30

Find files in a specific date range - in this case, the first half of last year.

-newermt = modification time of the file is more recent than this date

GNU find allows any date specfication that GNU date would accept, e.g.

find . -type f -newermt "3 years ago" ! -newermt "2 years ago"

or

find . -type f -newermt "last monday"
touch -t 201001010000 begin; touch -t 201012312359.59 end; find . -newer begin -a ! -newer end
2011-06-22 20:09:05
Functions: find touch
Tags: find dates touch
1

Example above will recursively find files in current directory created/modified in 2010.

find . -name "*.[ch]" -print | xargs grep -i -H "search phrase"
2011-06-05 23:27:30
User: jblaine
Functions: find grep xargs
Tags: find grep
-3

Original submitter's command spawns a "grep" process for every file found. Mine spawns one grep with a long list of all matching files to search in. Learn xargs, everyone! It's a very powerful and always available tool.

perl -MFile::Find -e"finddepth(sub{rmdir},'.')"
find -L / -samefile /path/to/file -exec ls -ld {} +
2011-04-27 06:14:15
User: eightmillion
Functions: find ls
17

This command finds and prints all the symbolic and hard links to a file. Note that the file argument itself be a link and it will find the original file as well.

You can also do this with the inode number for a file or directory by first using stat or ls or some other tool to get the number like so:

stat -Lc %i file

or

ls -Hid file

And then using:

find -L / -inum INODE_NUMBER -exec ls -ld {} +
find . -maxdepth 1 -type f -not -iname '*.jpg' -ls |awk '{TOTAL+=$7} END {print int(TOTAL/(1024^2))"MB"}'
2011-04-26 18:18:37
User: mack
Functions: awk find
Tags: awk find filesize
1

With this sentence we can estimate the storage size of all files not named *.jpg on the current directory.

The syntax is based on Linux, for Unix compliance use:

find ./* -prune ! -name '*.jpg' -ls |awk '{TOTAL+=$7} END {print int(TOTAL/(1024^2))"MB"}'

We can change the jpg extension for whatever extension what we need

find . -maxdepth 1 -type f -mtime +60 -ls
find . -type f -name \*.php | while IFS="" read i; do expand -t4 "$i" > "$i-"; mv "$i-" "$i"; done
2011-04-08 12:53:14
User: flatcap
Functions: expand find mv read
-4

Recursively find php files and replace tab characters with spaces.

Options:

"\*.php" -- replace this with the files you wish to find

"expand" -- replace tabs with spaces (use "unexpand" to replace spaces with tabs)

"-t4" -- tabs represent 4 spaces

Note: The IFS="" in the middle is to prevent 'read' from eating leading/trailing whitespace in filenames.

find -L . -type l
find . -type l | xargs file | grep broken
find . -depth -print -execdir rename -f 'y/A-Z/a-z/' '{}' \;
2011-03-25 03:10:27
User: rsimpson
Functions: find rename
Tags: bash find mv rename tr
1

easier way to recursively change files to lowercase using rename instead

find . -name '*.xml' -type f -print | xargs du -ch
2011-03-22 00:47:42
User: nathwill
Functions: du find xargs
Tags: find du type
0

print sum of disk usage for filetype within current dir and subdirs

find . -type d -name .svn -prune -o -type f -print0 | xargs -r0 ...
find . -type f ! -iwholename \*.svn\* -print0 [ | xargs -0 ]
2011-03-21 16:45:35
User: alustenberg
Functions: find xargs
1

for when find . -print | grep -v .svn | xargs doesnt cut it.

alias big='BIG () { find . -size +${1}M -ls; }; BIG $1'
2011-03-10 06:33:00
User: greggster
Functions: alias find
0

This is for bash - make an alias - also a good blueprint for making aliases that take arguments to functions. If for Solaris use "-size +${1}000000c" to replace "-size +${1}M"

find . -maxdepth 1 -type d | grep -Pv "^.$" | sort -rn --field-separator="-" | sed -n '3,$p' | xargs rm -rf
find . -name "*.java" -type f -perm +600 -print | xargs -I _ sh -c 'grep -q hexianmao _ && iconv -f gb2312 -t utf8 -o _ -c _ '
2011-03-08 13:02:25
User: Sunng
Functions: find iconv sh xargs
Tags: find xargs iconv
-1

One of my friends committed his code in the encoding of GB2312, which broke the build job. I have to find his code and convert.

find `pwd` -iname *SEARCH_STRING* >> ~/PLAYLIST_NAME.m3u
2011-02-27 09:00:38
User: wxl
Functions: find
Tags: find playlist m3u
0

must be in the directory containing the track

outputs to ~ but could be replaced with whatever you like e.g. ~/music/

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.