在 Linux 中,如何复制所有不以给定字符串开头的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4669851/
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
In Linux, how to copy all the files not starting with a given string?
提问by mstaniloiu
I tried with the following command:
我尝试使用以下命令:
cp src_folder/[!String]* dest_folder
However, this command will copy all the files that don't start with any of the characters 'S','t','r','i','n','g' instead of copying files that don't start with "String".
但是,此命令将复制所有不以任何字符 'S','t','r','i','n','g' 开头的文件,而不是复制不以任何字符开头的文件以“字符串”开头。
采纳答案by Didier Trosset
A variation on Konrad answer, using cp
option -t
to specify target directory simplifies the last command. It creates a single cp
process to copy all the files.
Konrad 答案的变体,使用cp
选项-t
指定目标目录简化了最后一个命令。它创建一个cp
进程来复制所有文件。
ls src_folder | grep -v '^String' | xargs cp -t dest_folder
- list all files in
src_folder
- filter out all those that start with
String
- copy all remaining files to
dest_dir
- 列出所有文件
src_folder
- 过滤掉所有以
String
- 将所有剩余文件复制到
dest_dir
回答by Konrad Rudolph
ls src_folder | grep -v '^String' | xargs -J % -n1 cp % dest_folder
This will
这会
- list all files in
src_folder
- filter out all those that start with
String
(so that the rest remains) - Invoke the
cp
command- once for each of those files (
-n1
says to callcp
for each of them separately) - using, as its arguments,
% dest_folder
, where%
is replaced by the actual file name.
- once for each of those files (
- 列出所有文件
src_folder
- 过滤掉所有开头的
String
(以便其余部分保留) - 调用
cp
命令- 每个文件一次(
-n1
说分别调用cp
每个文件) - 使用,作为其参数,
% dest_folder
其中%
由实际文件名替换。
- 每个文件一次(
回答by TyrantWave
cp src_folder/!(String*) dest_folder
Try that ~ Chris
试试吧~克里斯
回答by Ignacio Vazquez-Abrams
In bash:
在 bash 中:
shopt -s extglob
cp src_folder/!(String*) dest_folder