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 -name '*oldname*' -print0 | xargs -0 rename 's/oldname/newname/'
2009-07-27 00:44:06
Functions: find rename xargs
0

This is better than doing a "for `find ...`; do ...; done", if any of the returned filenames have a space in them, it gets mangled. This should be able to handle any files.

Of course, this only works if you have rename installed on your system, so it's not a very portable command.

ls -drt /var/log/* | tail -n5 | xargs sudo tail -n0 -f
2009-07-22 14:44:41
User: kanaka
Functions: ls sudo tail xargs
Tags: bash tail log watch
5

This command finds the 5 (-n5) most frequently updated logs in /var/log, and then does a multifile tail follow of those log files.

Alternately, you can do this to follow a specific list of log files:

sudo tail -n0 -f /var/log/{messages,secure,cron,cups/error_log}

find / | xargs ls -l | tr -s ' ' | cut -d ' ' -f 1,3,4,9
ps -u $USER |grep $1 | awk '{ print $1}'| xargs kill
2009-07-20 10:06:32
User: buffer
Functions: awk grep ps xargs
-4

Well this can come handy , when you don't feel like playing with pid rather if you know

the process name say "firefox",it would kill it.The script given below would kill the process with its name given as first parameter , though not robust enough to notify that process doesn't exist , well if you know what you are doing that's wouldn't be a problem.:)

----

killhim.sh

----

#!/bin/bash

ps -u $USER |grep $1 | awk '{ print $1}'| xargs kill

----

git ls-files -z --deleted | xargs -0 git rm
2009-07-14 08:29:38
User: blindgaenger
Functions: xargs
Tags: git
5

I've used technicalpickles command a lot, but this one handles whitespaces in filenames. I'm sure you want to create an alias for it :)

find . -name '*.html' -print0| xargs -0 -L1 cat |sed "s/[\"\<\>' \t\(\);]/\n/g" |grep "http://" |sort -u
2009-07-14 07:00:15
User: jamespitt
Functions: cat find grep sed sort xargs
4

Just a handy way to get all the unique links from inside all the html files inside a directory. Can be handy on scripts etc.

/originalInstall/gem list | tr -d '(),' | xargs -L 1 sudo ./gemInst.sh
2009-07-09 21:46:06
User: snakerdlk
Functions: sudo tr xargs
Tags: bash Linux
0

gemInst.sh:

#!/bin/bash

for i in $@; do

if [ "$1" != "$i" ]

then

echo /newInstall/gem install $1 -v=\"$i\"

/newInstall/gem install $1 -v="$i"

if [ "$?" != "0" ]

then

echo -e "\n\nGEM INSTALL ERROR: $1\n\n"

echo "$1" > gemInst.err

fi

fi

done

ls *tgz | xargs -n1 tar xzf
sudo port installed | grep -v 'active\|The' | xargs sudo port uninstall
find . -not \( -name .svn -prune \) -type f -print0 | xargs --null grep <searchTerm>
2009-07-08 20:08:05
User: qazwart
Functions: find grep xargs
Tags: find xargs grep
8

By putting the "-not \( -name .svn -prune \)" in the very front of the "find" command, you eliminate the .svn directories in your find command itself. No need to grep them out.

You can even create an alias for this command:

alias svn_find="find . -not \( -name .svn -prune \)"

Now you can do things like

svn_find -mtime -3
unzip -lt foo.zip | grep testing | awk '{print $2}' | xargs rm -r
tar -tf <file.tar.gz> | xargs rm -r
S=`pidof skype`;grep heap /proc/$S/maps|cut -f1 -d' '|awk -F- '{print "0x" $1 " 0x" $2}'|xargs echo "du me t ">l;gdb -batch -p $S -x l>/dev/null 2>&1;strings t|grep \(smirk|head -n1
2009-06-26 20:03:17
User: alvieboy
Functions: awk cut echo grep head xargs
0

Skype has an internal regex which depicts the emoticons it supports. However you cannot simply search the binary file for it. This small 181 character line will do just that, provided skype is running. And of course, only works in linux.

svn status | grep '!' | sed 's/!/ /' | xargs svn del --force
dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d' | xargs sudo apt-get -y purge
2009-06-19 10:11:00
User: plasticdoc
Functions: sed sudo xargs
6

will purge:

only installed apps: /^ii/!d

avoiding current kernel stuff: /'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d

using app names: s/^[^ ]* [^ ]* \([^ ]*\).*/\1/

