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 xargs from sorted by
Terminal - Commands using xargs - 598 results
svn st | grep "^\?" | awk "{print \$2}" | xargs rm -rf
find . -name Root -print | xargs rm -f
grep -r "sampleString" . |uniq | cut -d: -f1 | xargs sed -i "/sampleString/d"
2009-02-09 20:21:30
User: sk1418
Functions: cut grep sed xargs
-2

checking files in current and sub directories, finding out the files containing "sampleString" and removing the containing lines from the file.

* Beware that The command will update the original file [no backup].

The command can be extended if play with 'find' command together,

e.g. it is possible to execute on certain type of files: *.xml, *.txt... (find -name "*.xml" | grep....)

if anybody knows a better solution on that, please drop a comment. thx.

accurev stat -M -fl | awk '{print "\"" $0 "\""}' | xargs accurev pop
find . \( -name '*.h' -o -name '*.cc' \) | xargs grep . | wc -l
2009-02-09 11:44:35
User: dgomes
Functions: find grep wc xargs
3

Counts number of lines of code in *.h and *.cc files

svn st | cut -c 8- | sed 's/^/\"/;s/$/\"/' | xargs tar -czvf ../backup.tgz
2009-02-09 11:24:31
User: chrisdrew
Functions: cut sed tar xargs
12

Create a tgz archive of all the files containing local changes relative to a subversion repository.

Add the '-q' option to only include files under version control:

svn st -q | cut -c 8- | sed 's/^/\"/;s/$/\"/' | xargs tar -czvf ../backup.tgz

Useful if you are not able to commit yet but want to create a quick backup of your work. Of course if you find yourself needing this it's probably a sign you should be using a branch, patches or distributed version control (git, mercurial, etc..)

echo -n m{1..5}.cluster.net | xargs -d' ' -n1 -P5 -I{} ssh {} 'uptime'
mount | grep : | tr -s ' ' -d 3 | xargs umount -v
find ~ -name '*.mp4' | xargs mplayer
grep -r -l xxxxx . | xargs perl -i -pe "s/xxxxx/yyyyy/g"
2009-02-06 08:18:50
User: hassylin
Functions: grep perl xargs
-1

This script first find all files which contains word xxxxx recursively. Then replace the word xxxxx to yyyyy of the files.

Use case:

- Web site domain change

- Function name change of the program

seq -f"ftp://ftp.vim.org/pub/vim/patches/7.1/7.1.%03g" 176 240 | xargs -I {} wget -c {};
2009-02-06 03:19:06
User: liupeng
Functions: seq wget xargs
3

Seq allows you to define printf like formating by specified with -f, %03g is actually tells seq I got three digits, fill the blank digits with 0, and the range is from 176 to 240.

find . -name "*.txt" | xargs perl -pi -e 's/old/new/g'
2009-02-06 00:28:03
User: neztach
Functions: find perl xargs
6

syntax follows regular command line expression.

example: let's say you have a directory (with subdirs) that has say 4000 .php files.

All of these files were made via script, but uh-oh, there was a typo!

if the typo is "let's go jome!" but you meant it to say "let's go home!"

find . -name "*.php" | xargs perl -pi -e "s/let\'s\ go\ jome\!/let\'s\ go\ home\!/g"

all better :)

multiline: find . -name "*.php" | xargs perl -p0777i -e 's/knownline1\nknownline2/replaced/m'

indescriminate line replace: find ./ -name '*.php' | xargs perl -pi -e 's/\".*$\"/\new\ line\ content/g'

locate searchstring | xargs ls -l
cd ~/.purple/logs/; egrep -ri "i can haz|pwn|l33t|w00|zomg" * | cut -d'/' -f 3 | sort | uniq | xargs -I {} echo "Note to self: ban user '{}'"
find . -type d -name '.svn' -print0 | xargs -0 rm -rdf
find . -name "*.py" | xargs wc -l
svn st | grep "^\?" | awk "{print \$2}" | xargs svn add $1
ps -ef | grep APP | awk '/grep/!{print$2}' | xargs -i kill {}
2009-02-05 16:10:05
User: hooobs
Functions: awk grep kill ps xargs
-2

Probably posted previously, I use this all the time to find and kill a process for "APP". Simply replace "APP" with the name of the process you're looking to kill.

ps -ef | grep [f]oo | awk '{print $2}' | xargs kill -9
2009-02-05 13:43:01
User: eredicatorx
Functions: awk grep kill ps xargs
1

Kill all processes with foo in them. Similar to pkill but more complete and also works when there is no pkill command.

Works on almost every Linux/Unix platform I have tried.

find . -type f -print0 | grep -vzZ '.svn' | xargs -0 grep --color -nEHi "SEARCHTERM"
2009-02-05 10:27:15
User: troelskn
Functions: find grep xargs
-3

Variant of find grep that ignores files with .svn in the name. Useful for searching through a local repository of source code.

svn status |grep '\?' |awk '{print $2}'| xargs svn add
2009-01-29 10:33:22
User: xsawyerx
Functions: xargs
13

checks which files are not under version control, fetches the names and runs them through "svn add". WARNING: doesn't work with white spaces.

find /path/to/dir -type f -print0 | xargs -0 rm
2009-01-26 11:30:47
User: root
Functions: find xargs
12

Using xargs is better than:

find /path/to/dir -type f -exec rm \-f {} \;

as the -exec switch uses a separate process for each remove. xargs splits the streamed files into more managable subsets so less processes are required.

alias cr='find . 2>/dev/null -regex '\''.*\.\(c\|cpp\|pc\|h\|hpp\|cc\)$'\'' | xargs grep --color=always -ni -C2'
2009-01-26 08:54:25
User: chrisdrew
Functions: alias grep xargs
0

Creates a command alias ('cr' in the above example) that searches the contents of files matching a set of file extensions (C & C++ source-code in the above example) recursively within the current directory. Search configured to be in colour, ignore-case, show line numbers and show 4 lines of context. Put in shell initialisation file of your choice. Trivially easy to use, e.g:

cr sha1_init