Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Which are the parameters for rm command, in order to delete a large number of files, with different names but with the same date?

Cristian,
The find command is typically used for that, being able to find a list of files or directories qualified by a large combination of qualifiers such as filename pattern, owner, file type, size, date, etc. It can print the qualifying entries or execute some command on each entry, such as rm. To list regular files with recent modify date (last several days) with the current directory as the starting point:find . -type f -mtime -5 -print
To find and remove files older than several days in the current directory tree:
find . -type f -mtime +5 -exec rm {} \;
List files in a narrow modify date-range:
find . -type f -mtime +5 -mtime -7 -print
The find command does not work on an exact calendar day, but rather multiples of 24-hr periods from this moment (my observation, quite informal). To zero in on an exact date, you could pipe an ls command into awk. For those lines it finds an exact hit, awk can execute a statement. In the example below, it does ls -l, but that could be changed to rm (with a space after it):
ls -l|awk '/Oct 9/ {system("ls -l " $NF)}'
There are major differences in the find and awk solutions shown here. The find command is recursive into subdirectories, whereas the ls|awk solution is not. Also, whereas the find command is truly looking at the modify date, note that awk, as coded above, is looking for "Oct 9" anywhere in the line, and as coded, files dated Oct 9 from a few days ago would qualify along with files dated Oct 9 from prior years. Would need additional coding to lock in on a year.
James

Just along the same lines:
How do you look for users that havnt been logged on in the last say for 14 days ??
Mario

A challenging question, Mario. Maybe a unix administrator knows an easy way. I just wrote the following script, works fine on HP-UX.
who /var/adm/wtmp | awk '
BEGIN {rc=getline < "/etc/passwd"
while (rc==1)
{name=substr($0,1,index($0,":")-1)
usr[name]="NUL 00 00:00"
rc=getline < "/etc/passwd"}
}{datetime=sprintf"%-3s %2d %-5s",$3,$4,$5
usr[$1]=datetime}END {for (i in usr) print usr[i],i}
' | sort -k1M# end of script
The who command displays chronological list of login history (roughly 6 months of history on my server). This output is piped into awk, which stores date-time for each user in an array. Newer entries overlay older entries for each user, so at the end, the array contains the latest entry for each user.
But at very start of awk, before starting to process the piped data, the BEGIN statement creates a NUL entry in the array for each defined user. This ensures that all users get into the report, even users who do not have entries in the login history.
Finally, the output of awk is piped into a sort. The M tells it that the sort key is an alphabetic month, and this gets a chronological sort instead of alpha sort (Apr-Aug-Dec).
And here is a snip of the final output:
NUL 00 00:00 lp
NUL 00 00:00 sched
NUL 00 00:00 uucp
NUL 00 00:00 www
Apr 3 08:34 ahuerta
Apr 10 09:34 lmartin
Jun 7 09:13 tmartin
Jun 18 10:05 scastle
Jul 5 14:18 applmgr
Oct 22 17:16 oratest
Oct 23 08:40 charding
Oct 23 13:29 skalidin
Oct 23 14:28 wt
Oct 23 14:52 jbootheIf the history spans end of year, since year is not included, the output will be sorted Jan-Feb-Mar-Oct-Nov-Dec.
James

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |