Search This Blog

Showing posts with label linux find command. Show all posts
Showing posts with label linux find command. Show all posts

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.

Finding files with extensions using find command and files which are 90 days old



find /path/to/search -mtime +90 -type f \( -name "*.txt" -o -name "*.pdf" -o -name "*.doc" \)

This will find all the files in directory /path/to/search which are 90 days old and files having extention as txt, pdf, doc. 

Like this you can add many extention and specify time accordingly to find specified days old files.


Featured Posts

Unlock Hidden Windows Startup and Shutdown Details : How to Enable Verbose Startup and Shutdown Messages in Windows 11

WINDOWS TROUBLESHOOTING · FULL GUIDE Unlock Hidden Windows Startup and Shutdown Details Enable Verbose Status...