Source path:
/var/log/
Here I have 4 folders named ad1nrld,ad2nrld,icp1rmnrl,icp2rmnrl
I can move all the files to the other destination named /home/spsy/logs_bkp
. But I want to keep latest 5 files in each folders (ad1nrld,ad2nrld,icp1rmnrl,icp2rmnrl) of this /var/log
path and rest will be moved to /home/spsy/logs_bkp
path.
At source path,
/var/log/ad1nrld
- Only latest 5 files will be present and rest will be moved to /home/spsy/logs_bkp/ad1nrld
path
I have tried the below code for moving the files and they are successfully moved. But I am unable to keep latest 5 files in the respective folders.
for i in `ls -1rt | egrep 'nrld|mnrl'`
do
cd $i
mv *log /home/spsy/logs_bkp/$i
echo "files moved for &i"
cd ..
done
The below part is not working while I am adding the code to keep latest 5 files.
for i in `ls -1rt | egrep 'nrld|mnrl'`
do
cd $i
count_files=`ls -lrt | wc -l`
if [ $count_files -gt 5 ];
then
tomove=$(($count_files-5))
for part in `ls -1rt`
do
if [ $tomove -gt 0 ]
then
mv $part /home/spsy/logs_bkp/$i
tomove=$(($tomove-1))
echo "files moved for &i"
cd ..
fi
done
fi
done
/home/spsy/logs_bkp
, how do you deal with duplicate file names? – pLumo Feb 24 '22 at 07:21logrotate
to handle the logs? – fuzzydrawrings Feb 24 '22 at 17:15