How to compare the dates in shell script

Hi

How to compare created or modified date of two files

help needed

thanks
Vajiramani :slight_smile:

what exactly are you trying to achieve here

you can check this way

-rw-rw-r--    1 kanth    kanth           0 Feb 24 11:33 created_file
-rw-rw-r--    1 kanth    kanth           0 Feb 24 11:33 modified_file

suppose you have two file as above...

take 6,7 and 8th column and compare it by using "if" conditions

create=`ls -lrt created_file | awk '{print $6$7$8}'` 
modify=`ls -lrt modified_file |awk '{print $6$7$8}'`

U can use find command .
it has various options
-mtime 3 --to find files modified exactly 3days ago
-mtime +3 --to find files modified more than 3days
-mtime -3 --to find files modified within 3days

similarly
u have
-atime,-ctime and -newer etc

Thank u its help full.

Actually what i want to trying to do is getting some parameters as command line arguments while running a shell script and use it. Then with this i need to delete files from a directory if its exceeding the count 7.

The above command will delete wat ever the specified date interval. Its also in away good but i need exactly 7 files in the directory even if i don't generate one file for a day.

Now i need how to get command line parameters to be used int he script

Thanks again

scpt
###########
echo $1  # Gives the first argument
echo $2  # Gives the second argument
echo $3  # Gives the third argument
###########

Call the scpt with three arguments

script arg1 arg2 arg3

If you pass more than 9 arguments, then use brace

echo ${10}
echo ${11}

If your string argument contain spaces then use double quotes

scpt "hi buddy" arg2 arg3

Hi dears

its helpful

Thank you

ls -lt

is a long listing of directory contents sorted by mod time (and a "total" at the top)

ls -lt | awk 'NR>1'

gets rid of that header line

ls -lt | awk 'NR>1 {print $9}'

displays only the ninth field(filename)

ls -lt | awk 'NR>1 {if (substr($1,1,1) == "-" ) print $9}'

displays name if the first field (like -rw-rw-r--) starts with hyphen (indicates an ordinary file)

ls -lt | awk 'NR>1 {if (substr($1,1,1) == "-" && n++ > 6) print $9}'

prints a list of file names but omits the first 7 (first = most recent)

ls -lt | awk 'NR>1 {if (substr($1,1,1) == "-" && n++ > 6) print $9}' | xargs rm

does exactly what you want

Thank you

I have to try with this one and test

thanks again :slight_smile:
Vajiramani

hi Perderabo

Its fantastic, i achieved this by writing ten line what you did it in one line.

Thank you

Regards
Vajiramani