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

Advertisements

Linux mv with xargs

 

mv(move) command file/directory name we want to move and destination name.

Using mv command with xargs is bit tricky.

Here is the example to move all files from oldDir to newDir:

$ find oldDir -type f | xargs -n 1 -I '{}' mv {} newDir/

where,

-type f is to find only files

-n 1 is for one argument at a time, that will move one file at time.

{} is the default argument list marker. You need to use {} this with various command which take more than two arguments at a time.

The main advantage of using xargs is it’s faster and efficient.

-Sany