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

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

Linux find mmin minutes

 

mmin option is to find files/directories with last modified in minutes.

Find files that are exactly 2 minutes old:

$ find . -mmin 2

Find files that are less than 2 minutes old:

$ find . -mmin -2

Find files that are more than 2 minutes old:

$ find . -mmin +2

Find files that are more than 2 hours old:

$ find . -mmin +120

Find files that are less than 2 hours old:

$ find . -mmin -120

-Sany

Linux find recently updated files

 

“Find” is on of the beautiful command in Linux.

find command is used to search for files in a directory hierarchy.

To find recently updated files (lets say updated in last 15 minutes) use following command:

$ find <INPUT_PATH> -mmin +0 -mmin -15

To find files modified between 6 and 9 minutes ago:

$ find <INPUT_PATH> -mmin +5 -mmin -10

To find files which are more than 10 days old

$ find <INPUT_PATH> -mtime +10

-Sany

Find directories/files with find command

 

Find is one of the beautiful command in Linux.

With reference to man page of find command it is used search for files in a directory hierarchy.

Show only directories with find command:

$ find <INPUT_DIRECTORY> -type d

Above command will list only directories recursively from INPUT_DIRECTORY path.

Show only files with find command:

$ find <INPUT_DIRECTORY> -type f

Above command will list only files recursively from INPUT_DIRECTORY path.

If you are not given any INPUT_DIRECTORY current directory will be considered as INPUT_DIRECTORY.

-Sany