find mv/rename with xargs example

 

Moving or renaming files with xargs is a bit tricky operation.

mv require both input and output arguments.

Example to rename:

Here I am trying to rename a file t.txt to t.txt.bak

$ find t.txt -print0 | xargs -n 1 -0 -I {} mv {} {}.bak

or

$ find t -print0 | xargs -0 -I {} mv {} {}.bak

where

-print0 will print output without new line.

You can rename {} to something else. In the following example {} is renamed as test. All three example will do same functionality.

$ find t -print0 | xargs -n 1 -0 -I test mv test test.bak

Example to move/mv:

Here I am trying to move file t.txt to x directory.

$ find t.txt -print0 | xargs -n 1 -0 -I test mv test x/

or

$ find t.txt -print0 | xargs -n 1 -0 -I {} mv {} x/

-Sany

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s