SSH command to cycle through directories EXCEPT a few specified

Is it possible to execute the bash command through SSH, which will iterate over several EXCEPT 3 or 4 directories that I specify? Sort of:

Delete public_html/outdated/for all directories /home/except /home/exception1/, /home/exception2/and/home/exception3/

I am on a HostGator Dedicated Linux server.

+3
source share
3 answers

Test first!

shopt -s extglob
rm -rf /home/!(exception1|exception2|exception3)/public_html/outdated/
+2
source

This is all one line, but I divided it into readability

find /home
    \( -wholename '/home/exception1'
    -o -wholename '/home/exception2'
    -o -wholename '/home/exception3' \)
    -prune -o
  -wholename '*/public_html/outdated' -type d
  -exec rm -rvf \{} \;

Before running this, I highly recommend replacing the bit -exec rm -rvf \{} \;with -printto make sure that it prints only what you want to delete.

: find . \(... \)' matches directories you want to skip entirely (ie: prune). The standard pattern for using -prune is to say what you want to prune out, then -prune -o`, , .

public_html/outdated, -wholename '*/public_html/outdated' -type d. (-type d " " ).

, , . -print, , , .

: :

find: `/home/foo/public_html/outdated': No such file or directory

, find , . - , .

+3

- , , .

cd /home
ls */public_html/outdated

; grep -v

ls */public_html/outdated | grep -Ev ^exception1/ | grep -Ev ^exception2/ | grep -Ev ^exception3/

, rm -rf

rm -rf `ls */public_html/outdated | grep -Ev ^exception1/ | grep -Ev ^exception2/ | grep -Ev ^exception3/`

, , , !

Note that this may not work if there are spaces in any of your directories, although the usernames in / home should not. A safe way to do this is to use find. (There is probably a way to throw an exception using find, but I can never remember.)

+1
source

Source: https://habr.com/ru/post/1779647/


All Articles