I have a backup from a disk that contains data files and analysis. The dir structure and names are not really consistent. To save space, I would like to zip all the subdirs that only contain data (*.txt) and delete the original afterwards. There are several threads on zipping subdirs, but not on the conditions I have.To be more precise: I do not want to zip all *.txt files, but all directories that exclusively contain txt-files into a directory.zip and delete the original directory.
How to zip recursively all subdirectories that only contain text files
directory;recursive;zip
This script will archive then optionally remove all folders containing *.txt files and nothing else.folders=$(find . -type d -exec sh -c 'cd $1;[ $(ls *.txt 2>/dev/null) ] \          && [ -z $(ls -ad * | grep -v '\.txt$') ] && echo $1' sh {} \;)echo $folders | zip -r@ archive && echo $folders | while read folder; do  echo will remove $folder  # Uncomment next line for the folder to be removed  # rm -rf $folderdoneEdit: here is a solution that creates individual zip files:find . -depth -type d -exec sh -c 'cd $1 || exit[ $(ls ./*.txt 2>/dev/null) ] &&[ -z $(ls -ad ./* | grep -v '\.txt$') ] &&(  b=$(basename $1)  cd ..  zip -r $b.zip $b && rm -rf $b)' sh {} \;