简体   繁体   English

移动不包含特定字符串的文件

[英]move files that no not contain specific string

I would like to move files using mv that do not contain the letter S in the filename. 我想使用mv移动文件名中不包含字母S的文件。 Could not find anyhting in the mv manual. 在mv手册中找不到任何内容。 Maybe combination with find or grep ? 也许与findgrep相结合? It has to be case-sensitive. 它必须区分大小写。

input: 输入:

  file1
  fileS1
  file2
  fileS2

file to move: 要移动的文件:

  file1
  file2

You can do the selection in pure Bash without any extra software, if you enable extended globbing , which is off by default: 如果启用了Extended Globbing ,则可以在纯Bash中进行选择,而无需任何其他软件,默认情况下该功能处于关闭状态:

shopt -s extglob
mv !(*S*) /target/dir

For more information, search for extglob in the bash(1) manual page (the info is at the second match). 有关更多信息, extglobbash(1)手册页中搜索extglob (信息位于第二个匹配项)。

You can use find with the -not flag for example. 例如,您可以将find-not标志一起使用。

find /path/to/source/dir -type f -not -name '*S*' \
    | xargs mv -t /path/to/target/dir

您还可以使用ls的Ignore-pattern开关,例如:

mv $(ls -I '*S*') /target/dir

GREP 's -v flag can also be used here. GREP-v标志也可以在这里使用。 According to the docs, 根据文档,

-v, --invert-match Invert the sense of matching, to select non-matching lines. -v,--invert-match反转匹配感,以选择不匹配的行。

Just use 只需使用

ls | grep -v '*S*' | xargs mv -t target_dir/

Also, see this post. 另外,请参阅这篇文章。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM