ls command help

I was givin the task to clear up space in one of our directories. I was wondering if their was an ls command that I can search for a certain date (i.e. all files created in 2005) to display those files and then use a wildcard to delete all files created in a certain year. If so, this would make my life alot easier. I appreciate any help.

Thanks

Take a look at the find command.

dear

you can use the find command as below to find the files that have been modified more than two days ago and delete them:
find /ect -type f -mtime +2 -exec rm -fr {} \;
for more info see the man find

regards;
The Engineer
IBM Certified Specialist

Thanks for the head start. Please any other help will be greatly appreciated.

Ooopsie, Thank you! :slight_smile:

I see that the help you guys have givin me so far are a start. Is their not a way you can search by the YEAR instead of "how many days" a file was lost modified or accessed? Let me know either way please.

Thanks again

ls -l | awk '$8==2005{print $NF}' | xargs ls -l

will give you a long listing of all files that have 2005 in the date.

This will not work for files less than 6 months old (it shows the time, not the year in all versions of UNIX I have worked in).

Of course, you could get really fancy and use the system function in awk, but this is slightly faster.

awk Thanks sooo much.

My mananger wants to know if their is any way feasible to search for a certain date, (i.e. March 22 2004)?

Or list all files between May 2, 2005 and June 3, 2006.

Stuff like that.

Let me know.

Thanks again awk

The following commands display files between a certain date range:

## Creates a file with date of 01/Jan/2006 00:00
touch -m 0101000006 $$START_FILE

## Creates a file with date of 01/Jan/2007 00:00
touch -m 0101000007 $$END_FILE

## List files in date order and display only those in between
## $$START_FILE and $$END_FILE
ls -lt | sed -n "/$$END_FILE/,/$$START_FILE/p"

## Remove both temporary files.
rm -f $$START_FILE
rm -f $$END_FILE

How would I now go about deleting those selected files from that certain date range?

Meaning I would like to delete all files from 2006.

Thanks again

ndoggy020,
Did you run the code I wrote above?
Change the "ls" command from "ls -lt" to "ls -1t" and run it.

I did, and it indeed displayed the files within the date range I selected. Im just wondering what command I would use to delete those files?

As mentioned is the previous post,create to files with modification time as that of the date ranges

touch -mt 200505020000 /tmp/May-2-2005
touch -mt 200606030909 /tmp/June-6-2006

And try,

find <your-dir> -name '*' \( -newer /tmp/May-2-2005 -a ! -newer /tmp/June-6-2006 \) -ls

Hope this helps.

Thanks
Nagarajan Ganesan

Try this -- the "FINAL_rm_FILE" will have all the "rm" commands for each file:

ls -1t | sed -n "/$$END_FILE/,/$$START_FILE/p" > $$rm_FILE
sed 's/^/rm -f /' $$rm_FILE > FINAL_rm_FILE

This will help you to remove all the files between the two date ranges ie ( all files between May-2-2005 & June-6-2005)

find <your-dir> -name '*' \( -newer /tmp/May-2-2005 -a ! -newer /tmp/June-6-2006 \) -ok rm {} \;

Please run the commands mentioned in the previous post.Once you confirm the results,try running the above command,here "ok" used inplace od "exec" will wait for you confirmation before proceeding with the rm

Thanks
Nagarajan Ganesan.

Thanks ennstate for your help but I am going to go Shell Life's route!! I really appreciate your help though. :slight_smile:

i need the log files for the files which are 2 days older before deleting the files

Just to be very clear i need to generate a log file in which the file name should be there before deleting the files which are two days older .. can anyone can help me on this .. Thanks in advance !!

Comment the second of these two lines in the option you already chose to use.
The output file will contain the list.

ls -1t | sed -n "/$$END_FILE/,/$$START_FILE/p" > $$rm_FILE
sed 's/^/rm -f /' $$rm_FILE > FINAL_rm_FILE

i am using this command find * -type f -atime +30 -exec gunzip {} \; for unzipping the files which are 30 days older .. but how can i get the log for which files the command has unzipped ..

This should be in a single line ..

Thanks!!