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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2065447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:51:38  来源:igfitidea点击:

How do I exclude a folder when performing file operations i.e. cp, mv, rm and chown etc. in Linux

linuxbashcommand-linefilesystems

提问by Camsoft

How do you exclude a folder when performing file operations i.e. cp etc.

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

I would currently use the wild card * to apply file operation to all, but I need to exclude one single folder.

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

The command I'm actually wanting to use is chownto change the owner of all the files in a directory but I need to exclude one sub directory.

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

回答by Ignacio Vazquez-Abrams

If you're using bash and enable extglob via shopt -s extglobthen you can use !(<pattern>)to exclude the given pattern.

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

回答by Paused until further notice.

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

回答by danben

for file in *; do
  if [ $file != "file_I_dont_want_to_chown" ]
    then
      chown -R Camsoft $file
  fi
done 

回答by Christopher Bruns

Combine multiple small sharp tools of unix: To exclude the folder "foo"

结合unix的多个小利器:排除文件夹“foo”

% ls -d * | grep -v foo | xargs -d "\n" chown -R Camsoft

回答by mholzmann

For this situation I would recommend using find. You can specify paths to exclude using the -not -iwhilename 'PATH'. Then using exec you execute the command you want to execute

对于这种情况,我建议使用 find。您可以使用 -not -iwhilename 'PATH' 指定要排除的路径。然后使用 exec 你执行你想要执行的命令

find . -not -iwholename './var/foo*' -exec chown www-data '{}' \;

Although this probably does help for your situation I have also see scripts set the immutable flag. Make sure you remove the flag when your done you should use trap for this just in case the script is killed early (note: run from a script, the trap code runs when the bash session exits). A lot of trouble in my option but it's good in some situations.

虽然这可能对您的情况有所帮助,但我也看到脚本设置了不可变标志。确保在完成后删除标志,您应该为此使用陷阱,以防脚本提前终止(注意:从脚本运行,陷阱代码在 bash 会话退出时运行)。我的选择有很多麻烦,但在某些情况下很好。

cd /var
trap 'chattr -R -i foo > /dev/null 2>&1' 0
chattr -R +i foo
chown -R www-data *

回答by Martin Beckett

Another option might be to temporarily remove permission on the that file /folder.
In Unix you need 'x' permission on a directory to enter it.

另一种选择可能是暂时删除对该文件/文件夹的权限。
在 Unix 中,您需要对目录具有“x”权限才能进入它。

edit: obviously this isn't goign to work if you are backing up a live production database - but for excluding your 'interesting images' collection when copying documents to a USB key it's reasoanable.

编辑:显然,如果您要备份实时生产数据库,这将不起作用 - 但为了在将文档复制到 USB 密钥时排除“有趣的图像”集合,这是合理的。