0

I'm trying to copy (backup) a specific folder files (no directories and sub-directories files) to a second folder. I nested find command with backstick but it give me error for each file with file_name (copy).extension

cp -a -t /home/gianmarco/backup2 `find /home/gianmarco/backup1 -maxdepth 1 -type f`
cp: cannot stat '/home/gianmarco/backup1/helloWorld': No such file or directory
cp: cannot stat '(copy).txt': No such file or directory
cp: cannot stat '/home/gianmarco/backup1/2nuovoFile': No such file or directory
cp: cannot stat '(copy).txt': No such file or directory

Any suggestion? Thanks!

4 Answers4

2

Use find to run cp on those files:

find /home/gianmarco/backup1 -maxdepth 1 -type f -exec cp -at /home/gianmarco/backup2 {} +

This way, spaces and other special characters in the filenames won't be a problem. When you use command substitution without quoting, the shell will split the output on spaces, then perform wildcard expansion, etc. It's simpler to use find itself for this. Or see How can I exclude all subdirectories but include files of a directory in rsync?

muru
  • 197,895
  • 55
  • 485
  • 740
0

Try below command:

find /path_to_source/ -depth -type f -exec cp -apt /path_to_target_directory/ {} \;

where the -depth option makes find traverse the file system in a depth-first order.

David Foerster
  • 36,264
  • 56
  • 94
  • 147
0

Try to use rsync. In this exmaple I call syncronize all files & folders in src dir to dst, excluding .sh files, preserving permissions, ownership, timestamp:

rsync -a --exclude '*.sh' /path/to/src /path/to/dst

Also, you can add some additional parameters, it depends on your situation.

M. Dm.
  • 1,482
-1

find /home/gianmarco/backup1 -maxdepth 1 -type f -exec cp {} /home/gianmarco/backup2

muclux
  • 5,154