Computing.Net > Forums > Unix > file removing

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

file removing

Reply to Message Icon

Name: Cristian Popa
Date: October 13, 2001 at 06:34:25 Pacific
Comment:

Which are the parameters for rm command, in order to delete a large number of files, with different names but with the same date?



Sponsored Link
Ads by Google

Response Number 1
Name: solaris newbie
Date: October 13, 2001 at 21:36:59 Pacific
Reply:

i just know how to do that in perl. rm has no this kind of parameter on that.


0

Response Number 2
Name: James Boothe
Date: October 15, 2001 at 10:20:33 Pacific
Reply:

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


0

Response Number 3
Name: Mario
Date: October 22, 2001 at 03:26:47 Pacific
Reply:

Just along the same lines:

How do you look for users that havnt been logged on in the last say for 14 days ??

Mario


0

Response Number 4
Name: James Boothe
Date: October 23, 2001 at 15:35:50 Pacific
Reply:

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 jboothe

If the history spans end of year, since year is not included, the output will be sorted Jan-Feb-Mar-Oct-Nov-Dec.

James


0

Response Number 5
Name: dc
Date: November 30, 2001 at 16:51:08 Pacific
Reply:

hi


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: file removing

Dir/file remove with Date func? www.computing.net/answers/unix/dirfile-remove-with-date-func/4778.html

Deleting old files www.computing.net/answers/unix/deleting-old-files/4621.html

cat: write error: Bad address www.computing.net/answers/unix/cat-write-error-bad-address/3988.html