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 - 597 results
find * -type f -not -name ".*" | xargs wc -l
2010-05-21 21:03:31
User: Leechael
Functions: find wc xargs
1

We use `-not -name ".*"` for the reason we must omit hidden files (which unnecessary). We can only show up total lines like this:

find * -type f -not -name ".*" | xargs wc -l | tail -1
watch 'find -maxdepth 1 -mindepth 1 -type d |xargs du -csh'
2010-05-19 13:13:57
User: shadycraig
Functions: du watch xargs
0

This command shows the size of directories below here, refreshing every 2s.

It will also track directories created after running the command (that what the find bit does).

rm-but() { ls -Q | grep -v "$1" | xargs rm -r ; }
2010-05-13 09:28:56
User: sata
Functions: grep ls rm xargs
0
rm-but() { ls -Q | grep -v "$1" | xargs rm -r ; }

Add this to your .bashrc file.

Then whenever you need to remove all files/directories but one from present working directory. Run:

rm-but <important-file-or-directory>

Notes:

1. This doesn't affect the hidden files.

2. Argument is actually as string. And all files/directories having this string in there name are left untouched.

find / -type f -size +512000 | xargs ls -lh | awk '{ print $5 " " $6$7 ": " $9 }'
2010-05-12 17:21:12
User: johnss
Functions: awk find ls xargs
-1

This is an updated version that some one provided me via another "find" command to find files over a certain size. Keep in mind you may have to mess around with the print values depending on your system to get the correct output you want. This was tested on FC and Cent based servers. (thanks to berta for the update)

pgrep rouge-process | xargs sudo kill -9
2010-05-09 22:30:05
User: mheadd
Functions: kill sudo xargs
Tags: xargs pgrep
-3

Find and kill multiple instances of a process with one simple command.

find . \( -iname '*.[ch]' -o -iname '*.php' -o -iname '*.pl' \) | xargs wc -l | sort -n
2010-04-30 12:21:28
User: rbossy
Functions: find sort wc xargs
Tags: find count
0

find -exec is evil since it launches a process for each file. You get the total as a bonus.

Also, without -n sort will sort by lexical order (that is 9 after 10).

