【问题标题】:copy files to another directory except ".txt" file extension将文件复制到除“.txt”文件扩展名之外的另一个目录
【发布时间】:2018-01-08 19:32:47
【问题描述】:

我有很多不同文件扩展名的文件,所以我想复制所有文件,除了

“.txt”

到另一个目录。

我尝试使用以下命令查找所有可能具有不同文件扩展名的文件,除了“.txt”

ls -lrt /home/updatet/test/ -I "*.txt"

复制

ls -1 /home/updatet/test/ | xargs cp {} demo/

【问题讨论】:

标签: shell unix sh ls cp


【解决方案1】:

使用 bash extglob:

有关文档,请参阅 http://wiki.bash-hackers.org/syntax/pattern#extended_pattern_language

shopt -s extglob
cp /home/updatet/test/!(*.txt) demo/

便携:

for f in /home/updatet/test/*; do
  case $f in
    *.txt) :;;
    *)     cp "$f" demo/ ;;
  esac
done

【讨论】:

    【解决方案2】:
    find /home/updatet/test ! -name \*.txt -exec cp {} demo/ \;
    

    【讨论】:

    • -exec ... {} + 要求 {}+ 之前直接。你需要在这里使用效率较低的\;,除非你打算通过使用-exec cp -t demo/ -- {} +来依赖GNU cp
    • 对。 s/+/\;/ 将编辑答案。谢谢@CharlesDuffy
    猜你喜欢
    • 1970-01-01
    • 2018-03-13
    • 2013-10-14
    • 2015-06-12
    • 2017-12-11
    • 2017-11-18
    • 2011-08-18
    • 2012-02-15
    相关资源
    最近更新 更多