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 - 591 results
for each in /usr/bin/*; do echo $each | sed 's/\/usr\/bin\///' | xargs touch; done
2011-10-13 19:14:34
User: evil
Functions: echo sed xargs
1

This will make a false directory with the same file names as whatever directory you choose. This is wise to use when testing scripts that alter contents, filenames, or move files. I wrote this after an OOPS I made when renaming a directory of JPGs, PNGs, PSDs that were mixed. I recommend this as I lost over 2000 vacation pictures and some graphics I designed for software and web sites. :(

NOTE: This only creates name copies, that data itself is not copied.

svn status|grep -iR '^!'|sed 's/!/ /g'|xargs -i svn rm '{}'
2011-10-06 08:11:25
User: erdeszt
Functions: grep rm sed xargs
Tags: svn sed xargs grep
0

Helps if you accidentally deleted files from an svn repo with plain rm and you would like to mark them for svn to delete too.

LC_ALL=C diff -q dir1 dir2 | grep differ | awk '{ print $2, $4 }' | xargs -n 2 gvim --nofork -d
2011-09-06 11:50:33
User: brock
Functions: awk diff grep xargs
0

LC_ALL=C is here to always grep on "differ" whatever your language env.

xargs -n 2 to run gvim -d with 2 arguments

gvim --nofork to use only one instance of gvim

find . -type f | xargs grep -n "Old Text" | tee filesChanged.txt | sed 's/:.*$//' | xargs sed -i 's/Old Text/New Text/g
find -iname \*.pdf -print0 | xargs -0 pdfgrep -i "my search text"
svn st -q | cut -c 2- | tr -d ' ' | xargs tar -czvf ../backup.tgz
2011-08-31 10:30:29
User: titus
Functions: cut tar tr xargs
1

This works more reliable for me ("cut -c 8-" had one more space, so it did not work)

find . -type f | xargs grep -l "string"
2011-08-30 13:22:42
User: Neo23x0
Functions: find grep xargs
-5

Finds a string in files recursively below the current directory on systems without the "egrep" and its "-r" functionality.

grep -lr -e '<oldword>' * | xargs sed -i 's/<oldword>/<newword>/g'
2011-08-27 02:42:51
User: o0110o
Functions: grep sed xargs
5

- grep for the word in a files, use recursion (to find files in sub directories), and list only file matches

-| xargs passes the results from the grep command to sed

-sed -i uses a regular expression (regex) to evaluate the change: s (search) / search word / target word / g (global replace)

find . -iname '*.conf' | xargs grep "searh string" -sl
allVideos() { find ./ -type f -print0 | xargs -0 file -iNf - | grep ": video/" | cut -d: -f1; }
2011-08-19 11:58:59
User: totti
Functions: cut file find grep xargs
0

Videos are found using their MIME type. Thus no need to for an extension for the video file.

This is a efficent version of "jnash" cmd (4086). Thanks for jnash. This cmd will only show video files while his cmd show files having "video" anywhere in path.

find . -name "directory_name" -type d -print0 | xargs -0 -i rm -rf {}
sort -R /usr/share/dict/british | grep -v -m4 ^\{1,10\}$ | tr [:upper:] [:lower:] | tr "\n" " " | tr -d "'s" | xargs -0 echo
2011-08-16 10:11:21
User: takac
Functions: grep sort tr xargs
Tags: tr xkcd
-1

Doesn't use shuf, its much faster with "shuf -n4" instead of sort -R

lsr() { find "${@:-.}" -print0 |sort -z |xargs -0 ls $LS_OPTIONS -dla; }
2011-08-15 03:10:58
User: h3xx
Functions: find ls sort xargs
2

Tells you everything you could ever want to know about all files and subdirectories. Great for package creators. Totally secure too.

On my Slackware box, this gets set upon login:

LS_OPTIONS='-F -b -T 0 --color=auto'

and

alias ls='/bin/ls $LS_OPTIONS'

which works great.

svn st | grep '^?' | sed -e 's/\?[[:space:]]*//' | tr '\n' '\0' | xargs -0 svn add
find . -type d -name 'CVS' | xargs rm -r
pgrep -u username php5-fcgi | xargs kill -9
vim -p `grep -r PATTERN TARGET_DIR | cut -f1 -d: | sort | uniq | xargs echo -n`
find . -type d|xargs tar rf ~/dirstructure.tar --no-recursion
2011-08-03 16:21:10
User: gdevarajan
Functions: find tar xargs
1

The original suggestion did not work for me, when operating on folders located on an external mount (ie other than the root device) in Ubuntu. A variation using xargs does the trick.

ipcs -ma | awk '/^m / { if ($9 == 0) { print $2 }}' | xargs -n 1 ipcrm -m
2011-07-26 15:53:59
User: keymon
Functions: awk ipcrm ipcs xargs
0

It can work for message queue, semaphore set or shared memory just changing the parameter.

locate -i yourfilename | sed 's/ /\\ /g' | xargs ls -lah | less
xargs -P 3 -n 1 <COMMAND> < <FILE_LIST>
2011-07-25 22:53:32
User: h3xx
Functions: xargs
0

For instance:

find . -type f -name '*.wav' -print0 |xargs -0 -P 3 -n 1 flac -V8

will encode all .wav files into FLAC in parallel.

Explanation of xargs flags:

-P [max-procs]: Max number of invocations to run at once. Set to 0 to run all at once [potentially dangerous re: excessive RAM usage].

-n [max-args]: Max number of arguments from the list to send to each invocation.

-0: Stdin is a null-terminated list.

I use xargs to build parallel-processing frameworks into my scripts like the one here: http://pastebin.com/1GvcifYa

find -L /proc/`ps aux | grep [f]lash | awk '{print $2}'`/fd/ | xargs file -L | grep Video | awk '{sub(/:/, ""); print $1}' | xargs vlc
2011-07-24 17:53:46
User: tuxcanfly
Functions: awk file find grep xargs
0

If you used to do `vlc /tmp/Flash*`, but no longer can't, this is for you.

gpg --list-sigs | sed -rn '/User ID not found/s/^sig.+([a-FA-F0-9]{8}).*/\1/p' | xargs -i_ gpg --keyserver-options no-auto-key-retrieve --recv-keys _
2011-07-22 16:31:25
User: lingo
Functions: gpg sed xargs
Tags: GPG sed fetch
0

For instance, if people have signed your key, this will fetch the signers' keys.

curl -s --compressed http://funnyjunk.com | awk -F'"' '/ '"'"'mainpagetop24h'"'"'/ { print "http://funnyjunk.com"$4 }' | xargs curl -s | grep -o 'ht.*m/pictures/.*\.jpg\|ht.*m/gifs/.*\.gif' | grep "_......_" | uniq | xargs wget
2011-07-21 15:57:21
User: laniner
Functions: awk uniq xargs
0

If your version of curl does not support the --compressed option, use

curl -s http://funnyjunk.com | gunzip

instead of

curl -s --compressed http://funnyjunk.com
find / -name "*.xls" -print0 | xargs -0 rename .xls .ods {}
2011-07-18 12:48:49
User: jagjit
Functions: find rename xargs
0

This command can be used to rename all the files with extension .xls( in this case) to .ods files. It can be used for other files with certain extension.