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,012 results
find ./ -size +10M -type f -print0 | xargs -0 ls -Ssh1 --color
find -print0 | xargs -0 -n 1 -P 4 -I {} sh -c "zcat '{}' | mysql nix"
2009-08-25 15:05:55
User: skygreg
Functions: find sh xargs
3

this command works with one gziped file per table, and restore 4 tables in parallel.

find /dir | awk '{print length, $0}' | sort -nr | sed 's/^[[:digit:]]* //' | while read dirfile; do outfile="$(echo "$(basename "$dirfile")" | unaccent UTF-8)"; mv "$dirfile" "$(dirname "$dirfile")/$outfile"; done
2009-08-24 21:24:18
User: Patola
Functions: awk basename find mv read sed sort
2

This command changes all filename and directories within a directory tree to unaccented ones. I had to do this to 'sanitize' some samba-exported trees. The reason it works might seem a little difficult to see at first - it first reverses-sort by pathname length, then it renames only the basename of the path. This way it'll always go in the right order to rename everything.

Some notes:

1. You'll have to have the 'unaccent' command. On Ubuntu, just aptitude install unaccent.

2. In this case, the encoding of the tree was UTF-8 - but you might be using another one, just adjust the command to your encoding.

3. The program might spit a few harmless errors saying the files are the same - not to fear.

find /backup/directory -name "FILENAME_*" -mtime +15 -exec rm -vf {};
find /backup/directory -name "FILENAME_*" -mtime +15 | xargs rm -vf
pgrep -u `id -u` firefox-bin || find ~/.mozilla/firefox -name '*.sqlite'|(while read -e f; do echo 'vacuum;'|sqlite3 "$f" ; done)
2009-08-22 10:36:05
User: kamathln
Functions: echo find read
11

Sqlite database keeps collecting cruft as time passes, which can be cleaned by the 'vacuum;' command. This command cleans up the cruft in all sqlite files relating to the user you have logged in as. This command has to be run when firefox is not running, or it will exit displaying the pid of the firefox running.

find / -type f -exec wc -c {} \; | sort -nr | head -100
find . -type d -empty -delete
2009-08-22 09:03:14
User: hemanth
Functions: find
Tags: find rmdir
6

You can also use, $ find . -depth -type d -exec rmdir {} \; 2>/dev/null

for i in `grep "unable to stat" /var/log/syslog | cut -d "/" -f 3 | sort | uniq`; do find /var/qmail/queue -name $i -type f -exec rm -v {} \; ; done
id=<XXXX>; find /var/spool/postfix/ -name $id -exec less {} \;
find /var/www/ -type f -name ".htaccess" -exec perl -pi -e 's/AddHandler[\s]*php(4|5)-cgi/AddHandler x-httpd-php\1/' {} \;
2009-08-21 21:55:22
User: foob4r
Functions: find perl
0

Alter "AddHandler php5-cgi .php" and "AddHandler php4-cgi .php" entries to new "AddHandler x-httpd-php5 .php" respective php4 entries in all .htaccess files under /var/www

find dir/ -type f | xargs tail -fqn0
2009-08-21 18:05:12
User: chickenzilla
Functions: find tail xargs
1

The `-q' arg forces tail to not output the name of the current file

find . -type f | grep -rl $'\xEF\xBB\xBF'
2009-08-19 13:27:09
User: pfredrik
Functions: find grep
4

Character: "?" is the Byte Order Mark (BOM) of the Unicode Standard.

Specifically it is the hex bytes EF BB BF, which form the UTF-8 representation of the BOM,

misinterpreted as ISO 8859/1 text instead of UTF-8.

find / -name "*.pdf" -exec cp -t ~/Documents/PDF {} +
2009-08-18 06:11:35
Functions: cp find
Tags: find cp for
9

I used this to copy all PDFs recursively to a selected dir

(cd SRC; find . -type d -exec mkdir TARGET/{} ";"; find . -type f -exec mv {} TARGET/{} ";")
2009-08-17 12:35:48
User: karel1980
Functions: cd find mkdir mv
0

Using a GUI file managers you can merge directories (cut and paste). This command roughly does the same (it doesn't ask for confirmation (no problem for me) and it doesn't clean up the empty SRC directories (no problem, trivial).

probably does the same:

cp -l SRC TARGET; rm -rf SRC
diff <(cd /path-1; find . -type f -print | egrep -i '\.m4a$|\.mp3$') <(cd /path-2; find . f -print | egrep -i '\.m4a$|\.mp3$')
2009-08-17 00:49:31
User: drewk
Functions: cd diff egrep find
3

diff is designed to compare two files. You can also compare directories. In this form, bash uses 'process substitution' in place of a file as an input to diff. Each input to diff can be filtered as you choose. I use find and egrep to select the files to compare.

IFS=:; find $PATH | grep pattern
2009-08-14 13:38:58
User: camspiers
Functions: find grep
Tags: bash find grep
1

Best to put it in a file somewhere in your path. (I call the file spath)

#!/bin/bash

IFS=:; find $PATH | grep $1

Usage: $ spath php

find . -type f -printf '%20s %p\n' | sort -n | cut -b22- | tr '\n' '\000' | xargs -0 ls -laSr
2009-08-13 13:13:33
User: fsilveira
Functions: cut find ls sort tr xargs
Tags: sort find ls
10

This command will find the biggest files recursively under a certain directory, no matter if they are too many. If you try the regular commands ("find -type f -exec ls -laSr {} +" or "find -type f -print0 | xargs -0 ls -laSr") the sorting won't be correct because of command line arguments limit.

This command won't use command line arguments to sort the files and will display the sorted list correctly.

find . -name "*.[ch]" -exec grep "TODO" {} +
2009-08-13 06:17:22
User: peshay
Functions: find grep
Tags: grep
2

-exec works better and faster then using a pipe

find . -name "*.[ch]" | xargs grep "TODO"
find $MAILDIR/ -type f -printf '%T@ %p\n' | sort --reverse | sed -e '{ 1,100d; s/[0-9]*\.[0-9]* \(.*\)/\1/g }' | xargs -i sh -c "cat {}&&rm -f {}" | gzip -c >>ARCHIVE.gz
find . -name '*'.tiff -exec bash -c "mogrify -format jpg -quality 85 -resize 75% {} && rm {}" \;
2009-08-10 18:27:10
Functions: bash find
4

Simple command to convert a large number of images into jpeg-format. Will delete originals after conversion.

find . -name \*.pdf -exec pdfinfo {} \; | grep Pages | sed -e "s/Pages:\s*//g" | awk '{ sum += $1;} END { print sum; }'
find . -name "-help" -exec mv {} help.txt \;
find . -type f -print0 | xargs -0 -P 4 -n 40 grep -i foobar
2009-08-05 23:18:44
User: ketil
Functions: find grep xargs
4

xargs -P N spawns up to N worker processes. -n 40 means each grep command gets up to 40 file names each on the command line.