简体   繁体   English

在Linux中,如何复制不以给定字符串开头的所有文件?

[英]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 characters 'S','t','r','i','n','g' instead of copying files that don't start with "String". 但是,此命令将复制所有不以任何字符'S','t','r','i','n','g'开头的文件,而不是复制不复制的文件以“String”开头。

A variation on Konrad answer, using cp option -t to specify target directory simplifies the last command. Konrad回答的变体,使用cp选项-t指定目标目录简化了最后一个命令。 It creates a single cp process to copy all the files. 它创建一个单独的cp进程来复制所有文件。

ls src_folder | grep -v '^String' | xargs cp -t dest_folder
  • list all files in src_folder 列出src_folder中的所有文件
  • filter out all those that start with String 过滤掉所有以String开头的东西
  • copy all remaining files to dest_dir 将所有剩余文件复制到dest_dir

In bash: 在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 列出src_folder中的所有文件
  • filter out all those that start with String (so that the rest remains) 过滤掉所有以String开头的东西(剩下的就剩下了)
  • Invoke the cp command 调用cp命令
    • once for each of those files ( -n1 says to call cp for each of them separately) 每个文件一次( -n1分别为每个文件调用cp
    • using, as its arguments, % dest_folder , where % is replaced by the actual file name. 使用% dest_folder作为参数,其中%由实际文件名替换。
cp src_folder/!(String*) dest_folder

试试吧〜克里斯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在linux中复制特定文件? - how to copy specific files in linux? 使用 glob() 获取所有不以给定文本开头的文件 - Using glob() to get all files not starting with given text 高效获取大型文本文件中以给定字符串开头的所有行 - Efficiently get all lines starting with given string for a large text file PHP删除包含给定字符串的所有文件 - PHP Delete all files that contain a given string 在python中给定起始匹配条件的情况下,如何解析/替换字符串的一部分? - How to parse/substitute part of a string given a starting matching condition in python? 列出所有不以数字开头的文件 - List all files not starting with a number 在可编辑的div中,如何根据给定的字符串“开始索引”和“替换字符串”来替换字符串 - How to replace string given its “starting index” and “string to replace” in an editable div 给定字符串的开始和结束部分时,如何使用正则表达式提取字符串的一部分 - How to extract a part of a string using regex, when starting and ending portion of the string is given 如何获取Javascript中以'{'开头,'}'结尾的所有单词? - How to get all the words in a string starting with '{' and ending with '}' in Javascript? 不以几个给定单词开头的字符串的正则表达式模式 - Regex Pattern for a string not starting with a couple of given words
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM