Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Delete directory older than n days Post 303046017 by jim mcnamara on Tuesday 21st of April 2020 05:04:46 PM
Old 04-21-2020
Based on this
Code:
drwxr-xr-x 2 dbadmin dbadmin 4096 Apr 21 09:44 2020-04-17
drwxr-xr-x 2 dbadmin dbadmin 4096 Apr 21 09:44 2020-04-16
drwxr-xr-x 2 dbadmin dbadmin 4096 Apr 21 09:44 2020-04-20
drwxr-xr-x 2 dbadmin dbadmin 4096 Apr 21 12:39 2020-04-14
drwxr-xr-x 2 dbadmin dbadmin 4096 Apr 21 12:41 2020-04-15
drwxr-xr-x 2 dbadmin dbadmin 4096 Apr 21 12:44 2020-04-18
drwxr-xr-x 2 dbadmin dbadmin 4096 Apr 21 13:18 2020-04-21

as output from ls and using the filename to generate a touch command to change the mtime on the directory:
Code:
ls -ld | while read f1 f2 f3 f4 f5 f6 f7 dname
do
   newtime=$( echo "$dname" | awk '{ gsub("-",""); printf("%s0000", $0) } ' )
    touch -t "$newtime" $dname
done

This only changes filetimes.

Last edited by jim mcnamara; 04-21-2020 at 06:48 PM..
This User Gave Thanks to jim mcnamara For This Post:
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

delete files older than 7 days

can anyone tell me how I would write a script in ksh on AIX that will delete files in a directory older than 7 days? (1 Reply)
Discussion started by: lesstjm
1 Replies

2. Shell Programming and Scripting

delete file older than N days

Hi, All, I'd like to delete files older than 1 day. I thought the following command find /your_directory -mtime +1-exec rm -f {} \; will do the work, but not so, it seems like it won't delete files unless it is 2 days old or older. the files between 1 day and 2 days old does not... (7 Replies)
Discussion started by: ericaworld
7 Replies

3. UNIX for Dummies Questions & Answers

Delete files older than 30 days

This is driving me crazy. How can I delete files in a specifc directory that are over 30 days old? Thanks in advance. (3 Replies)
Discussion started by: tlphillips
3 Replies

4. Solaris

Delete files older than 30 days

Hi all, I want to delete log files with extension .log which are older than 30 days. How to delete those files? Operating system -- Sun solaris 10 Your input is highly appreciated. Thanks in advance. Regards, Williams (2 Replies)
Discussion started by: William1482
2 Replies

5. Shell Programming and Scripting

To delete logs older than 30 days

I want to write a shell script that deletes all log files in a directory that are older than 30 days except for 3 files: I am using the following command: find /tmp/logs -name "*.log" -mtime +30 -exec rm -f {} \;But this command deletes all the log files. How can i modify this script that... (5 Replies)
Discussion started by: mmunir
5 Replies

6. Shell Programming and Scripting

Delete files older than X days.

