Commands to delete older files in Unix / Linux

1. Delete any files/folders older than 30 days from the current directory

find . -mtime +30 -exec rm {} \;

-exec : For each result found from the find command, perform the command listed next.
The {} part is where the find result gets substituted into from the previous part.

2. Delete only files (No folders) older than 30 days from the current directory

find . -type f -mtime +30 -exec rm {} \;

3. Delete only files older than 30 days from any directory

find <directory path to files> -type f -mtime +30 -exec rm {} \;
Ex : find /tmp -type f -mtime +30 -exec rm {} \;

4. Delete files with a fixed file name pattern or fixed extension and older than 30 days from any directory

find <directory path to files> -type f -mtime +30 -name '<file name pattern' -exec rm {} \;
Ex : find /tmp -type f -mtime +30 -name 'archive*.gz' -exec rm {} \;
        find /tmp -type f -mtime +30 -name '*' -exec rm {} \;

5. List files with a fixed file name pattern or fixed extension and older than 30 days from any directory.

find <directory path to files> -type f -mtime +30 -name '<file name pattern' -exec ls -l {} \;
Ex : find /tmp -type f -mtime +30 -name 'archive*.gz' -exec ls -l {} \;


6. Find and delete files modified in the last 30 minutes

find /tmp/ -type f -mmin 30 -exec rm {} \;
mtime = days
mmin = minutes

7. force delete temp files older then 30 days

find /tmp -mtime +30 -exec rm -f {} \;

8. move files older than 30 days to an archive folder – and preserve path strcuture

find /tmp -mtime +30 -exec mv -t {} /archive/directory/ \;
mv -t: ensure directory structure is preserved

9. Find and delete files older than 30 days and list the files to be deleted, before you perform the delete operation.

find /tmp -mtime +30 -name * -exec echo "Delete {} " \; -exec -rm -rf {} \;

No comments:

Post a Comment

ASM and Database does not auto start in Oracle restart environment (After Server start)

 If you are supporting Any Oracle restart environment and if you notice that your ASM instance and Database instance does not start automati...