Disclaimer

These scripts come without warranty of any kind. Use them at your own risk. I assume no liability for the accuracy, correctness, completeness, or usefulness of any information provided by this site nor for any sort of damages using these scripts may cause.
Showing posts with label Script to move file from one directory to another. Show all posts
Showing posts with label Script to move file from one directory to another. Show all posts

Monday, November 27, 2017

How to move files from one directory to another using find and xargs command



find - search for files in a directory hierarchy
xargs - build and execute command lines from standard input

Using find and xargs we can search any file in the directory hiearchy and xargs execute the command accepting the output of find command as input.

Use xargs with -I option to replace occurrences of replace-str in the initial-arguments with names read from standard input(In our case from find command). The placeholder following the -I is a string that can appear multiple times in the command.

Example: Below command will find all files with pattern *.txt in current directory and move to /NewDir/.

find . -name "*.txt" | xargs -I '{}' mv '{}' /NewDir/'{}'


Here '{}' is placeholder