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 - 599 results
find . -type f -size -80k -print0|xargs -0 rm
2012-09-19 12:15:32
User: DeepThought
Functions: find xargs
0

Probably neither faster nor better than -delete in find. It's just that I generally dislike teaching find builtin actions.

lpstat -p | cut -d' ' -f2 | xargs -I{} lpadmin -x {}
2012-09-18 02:11:53
User: bmeehan
Functions: cut lpadmin lpstat xargs
0

This is the closest you can get to "reset printing system" from the command line. Giving credit back to J D McIninch from an apple forum back in 2009.

git status --porcelain | awk '{print $2}' | xargs git add
2012-09-05 18:07:26
User: brandizzi
Functions: awk xargs
0

Uses the --porcelain option, which is garanteed to be stable among git versions and configurations - also, is way easier to parse.

lsmod | tail -n +2 | cut -d' ' -f1 | xargs modinfo | egrep '^file|^desc|^dep' | sed -e'/^dep/s/$/\n/g'
sudo find . -name "*.csv" | xargs /bin/rm
2012-08-29 11:38:37
User: defc0n1
Functions: find sudo xargs
0

In case you ever got to many arguments using rm to delete multiple files matching a pattern this will help you

find . -type f | xargs -I% bash -c 'echo -e "\033[31m%\033[0m" && [[ ! `file %` =~ .*binary.* ]] && head "%"'
touch -t 201208211200 first ; touch -t 201208220100 last ; find /path/to/files/ -newer first ! -newer last | xargs -ifile mv -fv file /path/to/destination/ ; rm first; rm last;
2012-08-22 09:51:40
User: ktopaz
Functions: file find last mv rm touch xargs
0

touch -t 201208211200 first ; touch -t 201208220100 last ;

creates 2 files: first & last, with timestamps that the find command should look between:

201208211200 = 2012-08-21 12:00

201208220100 = 2012-08-22 01:00

then we run find command with "-newer" switch, that finds by comparing timestamp against a reference file:

find /path/to/files/ -newer first ! -newer last

meaning: find any files in /path/to/files that are newer than file "first" and not newer than file "last"

pipe the output of this find command through xargs to a move command:

| xargs -ifile mv -fv file /path/to/destination/

and finally, remove the reference files we created for this operation:

rm first; rm last;

curl -s http://boards.4chan.org/---/res/nnnnnn | grep -o -i 'File: <a href="//images.4chan.org\/[a-z]*\/src\/[0-9]*\.[a-z]\{3\}' | sed -r 's/File: <a href="\/\///' |xargs wget
find . -type f -exec file '{}' + | grep shell | awk -F':' '{print $1}' | xargs chmod u+x
2012-08-09 18:53:08
User: llebegue
Functions: awk chmod file find grep xargs
0

Allows to change 'shell' compatible files execution bit even if their name is not *.sh

ls | grep -Ze ".*rar" | xargs -d '\n' -I {} mv {} backup-folder
2012-08-06 09:07:03
User: crisboot
Functions: grep ls mv xargs
0

In the example suppose we want to move all *.rar files in the current folder to a backupfolder

find . -name '*' -type f -print0 | xargs -0 grep -n pattern1 | grep pattern2
2012-07-29 00:40:06
User: bugmenot
Functions: find grep xargs
1

The option -print0 for find and -0 for grep help prevent issue with weird characters or spaces in filenames. Furthermore with xargs there is no limited number of arguments that find can throw.

find . -type f \( -name '*.c' -o -name '*.cpp' -o -name '*.cc' -o -name '*.cxx' \) | xargs grep "#include.*\.c.*" 2>&1 | tee source_inside_source_list.txt
find . -name "*.php" -print | xargs sed -i 's/foo/bar/g'
ps -fu $USER | awk {'print $2'} | xargs kill [-9]
ps auxww | grep application | grep processtobekilled | gawk '{print $2}' | grep -v grep | xargs kill -9
2012-07-03 20:37:56
User: j0sh10
Functions: gawk grep kill ps xargs
0

You can also use gawk:

ps auxww | gawk '/application/' | gawk '/processtobekilled/' | gawk '{print $2}' | grep -v grep | xargs kill -9

find . -type f -print0 | xargs -0 du -h | sort -hr | head -10
find . -type f -print0 | xargs -0 du -h | sort -hr | head
git submodule status | awk '{print $2}' | xargs -P5 -n1 git submodule update --init
find /path/to/dir -type f -print0 | xargs -0 grep -l "foo"
grep -r -Z -l "<text>" . | xargs -0 echo rm
2012-06-14 08:09:46
User: limonte
Functions: echo grep xargs
Tags: grep
4

-r recursively

-Z zero byte after each file name instead of the usual newline

-l only filenames

find . -type f -print0 | xargs -0 chmod a-x
2012-06-11 07:28:30
User: jlaunay
Functions: chmod find xargs
-2

Using xargs is usually much quicker as it does not have to execute chmod for every file

ls -ltr |grep 'May 12'|awk '{print $9;}'|xargs rm -v
git ls-files | xargs -n1 git blame --line-porcelain | sed -n 's/^author //p' | sort -f | uniq -ic | sort -nr
2012-06-02 22:04:36
User: hugopeixoto
Functions: sed sort uniq xargs
Tags: statistics git
1

Uses line-porcelain in git blame, which makes it easier to parse the output.

for foo in <list of directories>; do while find $foo -type d -empty 2>/dev/null| grep -q "."; do find $foo -type d -empty -print0 | xargs -0 rmdir; done; done
2012-05-23 08:09:16
Functions: find grep xargs
0

This will check if there are any empty directories, or newly emptied directories, in a list of directories. It will then delete all of those directories. It works with gnu find and OSX/BSD find.