Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cp: silence "omitting directory" warning

Tags:

bash

cp

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?

like image 846
Ajedi32 Avatar asked Apr 02 '13 20:04

Ajedi32


People also ask

How do I fix cp omitting directory?

cp omitting directory error solution cp: omitting directory error tells that directories are not copied as the cp command by default works on the files only. Simply, use the cp command with -r or -R (recursive) as an argument to resolve cp: omitting directory error.

How does the cp command work?

You use the cp command for copying files from one location to another. This command can also copy directories (folders). [file/directory-sources] specifies the sources of the files or directories you want to copy. And the [destination] argument specifies the location you want to copy the file to.

Is rsync faster than cp?

In the general case, rsync is definitively slower than a “random copy” (which I assume to be just cp ). This is simply because rsync has to do more work than cp in the general case: Read source files to build list of hashes. Read destination files to build list of hashes.


2 Answers

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.
like image 38
hek2mgl Avatar answered Oct 12 '22 22:10

hek2mgl


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.

like image 182
Michael Szymczak Avatar answered Oct 13 '22 00:10

Michael Szymczak