简体   繁体   English

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

[英]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.

相关问题 我如何在Linux中的cp(复制)或rm(删除)命令上发送电子邮件 - How can i send an email on cp (copy) or rm (remove) command in linux 在linux shell中,如何按时间cp/rm文件? - In linux shell, How to cp/rm files by time? 如何从父进程获取子进程的状态,即它是停止、继续还是退出? 对于 Linux、C 语言 - How do I get the status of a child process from a parent, i.e. has it stopped, continued, or exited? For Linux, C lang 如何排除 Linux 中的字符 - How do I exclude a character in Linux cp 和 mv 中的 linux 通配符用法 - linux wildcard usage in cp and mv “运行命令”文件编辑(即“*.rc”文件):像Linux一样回声到终端? - "Run Command" file editing (i.e. " *.rc " files): Echoing to the terminal like Linux? 在现代 Linux(即 ver&gt;=3.0.0)上,是否有更简单的方法在不相关的进程之间共享文件描述符? - Is there an easier way to share file descriptors between unrelated processes on modern day Linux (i.e., ver>=3.0.0)? Linux 如何使用 rm 和 exclude 开关 - Linux how to use rm and exclude switch 如何计算linux中两个二进制文件(即两个可执行文件)之间的差异 - how to compute differences between two binaries (i.e., two executables) in linux 如何在节点 js docker 容器外(即在 linux 主机内)保存上传的文件? - How to save uploaded files outside node js docker container (i.e. inside linux host machine)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM