LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > SUSE / openSUSE
User Name
Password
SUSE / openSUSE This Forum is for the discussion of Suse Linux.

Notices


Reply
  Search this Thread
Old 11-15-2010, 07:31 AM   #1
ladhibi
LQ Newbie
 
Registered: Mar 2009
Posts: 15

Rep: Reputation: 0
How to sort and delete directory going back for years?


Got directories dated of 2009 2008,
What is the best way to ls the directories, sort them by date, redirect the output to a file and then delete them?

Thank you
 
Old 11-15-2010, 07:36 AM   #2
stress_junkie
Senior Member
 
Registered: Dec 2005
Location: Massachusetts, USA
Distribution: Ubuntu 10.04 and CentOS 5.5
Posts: 3,873

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Sounds like homework.

Why do you want to sort the files by date if you are going to delete all of them?

Why do you want to put all of the file names in a file before you delete them?
 
Old 11-15-2010, 08:00 AM   #3
ladhibi
LQ Newbie
 
Registered: Mar 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by stress_junkie View Post
Sounds like homework.

Why do you want to sort the files by date if you are going to delete all of them?

Why do you want to put all of the file names in a file before you delete them?
I need to sort them because i have 2010 directories to be kept, to put them in a file just to keep record of what was deleted..

regards
 
Old 11-15-2010, 08:30 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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.
 
Old 11-15-2010, 11:27 PM   #5
ladhibi
LQ Newbie
 
Registered: Mar 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Wake up Linux Guru....

still waiting for an answer, script or perl command what ever.. I have to delete all directories dated 2008 for example.. plz hlp
 
Old 11-15-2010, 11:31 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Generally LQ does not write scripts to order; it helps you when you get stuck. That way you learn. What have you tried so far?
 
Old 11-16-2010, 04:37 AM   #7
ladhibi
LQ Newbie
 
Registered: Mar 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
Generally LQ does not write scripts to order; it helps you when you get stuck. That way you learn. What have you tried so far?
ok i found this useful for me and it works..:

rm -R `ls -l | grep '2007' | tr -s ' ' | cut -d ' ' -f9`
 
Old 11-16-2010, 05:46 AM   #8
ladhibi
LQ Newbie
 
Registered: Mar 2009
Posts: 15

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ladhibi View Post
ok i found this useful for me and it works..:

rm -R `ls -l | grep '2007' | tr -s ' ' | cut -d ' ' -f9`
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?
 
Old 11-16-2010, 06:24 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ladhibi View Post
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:
Code:
find <your starting directory> -type d -name '*2007*' -exec echo rm -fr {} \;
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?
 
Old 11-17-2010, 12:20 AM   #10
ladhibi
LQ Newbie
 
Registered: Mar 2009
Posts: 15

Original Poster
Rep: Reputation: 0
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?

thank you
 
Old 11-17-2010, 12:27 AM   #11
ladhibi
LQ Newbie
 
Registered: Mar 2009
Posts: 15

Original Poster
Rep: Reputation: 0
by the way the directory name is this type D4320071D5CC49029024DB1AD2E70F3B
 
Old 11-17-2010, 12:46 AM   #12
ladhibi
LQ Newbie
 
Registered: Mar 2009
Posts: 15

Original Poster
Rep: Reputation: 0
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
---------------------------------------------------------------------------
 
Old 11-17-2010, 06:19 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ladhibi View Post
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):
Code:
ls -l | grep '2007' | tr -s ' ' | cut -d ' ' -f 9 | xargs echo rm
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 {} \;
 
Old 11-17-2010, 07:13 AM   #14
ladhibi
LQ Newbie
 
Registered: Mar 2009
Posts: 15

Original Poster
Rep: Reputation: 0
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

find . -daystart -type d -mtime -$days_since_last_day_of_2006 -mtime +$days_since_first_day_of_2008 -exec rm -r {} \;
 
Old 11-17-2010, 08:43 AM   #15
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ladhibi View Post
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

find . -daystart -type d -mtime -$days_since_last_day_of_2006 -mtime +$days_since_first_day_of_2008 -exec rm -r {} \;
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?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Back to Linux after many years jasonhtt LinuxQuestions.org Member Intro 1 12-29-2009 09:43 PM
LXer: IBM developerWorks looks back on 10 years of Linux LXer Syndicated Linux News 0 12-18-2009 06:30 PM
LXer: Looking Back on Three Years of OpenUsability with Jan Mühlig LXer Syndicated Linux News 0 12-01-2006 08:03 PM
Three years later, three steps back RAnthony General 1 09-11-2004 04:35 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > SUSE / openSUSE

All times are GMT -5. The time now is 03:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration