Tag Archives: delete

Delete Files Older Than x Days on Linux / Unix

The find utility on linux allows you to pass in a bunch of interesting arguments, including one to execute another command on each file. We’ll use this in order to figure out what files are older than a certain number of days, and then use the rm command to delete them.

Command Syntax

find /path/to/files* -mtime +5 -exec rm {} ;

Note that there are spaces between rm, {}, and ;

Explanation

  • The first argument is the path to the files. This can be a path, a directory, or a wildcard as in the example above. I would recommend using the full path, and make sure that you run the command without the exec rm to make sure you are getting the right results.
  • The second argument, -mtime, is used to specify the number of days old that the file is. If you enter +5, it will find files older than 5 days.
  • The third argument, -exec, allows you to pass in a command such as rm. The {} ; at the end is required to end the command.

AIX Delete problem

Today one user complained, that he is not able to delete files.

Most probably this was due to non printable charter in the file name.

# cd /oracle
# ls -ltr
total 7151632
drwxr-xr-x    2 oracle   oinstall        256 May 27 2008  lost+found
drwxr-xr-x    3 oracle   oinstall        256 May 28 2008  app
drwxr-xr-x    2 oracle   oinstall       4096 May 28 2008  TT_DB
-rwxrwxrwx    1 oracle   oinstall       3970 May 28 2008  .dtprofile
-rwxrwxrwx    1 oracle   oinstall         98 May 28 2008  .Xauthority
-rwxrwxrwx    1 oracle   oinstall        219 May 28 2008  .TTauthority
drwxrwxrwx   10 oracle   oinstall       4096 May 28 2008  .dt
-rwxrwxrwx    1 oracle   oinstall          3 May 30 2008  .wmrc
drwxr-xr-x    3 oracle   oinstall        256 May 31 2008  .java
-rw-------    1 oracle   oinstall          3 Nov 28 2008  dead.letter
-rw-r--r--    1 oracle   oinstall          0 Dec 05 2008  VALID
drwx--x--x    2 oracle   oinstall        256 Dec 05 2008  Mail
-rw-r--r--    1 oracle   oinstall         11 Dec 05 2008  .mh_profile
-rw-r--r--    1 oracle   oinstall          0 Feb 28 2009  smit.transaction
-rw-r--r--    1 oracle   oinstall          0 Feb 28 2009  smit.script
drwxr-xr-x    3 oracle   oinstall        256 May 26 2009  oradiag_oracle
-rw-r--r--    1 oracle   oinstall       2409 Feb 02 2011  back.sh
drwxr-----    3 ofm11g   dba             256 Jun 14 2011  application
drwx------    2 oracle   oinstall        256 Jun 17 2011  .ssh
-rw-r--r--    1 oracle   oinstall         54 Aug 02 2011  afiedt.buf
drwxrwxrwx    3 cash     staff           256 Nov 01 2011  dbdirectory
-rwxrwxr-x    1 oracle   oinstall        630 Nov 30 2011  .profile
drwxrwxrwx    2 bbk      staff           256 Jan 09 2012  CMS_FILE_LOCATION
-rw-r-----    1 oracle   oinstall       4817 Jun 14 13:04 sqlnet.log
-rw-------    1 oracle   oinstall     127931 Jul 31 19:19 nohup.out
-rw-r--r--    1 oracle   oinstall     127543 Jul 31 19:19 cmsdb12_27jul12_exp.lo                                                                             g
-rw-r--r--    1 oracle   oinstall 3661254656 Jul 31 19:19 cmsdb12_27jul12.dmp
-rw-r--r--    1 oracle   oinstall        231 Aug 01 10:19 cmsdb6.txt
-rw-r--r--    1 oracle   oinstall        231 Aug 01 13:45 cmsdb6-1.txt
-rw-r--r--    1 oracle   oinstall      11199 Aug 01 16:45 kill_inactive.sql
-rwxrwxrwx    1 oracle   oinstall        356 Aug 01 17:28 .vi_history
-rwxrwxrwx    1 oracle   oinstall       1462 Aug 01 17:36 .bash_history
-rw-------    1 oracle   oinstall      13300 Aug 03 10:51 .sh_history
# file cmsdb6-1.txt
file: cannot open cmsdb6-1.txt
# ls -li cmsdb6-1.txt
ls: 0653-341 The file cmsdb6-1.txt does not exist.
# rm cmsdb6-1.txt
rm: cmsdb6-1.txt: A file or directory in the path name does not exist.
# ls -lbi *cmsdb6*
   39 -rw-r--r--    1 oracle   oinstall        231 Aug 01 10:19 cmsdb6.txt
   42 -rw-r--r--    1 oracle   oinstall        231 Aug 01 13:45 cmsdb6.txt?33[D?33[D?33[D?33[D-1.txt
# rm -i cmsdb6.txt*
rm: Remove cmsdb6.txt? n
rm: Remove cmsdb6-1.txt? y
# ls -lbi *cmsdb6*
   39 -rw-r--r--    1 oracle   oinstall        231 Aug 01 10:19 cmsdb6.txt
# rxit
ksh: rxit:  not found.
You have mail in /usr/spool/mail/root
#

So if you face same kind of issue, Please try “ls -lbi” to display nonprintable characters and the inode (first column of the output) of the file(s).

Then you have different options to delete the file.

E.g. “rm -i p*” and reply only “y” for the file you want to delete.

Or use something like

“find . -inum <inum-of-your-file-in-question> -exec rm {} ;”

to identify the file by the inode number you got with the ls -b.