iSeries Delete IFS files based on Last Access Date or Modified Date

The easiest way is to use QShell's "find" command. You can go to a QShell command line by typing STRQSH.

Delete based on Last Access Date

find /directory_path -type f -atime +5 -print | xargs rm 

Delete based on Last Modified Date

find /directory_path -type f -mtime +5 -print | xargs rm 

How does it work? Explanation for above:

  • find /directory_path -type f tries to find files within that directory.
  • -atime +5 tells the criteria to find based on last access date of 5 or more. You ca change the +5 to your own number of days.
  • -mtime is same as -atime but it tells the find command to find based on last modified date.
  • The unix pipe | passes the results to the next function.
  • Where xargs makes them as input arguments
  • rm command takes those arguments as input and deleted the files.

Warning ! Before you take the previous approach you have to make sure you file names don't have BLANKS in it

If they have blanks then the above approach is not going to work as it takes all the file names as arguments to the rm command. So a space in between the file name will make the rm command interpret it as separate arguments. Here is another way to do the same but it has its limitations too. Its running the rm command for each file so its not efficient and will take a lot of resources and time.
find /directory_path -type f -atime +5 -exec /usr/bin/rm {} \;
find /directory_path -type f -mtime +5 -exec /usr/bin/rm {} \;

So here a sample CLP program to Delete IFS files based on Last Access Date or Modified Date. Thanks to Scott Klement! You can make your own RPGLE program based on this if you want.

Sample CLP program to Delete IFS files based on Last Access Date or Modified Date

/*  Delete IFS files based on Last Access or Modified Date        + 
    USE WITH CAUTION : No Guarantee provided !                    + 
                                                                  + 
                                                                  + 
    TO COMPILE:                                                   + 
                                                                  + 
        CRTPF FILE(QTEMP/MYFILE) RCDLEN(1000)                     + 
        CRTCLPGM PGM(DELIFS) SRCFILE(Your_lib/Source)             + 
                                                                  + 
    TO RUN:                                                       + 
                                                                  + 
        CALL DELIFS PARM('/Dir1/Dir2/..' 'A' '5')                 + 
             Delete Files in IFS based on Last Access Date        + 
        Note:The access time of directories in path is changed by + 
             find command itself.                                 + 
                                                                  + 
        CALL DELIFS PARM('/Dir1/Dir2/..' 'M' '5')                 + 
             Delete Files in IFS based on Last Modified Date      + 
                                                                  + 
    PARAMETERS:                                                   + 
        &DIR  = Directory path                                    + 
                Do not just put / as it means the root of the IFS + 
        &TYPE = A=Access Date M=Modfied Date                      + 
        &DAYS = No. of Days                                       + 
                                                                 */ 
                                                                    
             PGM        PARM(&DIR &TYPE &DAYS)                      
                                                                    
             DCL        VAR(&DIR) TYPE(*CHAR) LEN(32)               
                        /* You can increase the length from 32 to  +
                        something else but remember if the lenngth +
                        is more than 32 then you can't call from   +
                        command line OR make a COMMAND            */
                                                                    
             DCL        VAR(&TYPE) TYPE(*CHAR) LEN(1)               
             DCL        VAR(&DAYS) TYPE(*CHAR) LEN(4)               
                                                                    
             DCL        VAR(&CMD) TYPE(*CHAR) LEN(250)              
             DCL        VAR(&EOF) TYPE(*LGL) VALUE('0')             
                                                                    
             DCLF       FILE(QTEMP/MYFILE)                          
                        /* This will have the list of files */      
                                                                    
             DLTF       FILE(QTEMP/MYFILE)                          
             MONMSG     MSGID(CPF0000)                              
                                                                    
             CRTPF      FILE(QTEMP/MYFILE) RCDLEN(1000)           
                                                                  
                                                                  
            /* Run Qshell "find" command to load the fileNames +  
               based on your criteria into the MYFILE in QTEMP */ 
                                                                  
             OVRDBF     FILE(STDOUT) TOFILE(QTEMP/MYFILE)         
                                                                  
             IF         COND(&TYPE *EQ 'A') THEN(DO)              
             CHGVAR     VAR(&CMD) VALUE('find' *BCAT &DIR *BCAT + 
                          '-atime +' *CAT &DAYS *BCAT '-type f')  
             ENDDO                                                
             IF         COND(&TYPE *EQ 'M') THEN(DO)              
             CHGVAR     VAR(&CMD) VALUE('find' *BCAT &DIR *BCAT + 
                          '-mtime +' *CAT &DAYS *BCAT '-type f')  
             ENDDO                                                
                                                                  
             STRQSH     CMD(&CMD)                                 
             DLTOVR     FILE(STDOUT)                              
                                                                  
                                                                  
            /* Loop thru each IFS file and Delete */              
                                                                 
 LOOP:       RCVF                                                         
             MONMSG     MSGID(CPF0864) EXEC(CHGVAR VAR(&EOF) +            
                          VALUE('1'))                                     
             IF         COND(&EOF *EQ '0') THEN(DO)                       
                        QSYS/DEL   OBJLNK(&MYFILE)                        
             GOTO       CMDLBL(LOOP)                                      
             ENDDO                                                        
                                                                          
            /* You can comment this LINE to get the deleted files LIST */ 
             DLTF       FILE(QTEMP/MYFILE)                                
                                                                          
             ENDPGM                                                  

Find Command Examples


From current directory
find . -name 'my*'

Files only
find . -name "my*" -type f

Print extended file information
find . -name "my*" -type f -ls

Search all directories
find / -type f -name "myfile" -print

Search all but one directory subtree
find / -path excluded_path -prune -o -type f -name myfile -print

Specify a directory
find /home/weedly -name "myfile" -type f -print

Search several directories
find local /tmp -name mydir -type d -print

Find any one of differently named files
find . \( -name "*jsp" -o -name "*java" \) -type f -ls

Execute an action
find /var/ftp/mp3 -name "*.mp3" -type f -exec chmod 644 {} \;

Search for a string
This command will search for a string in all files from the /tmp directory and below:
find /tmp -exec grep "search string" '{}' /dev/null \; -print

The /dev/null argument is used to show the name of the file before the text that is found. Without it, only the text found is printed. An equivalent mechanism is to use the "-H" or "--with-filename" option to grep:
find /tmp -exec grep -H "search string" '{}' \; -print

Search for all files owned by a user
find . -user <userid>

Search in case insensitive mode
find . -iname "MyFile*"

Search files by size
Example of searching files with size between 100 kilobytes and 500 kilobytes.
find . -size +100k -a -size -500k

Search files by name and size
find /usr/src -not \( -name "*,v" -o -name ".*,v" \) '{}' \; -print

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.