View deleted files and delete old files using find command in linux

#lsof -l

//it will print all the running file and all the related files showing

#lsof -l | grep -i deleted

//it will shows all previously deleted files in entire machine

#lsof -l | grep -i delted | grep -i /home/boobalan/

//it will shows all recently deleted file in /home/boobalan directories

//find old files or find any files using find command

#find / -name boobalan
/home/boobalan

//it will find the word 'boobalan' from entire '/' directory

//find older file using older date count

# find . -mtime +100 -print

//here display all the file which are more than 100 days in current directory. (.) represent current directory

#find /home/boobalan/ -mtime +100 -print

//if they want to delete

#find /home/boobalan/ -mtime +100 -exec rm -f {} \;

#find /home/boobalan/ -mtime +100 -delete;

---------------------------------------------------------------------------

#!/bin/bash

path="/data/backuplog/"
timestamp=$(date +%Y%m%d_%H%M%S)  
filename=log_$timestamp.txt  
log=$path$filename
days=7

START_TIME=$(date +%s)

find $path -maxdepth 1 -name "*.txt"  -type f -mtime +$days  -print -delete >> $log

echo "Backup:: Script Start -- $(date +%Y%m%d_%H%M)" >> $log


... code for backup ...or any other operation .... >> $log


END_TIME=$(date +%s)

ELAPSED_TIME=$(( $END_TIME - $START_TIME ))


echo "Backup :: Script End -- $(date +%Y%m%d_%H%M)" >> $log
echo "Elapsed Time ::  $(date -d 00:00:$ELAPSED_TIME +%Hh:%Mm:%Ss) "  >> $log
---------------------------------------------------------
// this what i modified to my environment

#!/bin/bash
#deletefile.sh

path="/root/Downloads/"
timestamp=$(date +%Y%m%d_%H%M%S)
filename=log_$timestamp.txt
log=$path$filename
days=3

START_TIME=$(date +%s)

#find $path -mtime +$days -print -delete >> $log

echo "Backup:: Script Start -- $(date +%Y%m%d_%H%M)" >> $log

echo "... code for backup ...or any other operation ..." >> $log

find $path -mtime +$days -print -delete >> $log

END_TIME=$(date +%s)

ELAPSED_TIME=$(( $END_TIME - $START_TIME ))


echo "Backup :: Script End -- $(date +%Y%m%d_%H%M)" >> $log
echo "Elapsed Time ::  $(date -d 00:00:$ELAPSED_TIME +%Hh:%Mm:%Ss) "  >> $log



-------------------------------------------------------
The code adds a few things.

    log files named with a timestamp
    log folder specified
    find looks for *.txt files only in the log folder
    type f ensures you only deletes files
    maxdepth 1 ensures you dont enter subfolders
    log files older than 7 days are deleted ( assuming this is for a backup log)
    notes the start / end time
    calculates the elapsed time for the backup operation...

Note: to test the code, just use -print instead of -print -delete. But do check your path carefully though.

Note: Do ensure your server time is set correctly via date - setup timezone/ntp correctly . Additionally check file times with 'stat filename'

Note: mtime can be replaced with mmin for better control as mtime discards all fractions (older than 2 days (+2 days) actually means 3 days ) when it deals with getting the timestamps of files in the context of days


------------------------------------------------------------------------------------
//list all the file /directory which have D on its name
[root@laptop ~]# ls -lah *D*
Desktop:
total 4.0K
drwxr-xr-x.  3 root root   29 Jun 12 22:22 .
dr-xr-x---. 21 root root 4.0K Jun 26 14:15 ..
drwx------.  3 root root   29 Jun 12 22:22 Old Firefox Data

Documents:
total 4.0K
drwxr-xr-x.  2 root root    6 May  6  2019 .
dr-xr-x---. 21 root root 4.0K Jun 26 14:15 ..

Downloads:
total 2.0M
drwxr-xr-x.  3 root root   40 Jun 13 13:18 .
dr-xr-x---. 21 root root 4.0K Jun 26 14:15 ..
drwxr-xr-x.  4 root root   34 Jun 13 13:18 original
-rw-r--r--.  1 root root 2.0M Jun 13 13:13 original.zip
------------------------------------------------------------------------------------------
//type is a command to find which is predefined or build-in arguments in linux

[root@laptop ~]# type man
man is hashed (/usr/bin/man)


[root@laptop ~]# type -p man
/usr/bin/man

[root@laptop ~]# type -a pwd
pwd is a shell builtin
pwd is /usr/bin/pwd
pwd is /bin/pwd
------------------------------------------------------------------------------

Post a Comment

0 Comments