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 tagged find from sorted by
Terminal - Commands tagged find - 359 results
find . -type f -exec awk '/linux/ { printf "%s %s: %s\n",FILENAME,NR,$0; }' {} \;
find `pwd` -name '.*' -prune -o \( -name *.h -o -name *.cpp \) -print | cscope -bi-
2011-11-02 08:43:40
User: kev
Functions: find
Tags: find cscope
-2

cd /

find `pwd` -name '.*' -prune -o \( -name *.h -o -name *.cpp \) -print | cscope -bi-

export CSCOPE_DB=/cscope.out

vim +'set cst'

find . ! -name "$(printf '*[\001-\037\177]*')"
grep -Ilr "<?php" .
for i in /usr/bin/* ;do touch ${i##*/}; done
2011-10-20 12:38:45
User: _john
Functions: touch
Tags: bash find xargs zsh
0

You could avoid xargs and sed in this case (shorter command and less forking): At least bash and zsh have some mighty string modifiers.

I would also suggest using find with exec option to get more flexibility. You may leave out or include "special" file for example.

alias busy='rnd_file=$(find /usr/include -type f -size +5k | sort -R | head -n 1) && vim +$((RANDOM%$(wc -l $rnd_file | cut -f1 -d" "))) $rnd_file'
2011-10-16 00:05:59
User: frntn
Functions: alias cut find head sort vim wc
0

Enhancement for the 'busy' command originally posted by busybee : less chars, no escape issue, and most important it exclude small files ( opening a 5 lines file isn't that persuasive I think ;) )

This makes an alias for a command named 'busy'. The 'busy' command opens a random file in /usr/include to a random line with vim.

#!/bin/bash find | grep -P -v "(class)|(zip)|(png)|(gz)|(gif)|(jpeg)|(jpg)" | xargs -I @ grep -H $1 @
2011-10-14 13:28:49
User: kirdie
Functions: find grep xargs
Tags: find grep
-1

calls grep on all non-binary files returned by find on its current working directory

find . -type f -name '*.jpg' -exec convert -quality 75 {} {} \;
2011-09-23 13:51:18
User: bobby_tables
Functions: find
0

Requires ImageMagick to be installed.

This command was stolen from @climagic on Twitter.

Probably a duplicate of command below, but this command uses slightly higher quality.

http://www.commandlinefu.com/commands/view/707/compress-images-using-convert-imagemagick-in-a-bulk

find /path/folder -type f -name "*.*" -print -exec rm -v {} + | wc -l;
2011-09-19 14:53:37
User: Koobiac
Functions: find rm wc
0

It does not work without the verbose mode (-v is important)

ls -trF | grep -v \/ | tail -n 1
2011-09-14 20:05:37
User: mrpollo
Functions: grep ls tail
Tags: find stat mtime
-1

Sort by time and Reverse to get Ascending order, then display a marker next to the a file, negate directory and select only 1 result

sudo find / -type f | perl -MFile::Basename -ne '$counts{dirname($_)}++; END { foreach $d (sort keys %counts) {printf("%d\t%s\n",$counts{$d},$d);} }'|sort -rn | tee /tmp/sortedfilecount.out | head
2011-09-14 19:41:19
User: tamouse
Functions: find perl sort sudo tee
0

Find which directories on your system contain a lot of files.

Edit: much shorter and betterer with -n switch.

fn=$(find . -type f -printf "%T@\t%p\n"|sort -n|tail -1|cut -f2); echo $(date -r "$fn") "$fn"
find . -printf "%T@ %p\n" | sed -e 1d | while read ts fn; do ts=${ts%.*}; if [ $ts -ge ${gts:-0} ]; then gts=$ts; echo `date -d @$gts` $fn; fi; done
ls -l /etc/**/*killall
2011-08-30 05:57:49
User: xeor
Functions: ls
9

This command will give you the same list of files as "find /etc/ -name '*killall' | xargs ls -l".

In a simpler format just do 'ls /etc/**/file'.

It uses shell globbing, so it will also work with other commands, like "cp /etc/**/sshd sshd_backup".

find . -iname '*.conf' | xargs grep "searh string" -sl
find /var/spool/mqueue -type f -mtime +7 | perl -lne unlink
2011-08-19 15:22:02
User: mengesb
Functions: find perl
0

Find all files in /var/spool/mqueue older than 7 days, pass to perl to efficiently delete them (faster than xargs or -exec when you've got millions or hundreds of thousands to delete). Naturally the type, directory, and file age vars can be adjusted to meet your specific needs.

allVideos() { find ./ -type f -print0 | xargs -0 file -iNf - | grep ": video/" | cut -d: -f1; }
2011-08-19 11:58:59
User: totti
Functions: cut file find grep xargs
0

Videos are found using their MIME type. Thus no need to for an extension for the video file.

This is a efficent version of "jnash" cmd (4086). Thanks for jnash. This cmd will only show video files while his cmd show files having "video" anywhere in path.

find . -type f | sed -n 's/..*\.//p' | sort -f | uniq -ic
2011-08-19 00:19:43
User: tyler_l
Functions: find sed sort uniq
0

Change "sort -f" to "sort" and "uniq -ic" to "uniq -c" to make it case sensitive.

lsr() { find "${@:-.}" -print0 |sort -z |xargs -0 ls $LS_OPTIONS -dla; }
2011-08-15 03:10:58
User: h3xx
Functions: find ls sort xargs
2

Tells you everything you could ever want to know about all files and subdirectories. Great for package creators. Totally secure too.

On my Slackware box, this gets set upon login:

LS_OPTIONS='-F -b -T 0 --color=auto'

and

alias ls='/bin/ls $LS_OPTIONS'

which works great.

find . -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort
find . -type d -name 'CVS' -exec rm -r {} \;
find . -type d -name 'CVS' | xargs rm -r
find * -type d -maxdepth 0
2011-08-07 06:04:50
User: edogawaconan
Functions: find
Tags: find ls grep sh
1

Alternatively,

ls -F | grep /\$

but will break on directories containing newlines. Or the safe, POSIX sh way (but will miss dotfiles):

for i in *; do test -d "./$i" && printf "%s\n" "$i"; done
find . -iname "*.flac" | cpio -pdm /Volumes/Music/FLAC
find . -type f -exec chmod -x {} \;