Operating System - HP-UX
1831724 Members
3172 Online
110029 Solutions
New Discussion

Re: Delete files and subfolders based on mtime recursively

 

Delete files and subfolders based on mtime recursively

I'm trying to delete files as below -

files with mtime >=1 along with the folders that contain these files.

sample files -
home/tmp1/100/a (mtime is > 1)
home/tmp1/200/b (mtime is > 1 )

I thought of doing with clean.ksh as below -

-----clean.ksh----
var_path=/home
cd $var_path
for j in `find . -type d -mtime +1`
do
find $var_path/$j -type f -mtime +1 -exec rm -i {} \;
rmdir $j
done
---------
OS used - HP-UX B.11.23

please review and advise.

satya
10 REPLIES 10
Kapil Jha
Honored Contributor

Re: Delete files and subfolders based on mtime recursively

Yups thats correct.

Thanks,
Kapil+
I am in this small bowl, I wane see the real world......

Re: Delete files and subfolders based on mtime recursively

Forgot to mention - Please let me know if there is a better way to do this!
Dennis Handly
Acclaimed Contributor

Re: Delete files and subdirectories based on mtime recursively

>I thought of doing with clean.ksh as below

This is going to remove files older than 2 days only if there were no changes in the directory in 2 days. I'm not sure why you care about the directory modification times? This is going to be random since subdirectories will be visited based on their possibly older parents.
cd $var_path
for j in $(find . -type d -mtime +1); do
find $j -type f -mtime +1 -exec rm -i {} +
rmdir $j
done

You won't be able to do that rmdir all of the time if there are files that are newer in the directory.

No real reason to use $var_path/$j, just $j is fine.
Using "rm -i" is already going to make it very slow and your finger tired. :-)

James R. Ferguson
Acclaimed Contributor

Re: Delete files and subfolders based on mtime recursively

Hi Satya:

Your logic may miss files that have been modified yet match the time criteria.

This will happen if a file is modified "in-place" by, for example, with 'vi' or simple shell redirection. Cases of update like this, where no file is added or removed from the directory will leave the directory 'mtime' unchanged and potentially cause you to fail to find modified files within.

You might want to consider using '-depth' to cause 'find' to decend subdirectories and process their contents before acting on the directory itself.

It isn't clear if you want to consider a directory as modified or not if the action of your script was the sole cause of the directory's update since its last execution.

Regards!

...JRF...

Re: Delete files and subfolders based on mtime recursively

Hi JRF,
I understand what you mean. My requirement is to -

delete folders that are (created 1+ days ago). It seems that the folders also have files inside so I need to delete these before proceeding to delete the folder itself. Is it possible to combine -
depth
ctime

with -exec rm and -exec rmdir to delete files as well as the folder in the same find ?

Regards,
Satya

James R. Ferguson
Acclaimed Contributor

Re: Delete files and subfolders based on mtime recursively

Hi Satya:

See if this meets your needs:

# find ${var_path} -depth -mtime +1 -exec /home/satya/remover {} \;

...where:

# cat /home/satya/remover
#!/usr/bin/sh
NODE=$1
if [ -f "${NODE}" ]; then
rm ${NODE}
rmdir $(dirname ${NODE})
exit
fi
if [ -d "${NODE}" ]; then
rmdir rmdir ${NODE}
fi
exit

...NOW NOTICE CAREFULLY, that I deliberately used the '\;' terminator with the 'find -exec' so that one and only one process of the script will be spawned for every file or directory found. We could "smarten" the 'myremover' to loop through a list of arguments, but I'll leave that to you until you see if this suggestion works. If it does, and you want to improve performance, change the 'myremover' script and change the 'find -exec' delimiter to '+'.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Delete files and subdirectories based on mtime recursively

>JRF: rmdir $(dirname ${NODE})

This may fail so you may want to redirect the errors:
rmdir $(dirname ${NODE}) 2> /dev/null

(And there was a typo in the other rmdir.)

James R. Ferguson
Acclaimed Contributor

Re: Delete files and subfolders based on mtime recursively

HI (again) Satya:

> Dennis: This may fail so you may want to redirect the errors

Yes, I would agree. I had left any error reporting (i.e. not an empty directory) inplace for testing, but agree that you probably want to safely ignore these messages.

Given this, and my typographical error that Dennis found :-}}

# cat ./remover
#!/usr/bin/sh
NODE=$1
if [ -f "${NODE}" ]; then
rm ${NODE}
rmdir $(dirname ${NODE}) 2> /dev/null
exit
fi
if [ -d "${NODE}" ]; then
rmdir ${NODE} 2> /dev/null
fi
exit

...

Regards!

...JRF...

Re: Delete files and subfolders based on mtime recursively

Hi JRF

I understand you are doing this -

Begin
if [file] then
delete file
delete dir of file
end if
if [dir] then
delete dir
end if
end

but to me it seems the below commands are commented out.

# find ${var_path} -depth -mtime +1 -exec /home/satya/remover {} \;
# cat ./remover
#!/usr/bin/sh


So its not clear how the target files/dirs are identified. Please help me understand this :(

Satya
James R. Ferguson
Acclaimed Contributor

Re: Delete files and subfolders based on mtime recursively

Hi (again) Satya:

> but to me it seems the below commands are commented out.

# find ${var_path} -depth -mtime +1 -exec /home/satya/remover {} \;
# cat ./remover
#!/usr/bin/sh

No, I am using the root prompt and what would be run at that commandline prompt.

The './remover' file is a shell script that begins with a declaration of the interpreter to use, namely '/usr/bin/sh' -- the standard HP-UX Posix shell. You could substitute '#!/usr/bin/ksh' for '#!/usr/bin/sh' if you wish. Your orignal 'clean.ksh' script as posted should have the "she-bang" interpreter line as the first line in its file, too.

Regards!

...JRF...