Commands using xargs (661)

  • The following command finds all the files not modified in the last 5 days under /protocollo/paflow directory and creates an archive files under /var/dump-protocollo in the format of ddmmyyyy_archive.tar


    0
    find /protocollo/paflow -type f -mtime +5 | xargs tar -cvf /var/dump-protocollo/`date '+%d%m%Y'_archive.tar`
    0disse0 · 2010-06-29 12:43:30 0
  • No need for grep, let awk do the match. This will not behave properly if the filenames contains whitespace, which is awk's default field separator.


    1
    svn st | awk '{if ($1 ~ "?") print $2}' | xargs svn add
    sciurus · 2010-06-19 03:07:26 0

  • -3
    ps ux|grep <process name>|awk '{print $2}'|xargs -n 1 kill
    metalx1000 · 2010-06-18 20:12:37 0

  • -3
    ps h -o pid,command | grep 'TEXT' | sed 's/^ \+//' | cut -d ' ' -f 1 | xargs -n 1 kill
    tiagofischer · 2010-06-18 18:44:22 0
  • on a dpkg managed system this PATTERN will help you generate .deb files from source AND remove all the dev libs you had to install. i hate cluttering up my machine with rouge packages and headers. it would be pretty darn easy on rpm systems as well. i just dont have a rpm managed system to test on right now. NOTE, you sharp ones will notice that it uninstalls the deb you just made! yeah, but the deb is still there to do with it what you want, like re install it. or you can just grep -v after the diff


    0
    dpkg-query -l > 1.lst; sudo apt-get install -y build-essential; ./configure; make; sudo checkinstall -D make install; dpkg-query --list > 2.lst; diff 1.lst 2.lst | grep '^>' | awk '{print $3}' | xargs sudo apt-get remove -y --purge
    danlangford · 2010-06-16 22:06:07 1
  • this is great if you loose you ssh connection (with out a screen session) or are working on a laptop with a bad battery, or just a power outage. Modifications: you may not need the -print; the mtime is last modified time in days


    -1
    find ./ -type f -mtime -1 -name .*.sw[po] -print | sed -r 's/^(.+)\/\.(\S+)\.sw[op]$/\1\/\2/' | xargs vim -r
    nodnarb · 2010-06-16 13:15:10 0
  • This will remove all installed kernels on your debian based install, except the one you're currently using. From: http://tuxtweaks.com/2009/12/remove-old-kernels-in-ubuntu/comment-page-1/#comment-1590


    4
    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
    mitzip · 2010-06-10 20:33:32 0

  • -1
    find ~/.thunderbird/*.default/ -name *.msf -print0 | xargs --no-run-if-empty -0 rm;
    tersmitten · 2010-06-07 12:34:21 0
  • The thunderbird message datastores get corrupt some times causing random failures, compaction to fail and general suck in thunderbird. Removing them causes thunderbird to rebuild the indexes and makes things quick again.


    -1
    find ~/.thunderbird/*.default/ -name *.msf | sed 's/ /\\ /g' | xargs rm {} \;
    allrightname · 2010-06-04 12:35:24 2
  • the command will calculate the size of hidden files Show Sample Output


    1
    find directory -maxdepth 1 -type f | xargs ls -l | awk 'BEGIN { SUM=0} { SUM+=$5 } END { print SUM/2^20 }'
    emacs · 2010-06-04 01:55:01 0
  • I found Flash eating one of my CPUs after resume, the command above will help with that. For optional kicks you can put it into a script in /etc/pm/sleep.d/ (aspect in #swhack wrote this for me)


    -1
    top -bn 1 | awk '{if($1 ~ /^[0-9]+$/ && $9 > 97) {print $1;exit}}'|xargs kill
    chx · 2010-06-02 13:51:40 0
  • It only encodes non-Basic-ASCII chars, as they are the only ones not well readed by UTF-8 and ISO-8859-1 (latin-1). It converts all * C3 X (some latin symbols like ASCII-extended ones) and * C2 X (some punctuation symbols like inverted exclamation) ...UTF-8 double byte symbols to escaped form that every parser understands to form the URLs. I didn't encode spaces and the rest of basic punctuation, but supposedly, space and others are coded as \x20, for example, in UTF-8, latin-1 and Windows-cp1252.... so its read perfectly. Please feel free to correct, the application to which I designe that function works as expected with my assumption. Note: I specify a w=999, I didn't find a flag to put unlimited value. I just suppose very improbable surpass the de-facto 255 (* 3 byte max) = 765 bytes length of URL Show Sample Output


    1
    od -An -w999 -t xC <<< "$1" | sed 's/[ ]\?\(c[23]\) \(..\)/%\1%\2/g;s/ /\\\\\x/g' | xargs echo -ne
    nightswimming · 2010-05-31 16:35:52 0
  • Works with files containing spaces and for very large directories.


    2
    find -type f -print0 | xargs -r0 stat -c %y\ %n | sort
    dooblem · 2010-05-29 13:40:18 0
  • find and normal files and list them sorting with modification time without group l: with detailed information t: sort with modification time r: reverse order h: show file's size in human-readable format, such as K(kilobytes), M(megabyes) etc. g: do not show group Show Sample Output


    -1
    find . -type f | xargs ls -ltrhg
    emacs · 2010-05-28 01:23:53 1

  • 1
    grep -R usepackage * | cut -d']' -f2 | cut -s -d'{' -f 2 | sed s/"}"/.sty"}"/g | cut -d'}' -f1 | sort | uniq | xargs dpkg -S | cut -d':' -f1 | sort | uniq
    prayer · 2010-05-22 19:37:26 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


    1
    find * -type f -not -name ".*" | xargs wc -l
    Leechael · 2010-05-21 21:03:31 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). Show Sample Output


    0
    watch 'find -maxdepth 1 -mindepth 1 -type d |xargs du -csh'
    shadycraig · 2010-05-19 13:13:57 4
  • 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.


    1
    rm-but() { ls -Q | grep -v "$1" | xargs rm -r ; }
    sata · 2010-05-13 09:28:56 0
  • 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) Show Sample Output


    0
    find / -type f -size +512000 | xargs ls -lh | awk '{ print $5 " " $6$7 ": " $9 }'
    johnss · 2010-05-12 17:21:12 0
  • Find and kill multiple instances of a process with one simple command.


    -3
    pgrep rouge-process | xargs sudo kill -9
    mheadd · 2010-05-09 22:30:05 3
  • 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).


    0
    find . \( -iname '*.[ch]' -o -iname '*.php' -o -iname '*.pl' \) | xargs wc -l | sort -n
    rbossy · 2010-04-30 12:21:28 5
  • 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) Show Sample Output


    15
    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
    polaco · 2010-04-28 23:45:35 1
  • Possible simplification of egrep-awk-sort with find and -exec with xargs. Show Sample Output


    2
    find . -maxdepth 1 -type f | xargs stat
    asolkar · 2010-04-26 20:51:54 2
  • copy some file from xx.m3u to target folder


    1
    more xx.m3u |grep -v "^#" |xargs -i cp {} target
    lishuai860113 · 2010-04-20 23:49:16 0
  • 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.


    -1
    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"
    rexington · 2010-04-12 21:45:16 0
  • ‹ First  < 15 16 17 18 19 >  Last ›

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. 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.

Share Your Commands



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: