Skip to content

Instantly share code, notes, and snippets.

@yuriy-yarvinen
Last active August 17, 2022 21:31
Show Gist options
  • Save yuriy-yarvinen/41a1c75eaba8068754206277e2c72d6e to your computer and use it in GitHub Desktop.
Save yuriy-yarvinen/41a1c75eaba8068754206277e2c72d6e to your computer and use it in GitHub Desktop.
bash
start of file
#!/bin/bash
variable from console
countdirectories=$(ls | wc -l);
argument from console
$1
check variable
if [ $1 ]
#!/bin/bash
date=$(date +%Y-%m-%d)
yesterday=$(date -d "1 day ago" +"%Y-%m-%d")
day_before_yesterday=$(date -d "2 day ago" +"%Y-%m-%d")
if [ $1 ]; then
if cd $1; then
count_directories=$(ls -d * | wc -l)
if [ $count_directories -gt 0 ]; then
for dir in $(ls -d *); do
cd $1/$dir
for dir_inner in $(ls -d *); do
cd $1/$dir/$dir_inner
count_inner_directories=$(ls -d * | wc -l)
if [ $count_inner_directories -gt 3 ]; then
check=$(find $1/$dir/$dir_inner/ -type d -name $date)
check2=$(find $1/$dir/$dir_inner/ -type d -name $yesterday)
check3=$(find $1/$dir/$dir_inner/ -type d -name $day_before_yesterday)
if [ $check ] && [ $check2 ] && [ $check3 ]; then
for backup_dir in $(ls -d *); do
if [ $backup_dir != $date ] && [ $backup_dir != $yesterday ] && [ $backup_dir != $day_before_yesterday ]; then
rm -r $1/$dir/$dir_inner/$backup_dir;
fi
done
else
echo 'no';
fi
else
echo 'Error'
fi
done
done
else
echo "No directories"
fi
else
echo "no directory"
fi
else
echo "Error, need to pass path to directory"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment