Ajedi32
Ajedi32

Reputation: 48368

cp: silence "omitting directory" warning

I'm using the command cp ./* "backup_$timestamp" in a bash script to backup all files in directory into a backup folder in a subdirectory. This works fine, but the script keeps outputting warning messages:

cp: omitting directory `./backup_1364935268'

How do I tell cp to shut up without silencing any other warnings that I might want to know about?

Upvotes: 12

Views: 12785

Answers (3)

hek2mgl
hek2mgl

Reputation: 157967

Probably you want to use cp -r in that script. That would copy the source recursively including directories. Directories will get copied and the messages will disappear.


If you don't want to copy directories you can do the following:

  • redirect stderr to stdout using 2>&1
  • pipe the output to grep -v
script 2>&1 | grep -v 'omitting directory' 

quote from grep man page:

  -v, --invert-match
          Invert the sense of matching, to select non-matching lines.

Upvotes: 3

Sireesh Yarlagadda
Sireesh Yarlagadda

Reputation: 13726

When copying a directory, make sure you use -R

cp -R source source_duplicate_copy_name

-R, -r, --recursive copy directories recursively

  --reflink[=WHEN]         control clone/CoW copies. See below
  --remove-destination     remove each existing destination file before
                             attempting to open it (contrast with --force)
  --sparse=WHEN            control creation of sparse files. See below
  --strip-trailing-slashes  remove any trailing slashes from each SOURCE

Upvotes: -2

Michael Szymczak
Michael Szymczak

Reputation: 1326

The solution that works for me is the following:

find -maxdepth 1 -type f -exec cp {} backup_1364935268/ \;

It copies all (including these starting with a dot) files from the current directory, does not touch directories and does not complain about it.

Upvotes: 9

Related Questions