开发者

In Linux, how to copy all the files not starting with a given string?

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 characte开发者_如何学运维rs 'S','t','r','i','n','g' instead of copying files that don't start with "String".


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.

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


In bash:

shopt -s extglob
cp src_folder/!(String*) dest_folder


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 call cp for each of them separately)
    • using, as its arguments, % dest_folder, where % is replaced by the actual file name.


cp src_folder/!(String*) dest_folder

Try that ~ Chris

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