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

前端 未结 6 1903

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

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

6条回答
  •  迷失自我
    2021-02-03 11:50

    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 -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.

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

提交回复
热议问题