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,049 results
find . -printf '%.5m %10M %#9u %-9g %TY-%Tm-%Td+%Tr [%Y] %s %p\n'|sort -nrk8|head
find /srv/code -maxdepth 4 -type f -regex ".*\(\(package\|composer|npm\\|bower\)\.json\|Gemfile\|requirements\.txt\\|\.gitmodules\)"
2014-11-28 16:34:35
User: renoirb
Functions: find
Tags: bash git PHP ruby
0

List all dependencies manifests so you can install them.

In a scenario where you want to deploy a number of web applications and run their dependency managers, how could you run all of them in a systematic order.

One of the complexity is to ensure you get only your own top level dependencies. That way, you don recursively call development dependencies of your own dependencies.

Otherwise you might end up discovering dependency management manifests that are already been pulled by your own projects.

# Using this command

This command helps me find them and I can then run what?s required to pull them from their respective sources.

This command assumes the following:

1. Your code checkouts are in a flat repository layout (i.e. not nested).

2. Finds manifests for:

- NPM (nodejs),

- Composer (php),

- bower,

- requirements.txt (Python), and

- git submodules

find . -type f -name "*\?*" | while read f;do mv "$f" "${f//[^0-9A-Za-z.\/\(\)\ ]/_}";done
2014-11-28 14:55:27
User: miccaman
Functions: find mv read
Tags: bash find mv
1

replace all "?" characters in filename to underscore

find . -type d -name "*\?*" | while read f;do mv "$f" "${f//[^0-9A-Za-z.\/\(\)\ ]/_}";done
2014-11-28 14:52:46
User: miccaman
Functions: find mv read
Tags: bash find mv
0

rename all dirs with "?" char in name, leave spaces and () in place

touch -t 197001010000 ./tmp && find . -newer ./tmp && rm -f ./tmp
2014-11-18 00:29:26
User: sergeylukin
Functions: find rm touch
-2

Sometimes you just want to operate on files that were created after specific date. This command consists of 3 commands:

- Create a dummy file with the custom date

- Find all files with "creation time" further than our custom date by using `-newer` find option. Add your crazy stuff here, like moving, deleting, printing, etc.

- Remove the dummy file

find ./i18n -name "*.po" | while read f; do msgfmt $f -o ${f%.po}.mo; done
2014-11-14 19:14:35
User: sergeylukin
Functions: find read
0

This command takes all `.po` files inside `i18n` directory and compiles them to `.mo` files with same basename

find -type f -exec ffmpeg -i "{}" "{}".mp3 \;
find . -name *.png | xargs optipng -nc -nb -o7 -full
find . -name '*.jar' | xargs -l jar vtf | grep XXX.java
find -not -empty -type f -printf "%-30s'\t\"%h/%f\"\n" | sort -rn -t$'\t' | uniq -w30 -D | cut -f 2 -d $'\t' | xargs md5sum | sort | uniq -w32 --all-repeated=separate
2014-10-19 02:00:55
User: fobos3
Functions: cut find md5sum sort uniq xargs
0

Finds duplicates based on MD5 sum. Compares only files with the same size. Performance improvements on:

find -not -empty -type f -printf "%s\n" | sort -rn | uniq -d | xargs -I{} -n1 find -type f -size {}c -print0 | xargs -0 md5sum | sort | uniq -w32 --all-repeated=separate

The new version takes around 3 seconds where the old version took around 17 minutes. The bottle neck in the old command was the second find. It searches for the files with the specified file size. The new version keeps the file path and size from the beginning.

find . -iname "*.mp4" -print0 | xargs -0 mv --verbose -t /media/backup/
cd tmp ; find . |cpio -o -H newc| gzip > ../initrd.gz
2014-09-24 14:07:54
User: akiuni
Functions: cd cpio find gzip
0

This commands compresses the "tmp" directory into an initrd file.

for file in $(find /var/backup -name "backup*" -type f |sort -r | tail -n +10); do rm -f $file; done ; tar czf /var/backup/backup-system-$(date "+\%Y\%m\%d\%H\%M-\%N").tgz --exclude /home/dummy /etc /home /opt 2>&- && echo "system backup ok"
2014-09-24 14:04:11
User: akiuni
Functions: date echo file find rm sort tail tar
Tags: backup Linux cron
0

