Skip to content

Instantly share code, notes, and snippets.

@SumitDasSAPBasis
Last active May 4, 2022 05:57
Show Gist options
  • Save SumitDasSAPBasis/d12977a6f693bf935f03e73111944775 to your computer and use it in GitHub Desktop.
Save SumitDasSAPBasis/d12977a6f693bf935f03e73111944775 to your computer and use it in GitHub Desktop.
Cleanup Files based on Year Month Date

Script to delete old files lesser than a particular day

This one liner command - deletes files in a directory tree that are less than 5 days old, and asks if it is okay to delete each file:

Code Snippet #1:

find /path/to/directory -mtime -5  -ok  rm -f {} \;

Explanation:

  • mtime- modification time older than 5days in this case
  • -f – files to be deleted

Code Snippet #2:


delcnt=0
for files in `find /path/to/directory -mtime -5 -print`
do
   echo "Deleting file $file"
   /bin/rm $file
   delcnt=$(($delcnt + 1))
done

echo "deleted $delcnt files"



Code Snippet #3:

for i in `ls -l |grep "Aug"|awk '{print $9}'`
do
echo $i
rm $i 
done

Code Snippet #4:

for i in `ls -l | grep " 2007 "| awk '{print $9}' `
do
echo "Removing...." >> bb
echo `ll $i` >> bb
rm $i 
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment