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,018 results
find . -name *.properties -exec /bin/echo {} \; -exec cat {} \; | grep -E 'listen|properties'
find . \( -not -path "./boost*" \) -type f \( -name "*.jpg" -or -name "*.png" -or -name "*.jpeg" \) 2>/dev/null
2014-02-13 09:11:11
Functions: find
0

If you need to find some pictures on your disk but excluding some path.

find /logdir -type f -mtime +7 -print0 | xargs -0 -n 1 nice -n 20 bzip2 -9
find $1 -not -iwholename "*.svn*" -type f | xargs md5sum | awk '{print $2 "\t" $1}'
2014-02-12 19:04:08
User: dronamk
Functions: awk find md5sum xargs
0

recurse through all files, get the message hash, flip the output as filename, hash value

find . -mtime +30 -exec mv {} old/ \;
2014-02-09 23:05:41
User: minnmass
Functions: find mv
Tags: bash file
-2

Use find's built-in "exec" option to avoid having to do any weirdness with quoting.

find * -regextype posix-extended -regex '.*\.(ext_1|ext_2)' -exec cp {} copy_target_directory \;
for i in $(find . -mtime +30); do mv $i old/; done
2014-02-05 01:24:45
User: valferon
Functions: find mv
Tags: bash file
-3

Will move in that case every file in the current folder older than 30 days to the "old" folder

Replace "mv $i old/" by any command such as rm / echo to do something different.

find /path/to/somewhere -newermt "Jan 1"
2014-02-02 18:02:07
User: renich
Functions: find
Tags: find
3

This command uses -newerXY to show you the files that are modified since a specific date. I recommend looking for "-newerXY" on the manpage to get the specifics.

find . -name "*.URL" | while read file ; do cat "$file" | sed 's/InternetShortcut/Desktop Entry/' | sed '/^\(URL\|\[\)/!d' > "$file".desktop && echo "Type=Link" >> "$file".desktop ; done
find . -type f -regex ".*/core.[0-9][0-9][0-9][0-9]$"
2014-01-17 16:44:47
User: H3liUS
Functions: find
0

Will find and list all core files from the current directory on. You can pass | xargs rm -i to be prompted for the removal if you'd like to double check before removal.

find . -name '*.sh' -exec bash -n {} \;
2014-01-17 16:20:36
User: tsener
Functions: bash find
8

Check all bash scripts in current dir for syntax errors WITHOUT running them.

find . -name "pattern" -type f -exec du -ch {} + | tail -n1
echo $(($(find . -name "pattern" -type f -printf "+%s")))
2014-01-16 03:14:36
User: flatcap
Functions: echo find
3

Use find's internal stat to get the file size then let the shell add up the numbers.

find . -name "pattern" -type f -printf "%s\n" | awk '{total += $1} END {print total}'
2014-01-16 01:16:18
User: pdxdoughnut
Functions: awk find
2

Using find's internal stat to get the file size is about 50 times faster than using -exec stat.

find . -name "pattern" -exec stat -c%s {} \; | awk '{total += $1} END {print total}'
2014-01-15 11:07:09
User: Koobiac
Functions: awk find stat
1

Find files and calculate size with stat of result in shell

find ./ -name '*:*' -exec rename 's/:/_/g' {} +
for i in */; do echo $(find $i -type f -regextype posix-extended -regex ".*\.(mp3|ogg|wav|flac)" | wc -l) $i ; done
find . -type d -maxdepth 1 -print0 | xargs -0 -I{} sh -c 'find "{}" -type f | grep "ogg\|mp3\|wav\|flac$" | wc -l | tr -d "\n"; echo " {}"'
2013-12-22 13:40:29
User: dbrgn
Functions: echo find grep sh tr wc xargs
0

This lists the number of ogg/mp3/wav/flac files in each subdirectory of the current directory. The output can be sorted by piping it into "sort -n".

find . -iname "*.mp3" | mpg123 -Z --list -
find */*.c | xargs grep 'mcs'
find -regextype posix-egrep -regex '.*\.(css|js)$' | xargs -I{} sh -c "echo '{}' && yuicompressor '{}' | gzip -c > '{}.gz'"
find /path/to/dir -type f -exec cachedel '{}' \;
2013-12-12 18:22:54
User: michelsberg
Functions: find
-1

This is just another example of what the nocache package is useful for, which I described in http://www.commandlinefu.com/commands/view/12357/ and that provides the commands

nocache <command to run with page cache disabled>

cachedel <single file to remove from page cache>

cachstats <single file> # to get the current cache state

Often, we do not want to disable caching, because several file reads are involved in a command and operations would be slowed down a lot, due to massive disk seeks. But after our operations, the file sits in the cache needlessly, if we know we're very likely never touching it again.

cachedel helps to reduce cache pollution, i.e. frequently required files relevant for desktop interaction (libs/configs/etc.) would be removed from RAM.

So we can run cachedel after each data intensive job. Today I run commands like these:

<compile job> && find . -type f -exec cachedel '{}' \; &> /dev/null # no need to keep all source code and tmp files in memory

sudo apt-get dist-upgrade && find /var/cache/apt/archives/ -type f -exec cachedel '{}' \; # Debian/*buntu system upgrade

dropbox status | grep -Fi idle && find ~/Dropbox -type f -exec cachedel '{}' \; &> /dev/null # if Dropbox is idle, remove sync'ed files from cache

https://github.com/Feh/nocache

http://packages.debian.org/search?keywords=nocache

http://packages.ubuntu.com/search?keywords=nocache

http://askubuntu.com/questions/122857

find /path/to/directory -not \( -name .snapshot -prune \) -type f -mtime +365
2013-12-11 14:51:53
User: cuberri
Functions: find
1

Useful when you want to cron a daily deletion task in order to keep files not older than one year. The command excludes .snapshot directory to prevent backup deletion.

One can append -delete to this command to delete the files :

find /path/to/directory -not \( -name .snapshot -prune \) -type f -mtime +365 -delete
find <PATH> -maxdepth 1 -type f -name "server.log*" -exec tar czPf '{}'.tar.gz --transform='s|.*/||' '{}' --remove-files \;