Search code examples
linuxbashterminalfind

Find command which makes directory and copies file


I was trying to use the find command to find some files using an expression and create those particular directories in a destination folder and copy the respective files.

find /home  -type f -mmin -60

The above prints some files on the terminal which have some files under nested directories and I want to create these additional directories under output/ in the same hierarchy structure

find /home  -type f -mmin -60 -execdir sh -c 'mkdir -p 'output/$0' && cp "$1" "output/$0"' sh {} \;

This does not work at all and acts pretty weirdly


Solution

  • I would use install command:

    find /home -type f -mmin -60 -exec install -D -m644 {} output/{} \;
    

    It transparently copy files and run mkdir under the hood.