1

I want to find images, from a directory, that were added in last 1 year and copy them to a new directory preserving original folder structure.

I am using find but it is not copying anything. is there any way it can be done using 1 line command?

find image/* -mtime +356 -exec cp {} modified-last-year/ \;

I am in the image directory when running this command and i want to only search image folder recursively.

[EDIT]
After the two answers I did following.
1. find image/* -mtime +356 | cpio -pd /target_dir
I get 0 Blocks.

  1. find /full/path/to/image -mtime 365 -type f ( "-name *.jpg -o -name *.gif ) -execdir cp {} /full/path/to/image_target_dir/modified-last-year \;
    AND
  2. find /full/path/to/image -mtime 365 -type f -execdir cp {} /full/path/to/image_target_dir/modified-last-year \;
    Nothing copied.

    AND simply find to get count of files with and without -type f.

  3. find /full/path/to/image -mtime 365 -type f | wc -l
    i get 0.

    I could verify that there are indeed files with in image dir and in sub directories which were added in last 1 yr. infact there should be more than 200 images.

    [EDIT 2]
    I have to also exclude one directory from find so the following code worked fine.

    Thanks to 1st answer, i was able to create this.
    find /full/path/to/image/* -path /full/path/to/image/ignored_dir -prune -o -print -mtime -365 | cpio -pd /full/path/to/target_dir/modified-last-year

2

You can use find and cpio in passthrough mode:

 find  image/ -mtime -365 | cpio -pd /target_dir

EDIT: removed unnecessary * from the find path.

  • For some reason I get 0 Block. I could verify that there are indeed files with in image dir and in sub directories which were added in last 1 yr. infact there should be more than 200 images. – Syed Mudabbir Aug 29 '16 at 16:07
  • The nice thing about using find and cpio is that you can test that the find command returns the desired files, before piping to cpio. What does 'find image/ -mtime +356' return? – bjerre Aug 29 '16 at 16:45
  • With a plus sign it was giving no results. So i went out of image dir one level up and executed this command and it worked just fine. Is there a way to exclude one particular directory? – Syed Mudabbir Aug 29 '16 at 16:49
  • Also does it check -mtime for subdirectories as well? – Syed Mudabbir Aug 29 '16 at 16:53
  • Okay so i was able to finally successfully execute this... ` find /full/path/to/image/* -path /full/path/to/image/ignored_dir -prune -o -print -mtime -365 | cpio -pd /full/path/to/target_dir/modified-last-year` Also added for ignored dir and everything worked out well. Thank you. – Syed Mudabbir Aug 29 '16 at 17:32
1

If you already are in directory image, then find image/* ... spells trouble. Instead for relative paths, either use find . (etc) where the "." signifies the present directory and its sub-directory tree, or better yet, use absolute paths:

$ find /full/path/to/imagedir -mtime -365 -execdir cpio -pdv {} /absolute/path/to/targetdir \;

Provided you have proper permission to do so, the above should copy all files modified more recently than 365*24 hours (before today) in yr target-directory, while preserving the directory structure.

Option -execdir is safer than -exec, does the same but is executed from the directory where the selected file is found.

You can further restrict the files being parsed by find by specifying the options -type f \( "-name \*.jpg -o -name \*.gif \) in the above cmd.

cpio's options are:

-p for passthrough (does not make an archive out of the set of dound files)
-d to preserve the directory structure
-v to visualize files being copied

Look at the manual page for find, at https://www.gnu.org/software/cpio/manual/cpio.html for the complete cpio online manual, and remember www.startpage.com is a friend that does not make you the product when you look for answers.

  • For some reason I get 0 files. I could verify that there are indeed files with in image dir and in sub directories which were added in last 1 yr. infact there should be more than 200 images. I did $ find /full/path/to/image -mtime 365 -type f \( "-name \*.jpg -o -name \*.gif \) -execdir cp {} /full/path/to/image_target_dir/modified-last-year \; and I also did just simple find /full/path/to/image -mtime 365 | wc -l but i get 0. – Syed Mudabbir Aug 29 '16 at 16:10
  • @SyedMudabbir: very odd. Can you tell us the exact absolute paths you use to issue yr cmd ? Then execute $ find /full/path/to/image -mtime 365 -type f \( "-name \*.jpg -o -name \*.gif \); echo $? and print out the resulting 10 first lines if any in an EDIT in yr original question. There is something iffy going on. – Cbhihe Aug 29 '16 at 16:28
  • I was running this command while in image dir itself. The target dir was also in image dir. So i moved target dir out of image, went up one level .. and I just modified the command and made -mtime 365 as -mtime -365. I was using absolute path for both dirs. This worked but only issue was it was not including parent dirs and was copying only files. – Syed Mudabbir Aug 29 '16 at 16:46
  • @SyedMudabbir: I did think that something along that line was causing trouble. To preserve directory structure, do (as suggested by bjerre above): $ find /full/path/to/imagedir -mtime -365 -execdir cpio -pd {} /absolute/path/to/targetdir \;. cpio's options are -p for passthrough (does not make an archive out of the set of dound files) and -dto preserve the directory structure. Sorry for the typo on -365. My bad ! – Cbhihe Aug 29 '16 at 18:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.