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,048 results
find /path/to/images -name '*.JPG' -exec rename "s/.JPG/.jpg/g" \{\} \;
2010-01-02 19:12:37
User: renich
Functions: find rename
Tags: find rename
8

This command is useful for renaming a clipart, pic gallery or your photo collection. It will only change the big caps to small ones (on the extension).

find . -exec grep $foo {} \; -print
2009-12-30 17:41:44
User: linuxgeek
Functions: find grep
-1

The command will help to print the location of the pattern. Above command will print all the files which contain variable "$foo" along with line containing that pattern.

Specify pattern after "grep"

find dir -size -1024k -type f -print0 | du --files0-from - -bc
2009-12-29 01:33:55
User: bhepple
Functions: dir du find
Tags: size sum
2

The original didn't use -print0 which fails on weird file names eg with spaces.

The original parsed the output of 'ls -l' which is always a bad idea.

find dir -size -1024k -type f | xargs -d $'\n' -n1 ls -l | cut -d ' ' -f 5 | sed -e '2,$s/$/+/' -e '$ap' | dc
2009-12-28 04:23:01
User: zhangweiwu
Functions: cut dir find ls sed xargs
Tags: size sum
1

The command gives size of all files smaller than 1024k, this information, together with disk usage, can help determin file system parameter (e.g. block size) or storage device (e.g. SSD v.s. HDD).

Note if you use awk instead of "cut| dc", you easily breach maximum allowed number of records in awk.

find /home/bubo/ -type f \( -iname \*.jpg -print0 , -iname \*.png -print0 , -iname \*gif -print0 \) | du -cm --files0-from - | tail -1
find . -name '*png' -printf '%h\0' | xargs -0 ls -l --hide=*.png | grep -ZB1 ' 0$'
find . -print -exec chmod 777 {} \;
2009-12-23 13:16:31
User: ringlerun
Functions: chmod find
-10

sometimes if directories are too deep, chmod -R fails... in those cases, a find comes in most handy :)

find | xargs chmod 777
find . \! -type d | rev | sort | rev | tar c --files-from=- --format=ustar | bzip2 --best > a.tar.bz2
2009-12-20 14:04:39
User: pornel
Functions: bzip2 c++ find rev sort tar
2

Avoids creating useless directory entries in archive, and sorts files by (roughly) extension, which is likely to group similar files together for better compression. 1%-5% improvement.

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"