avoiding stuff without a version number: /[0-9]/!d

cut -d: -f1 /etc/passwd | grep -vE "#" | xargs -i{} crontab -u {} -l
2009-06-18 16:49:52
User: hoberion
Functions: crontab cut grep xargs
1

additionally use "find /etc/cron*" for cronscripts

egrep -r '(render_message|multipart).*('`find app/views -name '*.erb' | grep mailer | sed -e 's/\..*//' -e 's/.*\///' | uniq | xargs | sed 's/ /|/g'`')' app/models
mkdir phrack66; (cd phrack66; for n in {1..17} ; do echo "http://www.phrack.org/issues.html?issue=66&id=$n&mode=txt" ; done | xargs wget)
2009-06-11 21:42:42
Functions: cd echo mkdir xargs
2

Nice reading in the morning on the way to work, but sadly the .tar.gz for the whole issue 66 is not on phrack's website yet. So use wget to download.

find . -iname '*.jar' | xargs du -ks | cut -f1 | xargs echo | sed "s/ /+/g" | bc
find . -type f -print0|xargs -0 md5sum|sort|perl -ne 'chomp;$ph=$h;($h,$f)=split(/\s+/,$_,2);print "$f"."\x00" if ($h eq $ph)'|xargs -0 rm -v --
2009-06-07 03:14:06
Functions: find perl rm xargs
19

This one-liner will the *delete* without any further confirmation all 100% duplicates but one based on their md5 hash in the current directory tree (i.e including files in its subdirectories).

Good for cleaning up collections of mp3 files or pictures of your dog|cat|kids|wife being present in gazillion incarnations on hd.

md5sum can be substituted with sha1sum without problems.

The actual filename is not taken into account-just the hash is used.

Whatever sort thinks is the first filename is kept.

It is assumed that the filename does not contain 0x00.

As per the good suggestion in the first comment, this one does a hard link instead:

find . -xdev -type f -print0 | xargs -0 md5sum | sort | perl -ne 'chomp; $ph=$h; ($h,$f)=split(/\s+/,$_,2); if ($h ne $ph) { $k = $f; } else { unlink($f); link($k, $f); }'
find ./* -ctime -1 | xargs ls -ltr --color
2009-06-05 13:53:26
User: gnuyoga
Functions: find ls xargs
3

added alias in ~/.bashrc

alias lf='find ./* -ctime -1 | xargs ls -ltr --color'

find /var/log -type f -exec file {} \; | grep 'text' | cut -d' ' -f1 | sed -e's/:$//g' | grep -v '[0-9]$' | xargs tail -f
2009-06-03 09:47:08
User: mohan43u
Functions: cut file find grep sed tail xargs
Tags: tail
5

Works in Ubuntu, I hope it will work on all Linux machines. For Unixes, tail should be capable of handling more than one file with '-f' option.

This command line simply take log files which are text files, and not ending with a number, and it will continuously monitor those files.

Putting one alias in .profile will be more useful.

find /var/logs -name * | xargs tar -jcpf logs_`date +%Y-%m-%e`.tar.bz2
git grep -l "your grep string" | xargs gedit
grep -PL "\t" -r . | grep -v ".svn" | xargs sed -i 's/\t/ /g'
2009-05-28 08:52:14
User: root
Functions: grep sed xargs
3

Note that this assumes the application is an SVN checkout and so we have to throw away all the .svn files before making the substitution.