this command can be added to crontab so as to execute a nightly backup of directories and store only the 10 last backup files.

find . -name "*.pdf" -print0 | xargs -r0 stat -c %y\ %n | sort|awk '{print $4}'|gawk 'BEGIN{ a=1 }{ printf "mv %s %04d.pdf\n", $0, a++ }' | bash
2014-09-23 06:40:45
Functions: awk find gawk printf stat xargs
Tags: sort awk find xargs
0

Caution: distructive overwrite of filenames

Useful for concatenating pdfs in date order using pdftk

find -type f -exec ffprobe -i "{}" -show_entries format=duration -v quiet -of csv="p=0" \; | paste -sd+ | bc
2014-08-29 23:45:09
User: Hype
Functions: find paste
0

Recursive. Ignores non-media files. Requires ffprobe, paste, and bc.

function findOlderThan () { find . -mmin -$((($(date "+%s") - $(stat -c %Y $1))/60)) -type f ; }
2014-08-29 17:52:34
User: RobertDeRose
Functions: date find stat
Tags: find date stat
0

This function will find the modification time in unix_time of the given file, then calculate the number of minutes from now to then and then find all files modified in that range.

find . -type d -print0 | while read -d $'\0' dir; do cd "$dir"; echo " process $dir"; find . -maxdepth 1 -name "*.ogg.mp3" -exec rename 's/.ogg.mp3/.mp3/' {} \; ; cd -; done
2014-08-25 11:28:43
Functions: cd echo find read rename
2

This is probably overkill, but I have some issues when the directories have spaces in their names.

The

find . -type d -print0 | while read -d $'\0' dir; do xxx; done

loops over all the subdirectories in this place, ignoring the white spaces (to some extend).

cd "$dir"; echo " process $dir"; cd -;

goes to the directory and back. It also prints some info to check the progress.

find . -maxdepth 1 -name "*.ogg.mp3" -exec rename 's/.ogg.mp3/.mp3/' {} \;

renames the file within the current directory.

The whole should work with directories and file names that include white spaces.

find /target_directory -type f -mmin -60 --mindepth 2
2014-08-09 06:59:34
User: vikranth
Functions: find
0

To search for files in /target_directory and all its sub-directories, that have been modified in the last 60 minutes:

find /target_directory -type f -mmin -60

To search for files in /target_directory and all its sub-directories, that have been modified in the last 2 days:

find /target_directory -type f -mtime -2

To search for files in /target_directory and all its sub-directories no more than 3 levels deep, that have been modified in the last 2 days:

find /target_directory -type f -mtime -2 -depth -3
find /var/log -type f -iregex '.*[^\.][^0-9]+$' -not -iregex '.*gz$' 2> /dev/null | xargs tail -n0 -f | ccze -A
2014-07-29 17:11:17
User: rubo77
Functions: find tail xargs
Tags: unix ccze logging
4

This will show all changes in all log files under /var/log/ that are regular files and don't end with `gz` nor with a number

find -L -type l
2014-07-22 19:52:18
Functions: find
6

-L tells find to follow symbolic links, so -type l will only return links it can't follow (i.e., those that are broken).

find -type l -xtype l
find . -name "*.DS_Store" -type f -delete
fn() { find . -iname "*$1*" -print; }
2014-07-15 05:30:59
User: suprjami
Functions: find
0

A simple bash function to the find command. I use this much more than find itself.

find . -name "*.php" -exec php -l {} \; | grep found
2014-07-11 14:17:34
User: azizsaleh
Functions: find grep
0

Same but will only returns the invalid file (great when emailing the list to the team).

find . -type f -iname '*.flac' | while read i; do mv -- "$i" "$i.tmp"; gst-launch filesrc location="$i.tmp" ! flacdec ! flacenc quality=8 ! filesink location="${i%.tmp}"; rm -- "$i.tmp"; done
2014-07-10 19:21:22
User: qdrizh
Functions: find mv read rm
0

Sometimes I get FLAC files that RhythmBox can't play but VLC can. So I re-encode them using GStreamer at highest compression.