Hi to everyone here,
I'm a new user and relatively-new linuxer.
I'm trying to write a script that checks if every file from a directory is present in a given list and if not, delete it.
should be simple. But I think I've done half the work only:
this is to create the reference list:
for c in `ls ~/cache`;
do for s in `ls ~/sel`;
do case $c in
$s) echo $s >> ~/installed;;
esac
done
done
Now I'm not able to check again every file in the "cache" with the "installed" list and eventually delete them.
# create a catalog list of files and directories you want to keep
# create a listing of files and directories in a given path
ls -1 > ../temp_list # place list of dirs and files out of current working directory
cat ../catalog >> ../temp_list # append dirs and files in the catalog list to the temp buffer file
sort ../temp_list | uniq -u | xargs rm -r # delete anything is not in catalog
Thank you for the reply!
I'm not really sure of what you mean with "catalog", but the main thing is that I have to figure out what "uniq" and "xargs" commands do.
In the meantime, in my way, I accomplished the task; maybe it's not so elegant, but at least it works!
#!/bin/bash
tot1=`ls /var/cache/apt/archives | grep -c deb`
echo apt-get autoclean...
sleep 1
sudo apt-get autoclean
echo installed packages back-up...
sleep 1
cd /var/cache/apt/archives
sudo mkdir cache-backup
for c in `ls | sed 's/_.*//'`;
do for s in `dpkg --get-selections`;
do case $c in
$s) echo backing-up $c && sudo cp -r $c*deb --target-directory=cache-backup;
esac
done
done
sleep 1
tot2=`ls /var/cache/apt/archives | grep -c deb`
bup=`ls /var/cache/apt/archives/cache-backup | grep -c deb`
echo ---------------------------
echo "total packages in cache were: $tot1"
echo "packages after auto-cleaning: $tot2"
echo "installed packages to retain in cache: $bup"
echo ---------------------------
sleep 2
echo removing entire cache...
sudo rm *.deb
sleep 2
echo restoring backed-up packages...
sudo cp /var/cache/apt/archives/cache-backup/*.deb /var/cache/apt/archives
sudo rm -r /var/cache/apt/archives/cache-backup
sleep 2
echo closing...
sleep 2
exit
...all stuffed with a lot of useless echoes, since I know exactly what the script does: removing from the cache in "/var/cache/apt/archives" all not installed packages.