Thursday, February 1, 2024

Move files found using find command to another directory with struture of source

To move files found using the find command to another directory while maintaining the source directory structure, you can use the cp (copy) command along with the --parents option and then rm (remove) to delete the original files. Here's an example:

find /path/to/search -type f -name "*.txt" -exec sh -c 'cp --parents "{}" /path/to/destination/ && rm "{}"' \;

In this command:

  • /path/to/search is the directory where you want to start the search.
  • -type f specifies that you are looking for files (not directories).
  • -name "*.txt" specifies the pattern to match for file names (replace with your desired file extension or pattern).
  • -exec sh -c 'cp --parents "{}" /path/to/destination/ && rm "{}"' \; executes a shell command for each file found. This command copies the file to the destination directory while preserving the directory structure using --parents and then removes the original file.

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

Enhancing Unix Proficiency: A Deeper Look at the 'Sleep' Command and Signals

Hashtags: #Unix #SleepCommand #Signals #UnixTutorial #ProcessManagement In the world of Unix commands, there are often tools that, at first ...