How to sort and delete directory going back for years?
SUSE / openSUSEThis Forum is for the discussion of Suse Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Then it would be better to log the successful deletion commands than to keep a file listing directories that the script was intended to delete but may not have been able to.
It works but it comes to big list of directories to be deleted did not work i got the following error:
-bash: /bin/rm: Argument list too long
Any idea won what went wrong?
The argument list was too long There's a fixed amount of space that Linux provides for each process to store its environment variables and argument list (essentially the command line) and your command exceeded it.
Two solutions come to mind, both using find. The first and simplest is to remove the directories one by one:
You would need to change <your starting directory> to, er, your starting directory (without the < >) and, when you are confident it will only remove the directories you want, remove the echo.
The second and faster running solution is to pipe the find output into the xargs command but given this is a one-off there's little benefit in the extra complexity.
Neither your solution nor the above will log the names of the deleted directories. Do you still want to do that?
I found the following post: how can we modify it to work with year not the day and the month?
if you want to delete files on XXX date you might want to use the date command with some variables like:
--------------------------------------------------
code:
#!/bin/sh
# i want may 24th here
thedateis="`date -v24d -v5m|awk '{print $2" "$3}'`"
for i in `ls -al |grep "$thedateis"|awk '{print $9}'` ; do ls -ald $i ; done
---------------------------------------------------
change ls -ald with rm -rf to remove files and directories in the current working directory that is dates to 24th may u can play with -v24d for days and -v5m for months
---------------------------------------------------------------------------
thank you for your suggestion, but did not work with no error,
what I'm after is to delete all directories with year 2007 stamp..
the -name will sort for date?
Sorry -- I missed that; I wrongly assumed that the directories had 2007 in their names.
Two possible solutions come to mind. The first is to use your original solution with xargs to workaround the "argument list too long" problem (not tested, here with echo for testing):
but that is dangerous because it will also: a) remove files with 2007 in their name b) not work if the files have whitespace in their path (each whitespace-separated part of the path will be treated as a separate file) c) not work if the files have unprintable characters in their name (ls does not print them). In the case of b and c this could mean deleting files which do exist but you do not want to delete. Inconveniently it will also a) attempt to remove directories (which cannot be done with rm, only with rm -fr) and, if you do use rm -fr, it will attempt to remove files which have already been removed.
The second is to modify the find solution to find only directories which were last modified during 2007. Problem with that solution is that although the directory may not have been modified since 2007, the files under it may have. How about modifying the find solution to find only files which were last modified during 2007, removing them and then removing any empty directories? For the first part, something like this:
Code:
#!/bin/bash
# Configure script environment
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
export PATH=/usr/bin:/bin
set -o nounset
unalias -a
# Calculate beginning and end times in whole days before now
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
days_this_year=$( date +%j )
days_since_first_day_of_2008=$(( days_this_year + 365 + 366 - 1 ))
days_since_last_day_of_2006=$(( days_this_year + 365 + 366 + 365 ))
# Test
# ~~~~
echo "This should be last day of 2006: $( date --date "today - $days_since_last_day_of_2006 days" +%F )"
echo "This should be first day of 2008: $( date --date "today - $days_since_first_day_of_2008 days" +%F )"
# Find
# ~~~~
find . -daystart -type f -mtime -$days_since_last_day_of_2006 -mtime +$days_since_first_day_of_2008 -exec ls -l {} \;
When you are satisfied it is finding only files you want to delete then change the part after -exec to rm {} \;
Thank you catkin for your help, i modify it little bit( -type d)for a directory and i added -exec rm -r but unfortunately did not work with this msg:
find: ./E7054F85AE5547d6AD7D2FA2BEEFD59A: No such file or directory
Thank you catkin for your help, i modify it little bit( -type d)for a directory and i added -exec rm -r but unfortunately did not work with this msg:
find: ./E7054F85AE5547d6AD7D2FA2BEEFD59A: No such file or directory
Could it be that ./E7054F85AE5547d6AD7D2FA2BEEFD59A has already been removed because the first file found was . (the current directory) so rm -r . had already removed ./E7054F85AE5547d6AD7D2FA2BEEFD59A and everything else under . ?
I did advise that removing directories was risky: "Problem with that solution is that although the directory may not have been modified since 2007, the files under it may have".
Have you browsed the file system to see what has been removed?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.