The Joys of xargs: Page 3

Posted September 2, 2008

Juliet Kemp

Juliet Kemp

(Page 3 of 4)

You can use xargs if you need to rename lots of files (e.g. datestamping). This command will rename each file in the current directory from filename.txt to 20080815-filename.txt:

ls | xargs -I {} mv {} 20080815-{}
This works because {} is a placeholder meaning "the current argument". (You can use xxx or yyy or any other string instead of {} if you want, as well, and it'll do exactly the same thing.) -I implies -n1, because you want to act on each file individually.

Or you might want to move all the files in directory 1 into directory 2:

ls dir1 | xargs -I {} -t mv dir1/{} dir1/{}


I've concentrated here on using xargs to manipulate files in various ways, but you can use the same tricks for other commands. For example, if you have a file containing a list of IP addresses,

cat iplist | xargs -n1 nmap -sV
would run nmap on each IP address at a time. Play around with it a bit and see what you can do!

This article was first published on

Page 3 of 4

Previous Page
1 2 3 4
Next Page

Comment and Contribute


(Maximum characters: 1200). You have characters left.



IT Management Daily
Don't miss an article. Subscribe to our newsletter below.