Hi All, I am using below code to delete files older than 2 days. In case if there are no files, I should log an error saying no files to delete. Please let me know, How I can achive this. find /path/*.xml -mtime +2 Thanks and Regards Nagaraja. (3 Replies)
Discussion started by: Nagaraja Akkiva
3 Replies

7. Shell Programming and Scripting

Delete files older than 10 Days in a directory

Hi All I want to remove the files with name like data*.csv from the directory older than 10 days. If there is no files exists to remove older than 10 days, It should not do anything. Thanks Jo (9 Replies)
Discussion started by: rajeshjohney
9 Replies

8. AIX

Want to delete directory, subdirectories and all files which are older than 7 days

how do i remove sub directories of a directory and all files which are older than 7 days by a single command in AIX. pls help me. I am using command as #find /gpfs1/home/vinod/hpc/ -depth -type d -mtime +7 -exec rm -rf {} \; so i want to delete all sub directories and all files from the... (1 Reply)
Discussion started by: vinodkmpal
1 Replies

9. UNIX for Dummies Questions & Answers

Delete file older than three days

I am using SFTP to transmit files from the Mainframe to an UNIX server. I am looking for some kind of script that runs with SFTP to delete tranmitted files older than 3 days. Can this be done in a SFTP transmission batch job? (5 Replies)
Discussion started by: Steve Carlson
5 Replies
lsMode(3pm)						User Contributed Perl Documentation					       lsMode(3pm)

NAME
Stat::lsMode - format file modes like the "ls -l" command does SYNOPSIS
use Stat::lsMode; $mode = (stat $file)[2]; $permissions = format_mode($mode); # $permissions is now something like `drwxr-xr-x' $permissions = file_mode($file); # Same as above $permissions = format_perms(0644); # Produces just 'rw-r--r--' $permissions = format_perms(644); # This generates a warning message: # mode 644 is very surprising. Perhaps you meant 0644... Stat::lsMode->novice(0); # Disable warning messages DESCRIPTION
"Stat::lsMode" generates mode and permission strings that look like the ones generated by the Unix "ls -l" command. For example, a regular file that is readable by everyone and writable only by its owner has the mode string "-rw-r--r--". "Stat::lsMode" will either examine the file and produce the right mode string for you, or you can pass it the mode that you get back from Perl's "stat" call. "format_mode" Given a mode number (such as the third element of the list returned by "stat"), return the appopriate ten-character mode string as it would have been generated by "ls -l". For example, consider a directory that is readable and searchable by everyone, and also writable by its owner. Such a directory will have mode 040755. When passed this value, "format_mode" will return the string "drwxr-xr-x". If "format_mode" is passed a permission number like 0755, it will return a nine-character string insted, with no leading character to say what the file type is. For example, "format_mode(0755)" will return just "rwxr-xr-x", without the leading "d". "file_mode" Given a filename, do "lstat" on the file to determine the mode, and return the mode, formatted as above. Novice Operation Mode A common mistake when dealing with permission modes is to use 644 where you meant to use 0644. Every permission has a numeric representation, but the representation only makes sense when you write the number in octal. The decimal number 644 corresponds to a permission setting, but not the one you think. If you write it in octal you get 01204, which corresponds to the unlikely permissions "-w----r-T", not to "rw-r--r--". The appearance of the bizarre permission "-w----r-T" in a program is almost a sure sign that someone used 644 when they meant to use 0644. By default, this module will detect the use of such unlikely permissions and issue a warning if you try to format them. To disable these warnings, use Stat::lsMode->novice(0); # disable novice mode Stat::lsMode->novice(1); # enable novice mode again The surprising permissions that are diagnosed by this mode are: 111 => --xr-xrwx 400 => rw--w---- 440 => rw-rwx--- 444 => rw-rwxr-- 551 => ---r--rwt 600 => --x-wx--T 640 => -w------T 644 => -w----r-T 660 => -w--w-r-T 664 => -w--wx--T 666 => -w--wx-wT 700 => -w-rwxr-T 711 => -wx---rwt 750 => -wxr-xrwT 751 => -wxr-xrwt 751 => -wxr-xrwt 755 => -wxrw--wt 770 => r------wT 771 => r------wt 775 => r-----rwt 777 => r----x--t Of these, only 400 is remotely plausible. BUGS
As far as I know, the precise definition of the mode bits is portable between varieties of Unix. The module should, however, examine "stat.h" or use some other method to find out if there are any local variations, because Unix being Unix, someone somewhere probably does it differently. Maybe it "file_mode" should have an option that says that if the file is a symlink, to format the mode of the pointed to file instead of the mode of the link itself, the way "ls -Ll" does. SEE ALSO
o "http://www.plover.com/~mjd/perl/lsMode/". o ls o chmod o stat AUTHOR
Mark-Jason Dominus ("mjd-perl-lsmode@plover.com"). perl v5.10.1 1998-04-20 lsMode(3pm)
All times are GMT -4. The time now is 05:44 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy