There are multiple directories under "home" directory. dir1 dir2 dir3 dir4 dir5
I want bash script to recursively archive dir1-2-3 as dir1.zip, dir2.zip & dir3.zip but exclude dir4 and dir5.
Is it doable ?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
No, Linux removed that feature with the latest release.
Only joking, of course it's doable.
for dir in dir1 dir2 dir3; do zip -r "$dir" "$dir" done
If the directories truly have the same prefix:
for dir in dir[1-3]; do zip -r "$dir" "$dir" done
This assumes that the loop is performed in the correct directory.
If you have many many directories and only want to exclude a few, use
bash with its
extglob shell option set:
shopt -s extglob for dir in dir!(4|5); do test -d "$dir" || continue zip -r "$dir" "$dir" done
dir!(4|5) will match any name starting with
dir and then continuing with anything not a
4 or a
5. The pattern could also have been written
I've also made sure, in the last loop, that any matched name that is not the name of a directory is skipped.