[英]How do I exclude a folder when performing file operations i.e. cp, mv, rm and chown etc. in Linux
How do you exclude a folder when performing file operations ie 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 chown
to change the owner of all the files in a directory but I need to exclude one sub directory. 我实际想要使用的命令是
chown
来更改目录中所有文件的所有者,但我需要排除一个子目录。
如果你正在使用bash并通过shopt -s extglob
启用extglob,那么你可以使用!(<pattern>)
来排除给定的模式。
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
for file in *; do
if [ $file != "file_I_dont_want_to_chown" ]
then
chown -R Camsoft $file
fi
done
结合unix的多个小锐利工具:排除文件夹“foo”
% ls -d * | grep -v foo | xargs -d "\n" chown -R Camsoft
For this situation I would recommend using find. 对于这种情况,我建议使用find。 You can specify paths to exclude using the -not -iwhilename 'PATH'.
您可以使用-not -iwhilename'PATH'指定要排除的路径。 Then using exec you execute the command you want to execute
然后使用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).
确保在完成后删除标记,以便在脚本被提前杀死的情况下使用陷阱(注意:从脚本运行,陷阱代码在bash会话退出时运行)。 A lot of trouble in my option but it's good in some situations.
我的选择很麻烦,但在某些情况下它很好。
cd /var
trap 'chattr -R -i foo > /dev/null 2>&1' 0
chattr -R +i foo
chown -R www-data *
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密钥时排除你的“有趣图像”集合是可以理解的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.