2

Found this answer here for copying files and keeping directory structure.

copy files

find . -name '*.xxx' -exec cp -iv --parents -t /path/to/target_dir {} +

Is there an easy way to maintain the list of copied files and delete the files from the source directory?

1 Answer 1

2

"Delete the files from the source directory" usually translates to mv. Unfortunately mv does not have an option like --parent. The easiest solution with cp is probably: Call cp once for every file. Inefficient but easy to code:

find . -type f \
  -name '*.xxx' -exec cp -iv --parents {} /path/to/target_dir \; -delete

The file is deleted if cp exits successfully. -delete may be specific to GNU find.

7
  • @Huake Laging So this will traverse into sub directories?
    – sayth
    Jun 2, 2014 at 4:59
  • @sayth Does your find code? Jun 2, 2014 at 10:24
  • find . -name '*.xxx' -exec cp -iv --parents {} /path/to/target_dir \; -delete had to remove the -type f, it worked perfect then.
    – sayth
    Jun 2, 2014 at 11:40
  • 1
    @sayth That is very strange as -type f means "act on regular files only" (excluding directories, FIFOs, device files and the like). Jun 2, 2014 at 11:47
  • sayth:~$ find . -type f \ -name '*.mp3' -exec cp -iv --parents {} /home/sayth/Music \; -delete find: paths must precede expression: -name Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
    – sayth
    Jun 2, 2014 at 12:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .