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 - 354 results
grep -ZlRr -e BAD_SCRIPT_LINE * |xargs -0 sed -i 's/BAD_SCRIPT_LINE//g'
2010-08-30 22:12:57
User: homoludens
Functions: grep sed xargs
0

recursive find and replace. important stuff are grep -Z and zargs -0 which add zero byte after file name so sed can work even with file names with spaces.

find . -type f -exec fgrep -l $'\r' "{}" \;
2010-08-20 23:26:56
User: putnamhill
Functions: fgrep find
Tags: find fgrep
1

Looking for carriage returns would also identify files with legacy mac line endings. To fix both types:

perl -i -pe 's/\r\n?/\n/g' $(find . -type f -exec fgrep -l $'\r' "{}" \;)
find . -type f -iname '*.flac' | while read FILE; do FILENAME="${FILE%.*}"; flac -cd "$FILE" | lame -b 192 - "${FILENAME}.mp3"; done
2010-08-15 19:02:19
User: paulochf
Functions: find read
7

find . -type f -iname '*.flac' # searches from the current folder recursively for .flac audio files

| # the output (a .flac audio files with relative path from ./ ) is piped to

while read FILE; do FILENAME="${FILE%.*}"; flac -cd "$FILE" | lame -b 192 - "${FILENAME}.mp3"; done

# for each line on the list:

# FILE gets the file with .flac extension and relative path

# FILENAME gets FILE without the .flac extension

# run flac for that FILE with output piped to lame conversion to mp3 using 192Kb bitrate

find . -type d -not \( -name .svn -prune \) -exec svn propset svn:ignore '*' {} \;
2010-08-15 03:45:57
User: tristan_ph
Functions: find
0

If you would like to ignore a directory including its subdirectory. For example, a tmp/ directory

ls | perl -lne '++$x{lc $1} if /[.](.+)$/ }{ print for keys %x'
2010-08-13 20:05:15
User: recursiverse
Functions: ls perl
-3

All with only one pipe. Should be much faster as well (sort is slow). Use find instead of ls for recursion or reliability.

Edit: case insensitive

find /path/to/dir -type f -name '*.*' | sed 's@.*/.*\.@.@' | sort | uniq
2010-08-12 15:48:54
User: putnamhill
Functions: find sed sort
1

If your grep doesn't have an -o option, you can use sed instead.

find /path/to/dir -type f | grep -o '\.[^./]*$' | sort | uniq
find <dir> -name "<pattern>" | while read file; do echo -n .; output=$(<command>) || (echo ; echo $file:; echo "$output"; ); done
2010-08-10 11:45:31
User: Marco
Functions: echo find read
2

This is a command template for achiving the following:

* loop over files --> find -name "" | while read file; do ...; done

* output progress --> echo -n .

* execute some command on each file and save output for later usage --> output=$()

* if command failed, open subshell and echo newline --> || (echo;...;...;)

* echo output of command --> echo "$output"

find "$1" -iname "*$2*"
find . -iname \*${MYVAR}\* -print
2010-08-04 05:43:51
User: Buzzcp
Functions: find
0

You define your variable MYVAR with the desired search pattern:

MYVAR=

...which can then be searched with the find command.

This is useful if you in a script, where you want the arguments to be fed into the find command.

The provided search is case insensitive (-iname) and will find all files and directories with the pattern MYVAR (not exact matches). This may go without saying, but if you want exact matches remove the \* and if you want case sensitive, use the -name argument.

find -maxdepth 1 -mtime 0 -type f
2010-07-30 01:56:34
Functions: find
Tags: find today mtime
1

Then pipe to 'xargs ls' for a familiar listing, possibly using find's -print0 and xarg's -0 options.

find . -name '*.txt' -print0 | parallel -0 -j+0 lzma
2010-07-28 21:01:12
Functions: find
Tags: find grep lzma
1

This will deal nicely with filenames containing newlines and will run one lzma process per CPU core. It requires GNU Parallel http://www.youtube.com/watch?v=OpaiGYxkSuQ

echo ${fullpath##*/}
2010-07-21 22:35:29
User: quincymd
Functions: echo
9

Quick method of isolating filenames from a full path using expansion.

Much quicker than using "basename"

find . -name '*.txt' | grep -v '\.lzma$' | xargs -n 1 lzma -f -v -3
find -type l -xtype d
ls -l $(find ./ -type l | perl -ne 'chomp; if (-d) { print "$_\n" }')
2010-07-16 19:31:28
User: rwadkins
Functions: find ls perl
-1

This will list all symlinks that are directories under the current directory. This will help you distinguish them from regular files.

nmap -v -sP 192.168.0.0/16 10.0.0.0/8
2010-07-14 19:53:02
User: anapsix
6

nmap for windows and other platforms is available on developer's site: http://nmap.org/download.html

nmap is robust tool with many options and has various output modes - is the best (imho) tool out there..

from nmap 5.21 man page:

-oN/-oX/-oS/-oG : Output scan in normal, XML, s|

grep -R --include=*.cpp --include=*.h --exclude=*.inl.h "string" .
2010-07-14 16:32:28
User: sweinst
Functions: grep
Tags: find xargs grep
0

Gnu grep allows to restrict the search to files only matching a given pattern. It also allows to exclude files.

find . -name '*.?pp' -exec grep -H "string" {} \;
find . -name '*.?pp' | xargs grep -H "string"
2010-07-14 14:41:07
User: cout
Functions: find grep xargs
Tags: find xargs grep
2

I like this better than some of the alternatives using -exec, because if I want to change the string, it's right there at the end of the command line. That means less editing effort and more time to drink coffee.

find /usr/share/figlet -name *.?lf -exec basename {} \; | sed -e "s/\..lf$//" | xargs -I{} toilet -f {} {}
2010-07-13 20:12:54
Functions: basename find sed xargs
Tags: find
1

Lists a sample of all installed toilet fonts

find . -name .svn -exec rm \-rf {} \;
sudo arp-scan -I eth0 192.168.1.0/24
FOR /L %i IN (1,1,254) DO ping -n 1 10.254.254.%i | FIND /i "Reply">> c:\ipaddresses.txt
2010-06-29 21:02:21
Functions: ping
-9

documents all active ips on a subnet and saves to txt file.