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,020 results
for f in $(find * -maxdepth 0 -type f); do file=$(find ~/target -name $f); if [ -n "$file" ]; then cp $file ${file}.bak; mv $f $file; fi; done
2009-07-08 10:18:06
User: sanmiguel
Functions: cp find mv
0

You could start this one with

for f in *; do

BUT using the find with "-type f" ensures you only get files not any dirs you might have

It'll also create backups of the files it's overwriting

Of course, this assumes that you don't have any files with duplicated filenames in your target structure

find / -type f -size +1000000000c
find -L /proc/*/fd -links 0 2>/dev/null
2009-06-26 18:42:51
User: res0nat0r
Functions: find
Tags: du proc df
12

Oracle DBA remove some logfiles which are still open by the database and he is complaining the space has not been reclaimed? Use the above command to find out what PID needs to be stopped. Or alternatively recover the file via:

cp /proc/pid/fd/filehandle /new/file.txt
find . -depth -type d -exec du -s {} \; | sort -k1nr
2009-06-23 20:52:35
User: mohan43u
Functions: du find sort
Tags: sort find du
4

somewhat faster version to see the size of our directories. Size will be in Kilo Bytes. to view smallest first change '-k1nr' to '-k1n'.

find . -name "*.sql" -print0 | wc -l --files0-from=-
2009-06-22 17:45:03
User: vincentp
Functions: find wc
Tags: find wc count line
2

This command gives you the number of lines of every file in the folder and its subfolders matching the search options specified in the find command. It also gives the total amount of lines of these files.

The combination of print0 and files0-from options makes the whole command simple and efficient.

find directory -size +nnn
find . -type f ! -perm /g=r -exec chmod g+r {} +
2009-06-17 13:39:59
User: sanmiguel
Functions: chmod find
Tags: find chmod
3

Makes any files in the current directory (and any sub-directories) group-readable.

Using the "! -perm /g=r" limits the number of files to only those that do not already have this property

Using "+" on the end of the -exec body tells find to build the entire command by appending all matching files before execution, so invokes chmod once only, not once per file.

find . -type f -name *.ext -exec cat {} > file.txt \;
2009-06-17 11:33:14
User: realgt
Functions: cat find
2

Useful if you have to put together multiple files into one and they are scattered across subdirectories. For example: You need to combine all .sql files into one .sql file that would be sent to DBAs as a batch script.

You do get a warning if you create a file by the same extension as the ones your searching for.

find . -type f -name *.sql -exec cat {} > BatchFile.txt \;

find `echo "${PATH}" | tr ':' ' '` -type f | while read COMMAND; do man -f "${COMMAND##*/}"; done
2009-06-13 19:56:24
User: mohan43u
Functions: find man read tr
Tags: man find read while tr
3

Obviously, you can replace 'man' command with any command in this command line to do useful things. I just want to mention that there is a way to list all the commands which you can execute directly without giving fullpath.

Normally all important commands will be placed in your PATH directories. This commandline uses that variable to get commands. Works in Ubuntu, will work in all 'manpage' configured *nix systems.

find . -iname '*.jar' | xargs du -ks | cut -f1 | xargs echo | sed "s/ /+/g" | bc
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 . -type f -print0|xargs -0 md5sum|sort|perl -ne 'chomp;$ph=$h;($h,$f)=split(/\s+/,$_,2);print "$f"."\x00" if ($h eq $ph)'|xargs -0 rm -v --
2009-06-07 03:14:06
Functions: find perl rm xargs
18

This one-liner will the *delete* without any further confirmation all 100% duplicates but one based on their md5 hash in the current directory tree (i.e including files in its subdirectories).

Good for cleaning up collections of mp3 files or pictures of your dog|cat|kids|wife being present in gazillion incarnations on hd.

md5sum can be substituted with sha1sum without problems.

The actual filename is not taken into account-just the hash is used.

Whatever sort thinks is the first filename is kept.

It is assumed that the filename does not contain 0x00.

As per the good suggestion in the first comment, this one does a hard link instead:

find . -xdev -type f -print0 | xargs -0 md5sum | sort | perl -ne 'chomp; $ph=$h; ($h,$f)=split(/\s+/,$_,2); if ($h ne $ph) { $k = $f; } else { unlink($f); link($k, $f); }'
find -L /path/to/check -type l -delete
2009-06-06 16:07:04
Functions: find
14

If you don't want to delete them, but just want to list them, do

find -L /path -type l

If you want to delete them with confirmation first, do

find -L /path -type l -exec rm -i {} +

Using the -L flag follows symlinks, so the -type l test only returns true if the link can't be followed, or is a symlink to another broken symlink.

find ./* -ctime -1 | xargs ls -ltr --color
2009-06-05 13:53:26
User: gnuyoga
Functions: find ls xargs
3

added alias in ~/.bashrc

alias lf='find ./* -ctime -1 | xargs ls -ltr --color'

find . -name "*jpg" -exec jpeginfo -c {} \; | grep -E "WARNING|ERROR"
2009-06-03 22:08:48
User: vincentp
Functions: find grep
11

Finds all corrupted jpeg files in current directory and its subdirectories. Displays the error or warning found.

The jpeginfo is part of the jpeginfo package in debian.

Should you wish to only get corrupted filenames, use cut to extract them :

find ./ -name *jpg -exec jpeginfo -c {} \; | grep -E "WARNING|ERROR" | cut -d " " -f 1
find /var/log -type f -exec file {} \; | grep 'text' | cut -d' ' -f1 | sed -e's/:$//g' | grep -v '[0-9]$' | xargs tail -f
2009-06-03 09:47:08
User: mohan43u
Functions: cut file find grep sed tail xargs
Tags: tail
5

Works in Ubuntu, I hope it will work on all Linux machines. For Unixes, tail should be capable of handling more than one file with '-f' option.

This command line simply take log files which are text files, and not ending with a number, and it will continuously monitor those files.

Putting one alias in .profile will be more useful.

find /var/logs -name * | xargs tar -jcpf logs_`date +%Y-%m-%e`.tar.bz2
find ~/Desktop/ \( -regex '.*/\..*' \) -print -exec rm -Rf {} \;
find <directory path> -mtime +365 -and -not -type d -delete
find ./ -type f -exec sed -i 's/\t/ /g' {} \;
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 / -iname '*.pdf' -print -exec pdftotext '{}' - \; | grep --color -i "unix"
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/