Why I get the "omitting directory" error using globbing in cp command?

13.8k views Asked by At

I'm trying to copy the "file.txt" file into all the directories.

[root@mycomputer]# ls -lh
total 132K
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:47 ilo01
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo02
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo03
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo04
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo05
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo06
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo07
drwxr--r--.  2 postgres postgres 4.0K Jan  2 08:40 ilo08
drwxr--r--.  2 postgres postgres 4.0K Jan  2 10:03 ilo09
drwxr--r--. 11 postgres postgres 4.0K Jan  2 11:15 ilo10
-rw-r--r--.  1 postgres postgres  64K Jun 27  2016 file.txt

Using TAB to see the behaviour of the command to run:

[root@mycomputer]# cp -p file.txt ilo[0-1][0-9]
ilo01/ ilo02/ ilo03/ ilo04/ ilo05/ ilo06/ ilo07/ ilo08/ ilo09/ ilo10/

But I get this error:

[root@mycomputer]# cp -v -p file.txt ilo[0-1][0-9]*
`postgresTdas.txt' -> `ilo10/file.txt'
cp: omitting directory `ilo01'
cp: omitting directory `ilo02'
cp: omitting directory `ilo03'
cp: omitting directory `ilo04'
cp: omitting directory `ilo05'
cp: omitting directory `ilo06'
cp: omitting directory `ilo07'
cp: omitting directory `ilo08'
cp: omitting directory `ilo09'

Same thing happens with:

[root@mycomputer]# cp -p file.txt ilo*

and

[root@mycomputer]# cp -p file.txt ilo*/

I don't understand why "[0-1][0-9] doen't work the way I need.

I'm assuming that the copy is going to put the file.txt in the list that the TAB shows.

What am I missing?

2

There are 2 answers

0
arkascha On BEST ANSWER

Most implementations of the cp command are not able to copy to multiple targets. Nothing you can do about that. You need to work around that limitation calling cp multiple times. Easiest probably is something like that:

ls -d dir* | xargs -n 1 cp file.txt
2
Jean-François Fabre On

The arguments expand to the file + all the directories

cp considers the last argument as the target, so the other directories are considered as sources.

And because cp won't copy directories unless -r or -R option is set (copy directory and contents), you get the warnings on all directories but the last one.

I'd do that with a bash/sh script instead:

for d in ilo[0-1][0-9]
do
   cp -p file.txt $d
done