zenity --list --width 500 --height 500 --column 'radio' --column 'url' --print-column 2 $(curl -s http://www.di.fm/ | awk -F '"' '/href="http:.*\.pls.*96k/ {print $2}' | sort | awk -F '/|\.' '{print $(NF-1) " " $0}') | xargs mplayer
2010-04-28 23:45:35
User: polaco
Functions: awk sort xargs
15

This is a very simple and lightweight way to play DI.FM stations

For a more complete version of the command with proper strings in the menu, try: (couldnt fit in the command field above)

zenity --list --width 500 --height 500 --title 'DI.FM' --text 'Pick a Radio' --column 'radio' --column 'url' --print-column 2 $(curl -s http://www.di.fm/ | awk -F '"' '/href="http:.*\.pls.*96k/ {print $2}' | sort | awk -F '/|\.' '{print $(NF-1) " " $0}') | xargs mplayer

This command line parses the html returned from http://di.fm and display all radio stations in a nice graphical menu. After the radio is chosen, the url is passed to mplayer so the music can start

dependencies:

- x11 with gtk environment

- zenity: simple app for displaying gtk menus (sudo apt-get install zenity on ubuntu)

- mplayer: simple audio player (sudo apt-get install mplayer on ubuntu)

find . -maxdepth 1 -type f | xargs stat
2010-04-26 20:51:54
User: asolkar
Functions: find xargs
2

Possible simplification of egrep-awk-sort with find and -exec with xargs.

more xx.m3u |grep -v "^#" |xargs -i cp {} target
S='<iframe src=\"http:\/\/254.254.254.254\/bad\/index.php\" width=\"1\" height=\"1\" frameborder=\"0\"><\/iframe>' && R=''; find . -name "*.html" -exec grep -l "$S" {} \; | xargs sed -i -e "s/$S/$R/g"
2010-04-12 21:45:16
User: rexington
Functions: find grep sed xargs
-1

Removes the given string from all files under the given path - in this case the path given is "." This demonstrates the characters that must be escaped for the grep and sed commands to do their work correctly. Very handy for fixing hacked html files.

ps aux|grep -i [p]rocessname|awk '{ print $2 }'|xargs kill
sudo deborphan | xargs sudo apt-get -y remove --purge
find . -name 'pattern'| xargs du -hc
ls | egrep -v "[REGULAR EXPRESSION]" | xargs rm -v
2010-04-01 02:40:40
User: Saxphile
Functions: egrep ls rm xargs
Tags: files rm
-1

This is a slight variation of an existing submission, but uses regular expression to look for files instead. This makes it vastly more versatile, and one can easily verify the files to be kept by running ls | egrep "[REGULAR EXPRESSION]"

find . -name file.txt | xargs -e grep "token" -o | wc -l
curl http://www.reddit.com/r/wallpapers.rss | grep -Eo 'http:[^&]+jpg' | head -1 | xargs feh --bg-seamless
2010-03-27 00:08:40
Functions: grep head xargs
4

You'll need "feh" to set the background from the commandline. Install with "apt-get install feh"

Thanks to the Redditors on this thread: http://www.reddit.com/r/linux/comments/bira4/is_there_a_linux_version_of_this_preferably_a/

find * \( -name "*.[hc]pp" -or -name "*.py" -or -name "*.i" \) -print0 | xargs -0 wc -l | tail -n 1
2010-03-25 18:58:29
User: neologism
Functions: find tail wc xargs
Tags: find xargs wc
1

Finds all C++, Python, SWIG files in your present directory (uses "*" rather than "." to exclude invisibles) and counts how many lines are in them. Returns only the last line (the total).

ps -ef |grep oracle |grep pmon |awk '{print $2}' |xargs -I {} ps eww {} |grep pmon |grep -v grep |awk '{print $5 " " $6 " " $0}' |sed 's/\(S*\) \(S*\) .*ORACLE_HOME/\1 \2/g' |cut -f1,2,3 -d" "
2010-03-23 20:36:59
User: brianmuckian
Functions: awk cut grep ps sed xargs
0

get a list of currently running oracle dbs (identified by the pmon process)

show the executable that spawned the process and

show the ORACLE_HOME relative to the environment within which the process is running

tailored to AIX (sed on linux behaves...differently)

suggestions for a better way...please.

find . -type d | tac | xargs rmdir 2> /dev/null
2010-03-23 11:54:38
User: drmaciver
Functions: find rmdir tac xargs
-1

Remove all empty directories below the current directory. If directories become empty as the results of this, remove those too.

svn up | sed 's/\.//g' | cut -d ' ' -f3 | xargs svn log -r
pattern='regexp_pattern'; find . -type f -perm +220 ! -name '*.bak' -print0 | xargs -0 egrep -lZ $pattern | xargs -0 sed -i.bak -e "/$pattern/d"
git log -g --pretty=oneline | grep '}: commit' | awk '{print $1}' | head -1 | xargs git checkout -f
awk '/d[a-z]+$/{print $4}' /proc/partitions | xargs -i sudo hdparm -I /dev/{} | grep 'device size with M'
ls -d */* | sed -e 's/^/\"/g' -e 's/$/\"/g' | xargs mv -t $(pwd)
2010-03-01 23:43:26
User: leovailati
Functions: ls mv sed xargs
-1

You WILL have problems if the files have the same name.

Use cases: consolidate music library and unify photos (especially if your camera separates images by dates).

After running the command and verifying if there was no name issues, you can use

ls -d */ | sed -e 's/^/\"/g' -e 's/$/\"/g' | xargs rm -r

to remove now empty subdirectories.

find -type d -name ".svn" -print0 | xargs -0 rm -rf
2010-02-25 10:16:18
User: tuxilicious
Functions: find rm xargs
-1

man find: If no paths are given, the current directory is used. - Can anybody tell me why so many people are typing the dot?