1

I have a dir that contains files and subdirs. I want to copy files in the dirs only, so I run

cp -v src/* dst/

and get error cp: -r not specified; omitting directory. I'd be ok to ignore it but the command fails if the -e bash flag is set.

How can I avoid my script failure assuming I do NOT want to copy recursively and I do want -e flag? The only thing I can think about is to use find -type f -maxdepth... -exec cp... but it looks too heavy. Any simpler way?

p.s. I know about -e bottom rocks, thanks.

2
  • Does appending || true to the command eliminate the failure when -e is set?
    – doneal24
    Nov 7, 2022 at 20:07
  • This is the best solution so far. Please move it from comment to answer and I'll accept it. @doneal24
    – Putnik
    Nov 8, 2022 at 11:32

2 Answers 2

2

A common technique to have -e set in your script but still have commands that you know can produce errors is to make them part of a conditional command, e.g.

cp -v src/* dst/ || true

This construct will never produce an error that would cause the script to exit. No fancy programming required and it is applicable to many commands, not just to the cp example.

1

With "extended glob" (shopt -s extglob) enabled, use src/!(*/) to match all items except those which would match src/*/ (directories or symlinks to directories).

Another common way to copy specific files is cpio -p, find src/ [...] | cpio -p dst/.

You must log in to answer this question.

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