- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Delete files and subfolders based on mtime rec...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2009 03:08 AM
01-22-2009 03:08 AM
Delete files and subfolders based on mtime recursively
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
- Tags:
- find
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2009 03:22 AM
01-22-2009 03:22 AM
Re: Delete files and subfolders based on mtime recursively
Thanks,
Kapil+
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2009 03:26 AM
01-22-2009 03:26 AM
Re: Delete files and subfolders based on mtime recursively
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2009 04:01 AM - edited 09-04-2011 01:45 PM
01-22-2009 04:01 AM - edited 09-04-2011 01:45 PM
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. :-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2009 05:30 AM
01-22-2009 05:30 AM
Re: Delete files and subfolders based on mtime recursively
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2009 04:10 AM
01-23-2009 04:10 AM
Re: Delete files and subfolders based on mtime recursively
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2009 07:14 AM
01-23-2009 07:14 AM
Re: Delete files and subfolders based on mtime recursively
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2009 02:26 PM - edited 09-04-2011 01:46 PM
01-23-2009 02:26 PM - edited 09-04-2011 01:46 PM
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.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-23-2009 02:42 PM
01-23-2009 02:42 PM
Re: Delete files and subfolders based on mtime recursively
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2009 09:22 AM
01-24-2009 09:22 AM
Re: Delete files and subfolders based on mtime recursively
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-24-2009 09:28 AM
01-24-2009 09:28 AM
Re: Delete files and subfolders based on mtime recursively
> 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...