bash cp:静音“省略目录”警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15774023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:04:58  来源:igfitidea点击:

cp: silence "omitting directory" warning

bashcp

提问by Ajedi32

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 ./* "backup_$timestamp"在 bash 脚本中使用该命令将目录中的所有文件备份到子目录中的备份文件夹中。这工作正常,但脚本不断输出警告消息:

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?

我如何告诉 cp 关闭而不关闭我可能想知道的任何其他警告?

采纳答案by hek2mgl

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

可能您想cp -r在该脚本中使用。这将递归复制源,包括目录。目录将被复制,消息将消失。



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
  • 使用重定向标准错误到标准输出 2>&1
  • 将输出通过管道传递给 grep -v
script | grep -v 'omitting directory' 

quote from grep man page:

引自 grep 手册页:

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

回答by Michael Szymczak

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.

它从当前目录复制所有(包括以点开头的文件)文件,不接触目录,也不抱怨它。

回答by Sireesh Yarlagadda

When copying a directory, make sure you use -R

复制目录时,请确保使用 -R

cp -R source source_duplicate_copy_name

cp -R source source_duplicate_copy_name

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

-R, -r, --recursive 递归复制目录

  --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