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 - 610 results
find / -xdev \( -perm -4000 \) -type f -print0 | xargs -0 ls -l
grep -rl string_to_find public_html/css/ | xargs -I '{}' vim +/string_to_find {} -c ":s/string_to_find/string_replaced"
2012-11-07 14:44:51
User: algol
Functions: grep vim xargs
-1

Open all files which have some string go directly to the first line where that string is and run command on it.

Other examples:

Run vim only once with multiple files (and just go to string in the first one):

grep -rl string_to_find public_html/css/ | xargs vim +/string_to_find

Run vim for each file, go to string in every one and run command (to delete line):

grep -rl string_to_find public_html/css/ | xargs -I '{}' vim +/string_to_find {} -c ":delete"
ls /var/log/sa/sa[0-9]*|xargs -I '{}' sar -u -f {}|awk '/^[0-9]/&&!/^12:00:01|RESTART|CPU/{print "%user: "$4" %system: "$6" %iowait: "$7" %nice: "$5" %idle: "$9}'|sort -nk10|head
ls /var/log/sa/sa[0-9]*|xargs -I '{}' sar -q -f {}| awk '/Average/'|awk '{runq+=$2;plist+=$3}END{print "average runq-sz:",runq/NR; print "average plist-sz: "plist/NR}'
diff ../source-dir.orig/ ../source-dir.post/ | grep "Only in" | sed -e 's/^.*\:.\(\<.*\>\)/\1/g' | xargs rm -r
2012-10-17 14:12:32
User: bigc00p
Functions: diff grep rm sed xargs
0

Good for when your working on building a clean source install for RPM packaging or what have you. After testing, run this command to compare the original extracted source to your working source directory and it will remove the differences that are created when running './configure' and 'make'.

find /var/cache/apt -not -mtime -7 | sudo xargs rm
ls /dev/disk* | xargs -n 1 -t sudo zdb -l | grep GPTE_
2012-10-06 20:19:45
User: grahamperrin
Functions: grep ls sudo xargs
1

Show the UUID-based alternate device names of ZEVO-related partitions on Darwin/OS X. Adapted from the lines by dbrady at http://zevo.getgreenbytes.com/forum/viewtopic.php?p=700#p700 and following the disk device naming scheme at http://zevo.getgreenbytes.com/wiki/pmwiki.php?n=Site.DiskDeviceNames

find . -type d -maxdepth 1 | xargs du -sh
find site/ -type d | xargs sudo chmod 755
find ./ -type f | xargs sudo chmod 644
find /var/cache/pacman/pkg -not -mtime -7 | sudo xargs rm
2012-09-20 12:36:44
User: brejktru
Functions: find sudo xargs
1

Sometimes my /var/cache/pacman/pkg directory gets quite big in size. If that happens I run this command to remove old package files. Packages that we're upgraded in last N days are kept in case you are forced to downgrade a specific package. The command is obviously Arch Linux related.

<cmd> | xargs -0 <cmd>
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