如何在Linux中执行文件操作(例如cp,mv,rm和chown等)时排除文件夹

Cam*_*oft 11 linux filesystems bash command-line

如何在执行文件操作时排除文件夹,即cp等.

我目前使用通配符*将文件操作应用于所有,但我需要排除一个单个文件夹.

我实际想要使用的命令是chown更改目录中所有文件的所有者,但我需要排除一个子目录.

Ign*_*ams 18

如果您正在使用bash并启用extglob,shopt -s extglob那么您可以使用它!(<pattern>)来排除给定的模式.

  • `chown 0755 -R!(foo)` (4认同)
  • 我已经执行了上面的命令.你能给我一个将!()语法与chown结合使用的例子吗? (2认同)

Pau*_*ce. 8

find dir_to_start -name dir_to_exclude -prune -o -print0 | xargs -0 chown owner

find dir_to_start -not -name "file_to_exclude"  -print0 | xargs -0 chown owner
Run Code Online (Sandbox Code Playgroud)


dan*_*ben 5

for file in *; do
  if [ $file != "file_I_dont_want_to_chown" ]
    then
      chown -R Camsoft $file
  fi
done 
Run Code Online (Sandbox Code Playgroud)