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 xargs from sorted by
Terminal - Commands tagged xargs - 118 results
find . -name \*.svg -print0 | xargs -0 -n1 -P4 -I{} bash -c 'X={}; convert "$X" "${X%.svg}.png"'
2014-04-11 14:30:30
User: flatcap
Functions: bash find xargs
2

Convert some SVG files into PNG using ImageMagick's convert command.

Run the conversions in parallel to save time.

This is safer than robinro's forkbomb approach :-)

xargs runs four processes at a time -P4

gsettings get org.gnome.desktop.background picture-uri | xargs nautilus
ls -1 | xargs ruby -e'puts ARGV.shuffle'
for i in */; do echo $(find $i -type f -regextype posix-extended -regex ".*\.(mp3|ogg|wav|flac)" | wc -l) $i ; done
find . -type d -maxdepth 1 -print0 | xargs -0 -I{} sh -c 'find "{}" -type f | grep "ogg\|mp3\|wav\|flac$" | wc -l | tr -d "\n"; echo " {}"'
2013-12-22 13:40:29
User: dbrgn
Functions: echo find grep sh tr wc xargs
0

This lists the number of ogg/mp3/wav/flac files in each subdirectory of the current directory. The output can be sorted by piping it into "sort -n".

find . -name '*pdf*' -print0 | xargs -0 ls -lt | head -20
2013-10-03 21:58:51
User: fuats
Functions: find head ls xargs
1

Sorts by latest modified files by looking to current directory and all subdirectories

wget -q -O- http://example-podcast-feed.com/rss | grep -o "<enclosure[ -~][^>]*" | grep -o "http://[ -~][^\"]*" | xargs wget -c
2013-09-24 12:38:08
User: talha131
Functions: grep wget xargs
0

This script can be used to download enclosed files from a RSS feed. For example, it can be used to download mp3 files from a podcasts RSS feed.

pbpaste | xargs wget
2013-08-11 23:12:10
User: loopkid
Functions: xargs
0

On Linux substitute pbpaste with `xsel --clipboard --output` or `xclip -selection clipboard -o` (untested)

curl -s $1 | grep -o -i '<a href="//images.4chan.org/[^>]*>' | sed -r 's%.*"//([^"]*)".*%\1%' | xargs wget
2013-07-22 10:33:55
User: bugmenot
Functions: grep xargs
1

first grep all href images then sed the url part then wget

find ./ -type f -print0 | xargs -0 md5sum
ssh user@remotehost "find basedir -type d" | xargs -I {} -t mkdir -p {}
2013-07-17 07:14:32
User: neomefistox
Functions: mkdir ssh xargs
0

The directories are created in the local host with the same structure below of a remote base directory, including the 'basedir' in case that it does not exists.

You must replace user and remotehost (or IP address) with your proper values

ssh will ask for the password of the user in remotehost, unless you had included properly your hostname in the remote .ssh/known_hosts file.

find . -empty -type d -print0 | xargs -0 rmdir -p
2013-07-01 02:44:57
User: rafar
Functions: find rmdir xargs
0

It starts in the current working directory.

It removes the empty directory and its ancestors (unless the ancestor contains other elements than the empty directory itself).

It will print a failure message for every directory that isn't empty.

This command handles correctly directory names containing single or double quotes, spaces or newlines.

If you do not want only to remove all the ancestors, just use:

find . -empty -type d -print0 | xargs -0 rmdir
watch 'ls -tr1 | tail -n1 | xargs tail'
2013-05-09 11:37:59
User: batandwa
Functions: tail watch xargs
Tags: tail ls xargs watch
-5

Watches for file modifications in the current directory and tails the file.

ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory
find /var/www/ -type f -print0 | xargs -0 chmod 644
find /var/www/ -type f -print0 | xargs -0 chmod 644
2013-03-28 11:10:30
User: FiloSottile
Functions: chmod find xargs
Tags: find xargs chmod
-1

xargs is a more elegant approach to executing a command on find results then -exec as -exec is meant as a filtering flag.

find -type f | xargs ls -1tr
pwd|grep -o '/'|perl -ne '$x.="./.";print`readlink -f $x`'|xargs -tn1 chmod 755
2013-03-14 12:03:44
Functions: chmod grep perl pwd xargs
0

`pwd` returns the current path

`grep -o` prints each slash on new line

perl generates the paths sequence: './.', './../.', ...

`readlink` canonicalizes paths (it makes the things more transparent)

`xargs -tn1` applies chmod for each of them. Each command applied is getting printed to STDERR.

svn revert .
cat item_list | xargs -n1 -P<n> process_item
for file in *; do convert $file -resize 800x600 resized-$file; done
2013-02-17 21:37:14
User: sonic
Functions: file
Tags: xargs convert
0

To ignore aspect ratio, run:

for file in *; do convert $file -resize 800x600! resized-$file; done

and all images will be exactly 800x600.

Use your shell of choice.. This was done in BASH.

for file in `svn st | awk '{print $2}'`; do svn revert $file; done
lsmod | tail -n +2 | cut -d' ' -f1 | xargs modinfo | egrep '^file|^desc|^dep' | sed -e'/^dep/s/$/\n/g'
find . -type f -exec stat -f '%m %N' {} \; | sort -n
find /path/to/dir -type f -print0 | xargs -0 grep -l "foo"