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 - 354 results
find . -type d -exec chmod 755 {} \;
2009-06-07 08:17:06
User: marssi
Functions: chmod find
Tags: find chmod
1

"find . -type d -print0 | xargs -0 chmod 755"

thanks masterofdisaster

find /var/logs -name * | xargs tar -jcpf logs_`date +%Y-%m-%e`.tar.bz2
find ~/Desktop/ \( -regex '.*/\..*' \) -print -exec rm -Rf {} \;
date -d '1 day ago'; date -d '11 hour ago'; date -d '2 hour ago - 3 minute'; date -d '16 hour'
2009-06-01 10:41:56
User: LrdShaper
Functions: date
5

With this command you can get a previous or future date or time. Where can you use this? How about finding all files modified or created in the last 5 mins?

touch -t `echo $(date -d "5 minute ago" "+%G%m%d%H%M.%S")` me && find . -type f -newer me

List all directories created since last week?

touch -t `echo $(date -d "1 week ago" "+%G%m%d%H%M.%S")` me && find . -type d -cnewer me

I'm sure you can think of more ways to use it. Requires coreutils package.

for f in `find ./ -name "*.zip"` ; do p=`pwd`; d=`dirname $f`; cd $d; b=`basename $f`; unzip $b; cd $p; done
2009-05-30 03:42:53
User: packetloss
Functions: cd
Tags: find unzip
-3

unzips all zip files in any subdirectory under the current directory. The zip files are unzipped in their respective subdirs

sort -n <( for i in $(find . -maxdepth 1 -mindepth 1 -type d); do echo $(find $i | wc -l) ": $i"; done;)
find . -uid 0 -print0 | xargs -0 chown foo:foo
2009-05-27 19:52:13
User: abcde
Functions: chown find xargs
1

In the example, uid 0 is root. foo:foo are the user:group you want to make owner and group. '.' is the "current directory and below." -print0 and -0 indicate that filenames and directories "are terminated by a null character instead of by whitespace."

diff <(cd dir1 && find | sort) <(cd dir2 && find | sort)
2009-05-21 04:44:29
User: mbirk
Functions: cd diff find
Tags: bash diff find
30

This uses Bash's "process substitution" feature to compare (using diff) the output of two different process pipelines.

find . -not \( -name .svn -prune \)
2009-05-20 19:45:24
User: mbirk
Functions: find
Tags: svn find
5

The "find" command can be annoying when used inside of a Subversion (or CVS) working directory. Obviously, you can combine this with other predicates and commands to create a more elaborate pipeline:

find /var/svn -type f -not \( -name .svn -prune \) -print0 | xargs -0 md5sum

Note: You can use my "dont-go-there.sh" script to wrap the "find" command and do this automatically at http://forwardlateral.com/blog/2006/02/27/dont-go-there/

find /home/fizz -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort
count() { find $@ -type f -exec cat {} + | wc -l; }
find . -type d -exec rmdir {} \;
2009-05-19 08:59:15
User: sharfah
Functions: find rmdir
Tags: find rm
1

Deletes empty directories and prints an error if directory is not empty.

find /var -mount -ls -xdev | /usr/bin/sort -nr +6 | more
find . -name "\.svn" -exec rm -rf {} ";"
find . -name "*.[ch]" -exec grep -i -H "search pharse" {} \;
2009-05-06 15:22:49
User: bunedoggle
Functions: find grep
Tags: find grep
33

I have a bash alias for this command line and find it useful for searching C code for error messages.

The -H tells grep to print the filename. you can omit the -i to match the case exactly or keep the -i for case-insensitive matching.

This find command find all .c and .h files

while :; do xfconf-query -c xfce4-desktop -p /backdrop/screen0/monitor0/image-path -s "$(find <image-directory> -type f -iregex '.*\.\(bmp\|gif\|jpg\|png\)$' | sort -R | head -1)"; sleep 30m; done
2009-04-30 03:09:52
Functions: sleep
Tags: bash find xfce
2

Change your wallpaper every thirty minutes (or however long you like, I suppose) to a randomly selected image in a directory and subdirectories. Bear in mind this is not safe to use if anyone else has write access to your image directory.

find . -path ./mnt -prune -o -path ./lost+found -prune -o -path ./sys -prune -o -path ./proc -prune -o -print | cpio -pumd /destination && mkdir /destination/mnt/ && mkdir /destination/proc && mkdir /destination/sys
2009-04-28 22:14:45
User: nutria
Functions: cpio find mkdir
Tags: find cpio
1

Clone linux installation.

find . | cpio -pumdv /destination
2009-04-28 22:08:30
User: nutria
Functions: cpio find
Tags: find cpio
3

Copy every file from current directory to destination preserving modification time.

find -writable
2009-04-11 22:16:35
User: kFiddle
Functions: find
4

Have a grudge against someone on your network? Do a "find -writable" in their directory and see what you can vandalize! But seriously, this is really useful to check the files in your own home directory to make sure they can't inadvertently be changed by someone else's wayward script.

find . -name "*.py" -exec grep -n -H -E "^(import|from) math" {} \;
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

mv `find .zip ./` .
2009-03-27 04:42:48
User: smcpherson
Functions: mv
Tags: find
-8

This is useful if you have a collection of files in folders (for example, a bunch of .zip files that are contained in folders) and you want to move them all to a common folder.

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 -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 '_'.