//Tips tagged xargs
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
To delete a file who's file name is a pain to define (eg. ^H^H^H) find it's inode number with the command "ls -il". Use the line below to find and delete the file.
find . -inum 12345 | xargs rm


View Comments »



To find out the number of files of each type in your current directory try the following:

find ${*-.} -type f | xargs file | awk -F, '{print $1}' | awk '{$1=NULL;print $0}' | sort | uniq -c | sort -nr  

5 PHP script text 2 data 2 Zip archive data 2 GIF image data 1 PNG image data

(You may want to add this as an alias rather than type it in each time!)


View Comments »

Add the following alias and function to your profile to be able to copy and paste files at the command line:

ccopy(){ cp $1 /tmp/ccopy.$1; }
alias cpaste="ls /tmp/ccopy* | sed 's|[^\.]*.\.||' | xargs -I % mv /tmp/ccopy.% ./%"

You can see below how this can be used:
blackbird:~/tst tks1$ ls
1.txt   2.txt   t1.tss  t2.tss  t3.tss
blackbird:~/tst tks1$ ccopy 1.txt
blackbird:~/tst tks1$ ccopy 2.txt

blackbird:~/tst tks1$ cd ../tst2 blackbird:~/tst2 tks1$ ls
blackbird:~/tst2 tks1$ cpaste
blackbird:~/tst2 tks1$ ls 1.txt 2.txt


View Comments »

To remove empty directories (even if filenames or dirnames contain spaces or weird characters) from a tree you can do:


find . -type d -empty -print0 | xargs -0 rmdir


View Comments »

Make a backup of existing files, afterwards copy new files from somedir:

1. Go to proddir
ls /update-200805/ |xargs -n1 -I xxx cp xxx xxx.`date +%Y%m%d` ; cp /update-200805/* . 


View Comments »

The following command creates in the /usr/project directory, a copy of the current working directory structure:

find . -type d -print|sed 's@^\.\{0,1\}@/usr/project@' | sed 's/ /\\ /' | xargs mkdir -p


View Comments »

Kick all users other than you from your box and keep them out.

watch -d 'w | awk 'NR==4 {print "/dev/"$2}' | xargs fuser -k'


View Comments »

And again about file extension changing.

There are two tips about this:
#27 - http://www.shell-fu.org/lister.php?id=27
#544 - http://www.shell-fu.org/lister.php?id=544

I'm happy to provide one more variant. It's up to you which one is the most useful.
function chext(){
  local fname
  local new_ext="$1"
  shift
  IFS=$'\n'
  for fname in $@
  do
    mv "$fname" "${fname%.*}.$new_ext"
  done
}

If you place this function into .bashrc, then you may use it like as follows:
chext new_ext *.old_ext
chext html `find ~ -iname "*.htm"`
find ~ -iname "*.htm" | xargs chext html


View Comments »


ls | xargs rm

Sometime there are so many files in a directory than the rm command doesn't work
[root@server logs]# rm *
bash: /bin/rm: Argument list too long


On this case the best option is to use ls in conjuntion with xargs
[root@server logs]# ls | xargs rm



View Comments »

This is the general form; it will run 'mycommand' once for each line of text you enter with that line of text supplying the arguments to the command:

cat | xargs -L1 mycommand

Changing to ledit adds readline support:

ledit | xargs -L1 mycommand

Example: A simple REPL for sending commands to a DBus object:

ledit | xargs -L1 -ILINE dbus-send --print-reply --dest=mydest myobj myiface.LINE


View Comments »

It's for removing those orphaned config files! :D

aptitude search ~c | awk '{ print $2 }' | xargs aptitude -y purge


View Comments »




Home Latest Browse Top 25 Random Hall Of Fame Contact Submit