LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices



Reply
 
Search this Thread
Old 04-16-2008, 09:06 AM   #1
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 66
find -delete folders


I'm running the following command within a script:
Code:
find $fold -maxdepth 0 -type d -exec rsync -r {}/ $base \; -exec rm -fr {} \; 2> /dev/null
But I'd rather have:
Code:
find $fold -maxdepth 0 -type d -exec rsync -r {}/ $base \; -delete 2> /dev/null
But find doesn't seem to want to delete folders...

The entire script is:
Code:
#!/bin/bash

root=$1
if [ -z $1 ]; then root=`pwd`; fi

compact_f()
{
	find $fold -maxdepth 0 -type d -exec rsync -r {}/ $base \; -exec rm -fr {} \; 2> /dev/null
}

for ((year=2004; year < `date +%Y`; year++)); do
	base=$root/$year
	fold=$base.[0-9][0-9]*
	compact_f
done

for ((j=1; j < `date +%m`; j++)); do
	year=`date +%Y`
	month=$j
	if [ $month -lt 10 ]; then month=0$month; fi
	
	base=$root/$year.$month
	fold=$base.[0-9][0-9]*
	compact_f
done

for ((k=1; k < `date +%d`; k++)); do
	year=`date +%Y`
	month=`date +%m`
	day=$k
	if [ $day -lt 10 ]; then day=0$day; fi
	
	base=$root/$year.$month.$day
	fold=$base\_[0-9][0-9].[0-9][0-9].[0-9][0-9]
	compact_f
done
It takes my archive folders which are created like YYYY.MM.DD_HH.MM.SS when they are first generated. After the day is up, it compacts all of the folders for a particular day into one (YYYY.MM.DD), after the month is up it compacts all of the folders for that month into one (YYYY.MM), and the same for the year (YYYY).

It works pretty well, I'm just trying to speed it up a little. Any suggestions?

Last edited by Meson; 04-16-2008 at 09:07 AM.
 
Old 04-16-2008, 09:23 AM   #2
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
Sorry, I erred in my response. Please ignore.

Last edited by jschiwal; 04-16-2008 at 09:26 AM.
 
Old 04-16-2008, 10:39 AM   #3
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 64
I think you meant
Code:
find $fold -maxdepth 1 -type d ...
because "-maxdepth 0" returns only "."

and even so, "find . -maxdepth 1 -type d -delete" does not delete non-empty directories. It is equivalent to a simple "rm".
 
Old 04-16-2008, 12:03 PM   #4
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Original Poster
Rep: Reputation: 66
I think I want the depth to be 0 because I'm only searching for directories within the working directory ($root).

Let's say I have:
Code:
/2008.04.01_10.30.00
/2008.04.01_11.30.00
/2008.04.01_12.30.00
/2008.04.01_12.30.00/2008.11.11_11.11.11
I don't want the subfolder 2008.11.11_11.11.11 to be seen as a special folder by this script. It's just any other file/folder.
 
Old 04-16-2008, 12:57 PM   #5
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 64
If I understood you correctly, this was my interpretation. look:
Code:
[miguel@babylon5 ~]$ cd tmp
[miguel@babylon5 tmp]$ mkdir meson
[miguel@babylon5 tmp]$ cd meson
[miguel@babylon5 meson]$ mkdir 2008.04.01_10.30.00 2008.04.01_11.30.00 2008.04.01_12.30.00 2008.04.01_12.30.00/2008.11.11_11.11.11
[miguel@babylon5 meson]$ tree .
.
|-- 2008.04.01_10.30.00
|-- 2008.04.01_11.30.00
`-- 2008.04.01_12.30.00
    `-- 2008.11.11_11.11.11

4 directories, 0 files
[miguel@babylon5 meson]$ find . -maxdepth 0 -type d
.
[miguel@babylon5 meson]$ find . -maxdepth 1 -type d
.
./2008.04.01_10.30.00
./2008.04.01_11.30.00
./2008.04.01_12.30.00
[miguel@babylon5 meson]$
(the subfolder 2008.11.11_11.11.11 was not seen by rsync, just like you want)
With maxdepth=0, rsync is just called once, with "." as source dir. None of 2008.04* are arguments to rsync.

There is another problem too. maxdepth=1 returns "." as first entry.
With maxdepth=1, rsync will process all dirs twice: one with "." and again for each dir in "."

Is that what you want or I missed something ?

regards,

Last edited by marozsas; 04-16-2008 at 12:59 PM. Reason: just to add tree output
 
Old 04-16-2008, 02:13 PM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
I guess my idea wasn't so dumb after all. There is a sample in the find info page that uses -maxdepth 0. This setting tests files listed as arguments. For example: find filea fileb filec filed -maxdepth 0 -empty will test just the 3 files in the arguments and return if one is empty.

The example in the info find manual has a find command argument of xargs to supply the arguments.
A perl script modifies the result of the first find command and this is fed to "xargs find {} maxdepth 0 ...".

Last edited by jschiwal; 04-16-2008 at 02:23 PM.
 
Old 04-16-2008, 02:22 PM   #7
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,393
Blog Entries: 1

Rep: Reputation: 64
and what was your idea jschiwal ? You edited your post and I never had a chance to read it.
 
Old 04-16-2008, 02:30 PM   #8
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
I found that my explanation did the same thing with -maxdepth 1 so it wasn't a good demonstration.
I created 4 files, two that were empty. I had 3 files listed in the find command: find filea fileb filec -maxdepth 0 -empty

After repeating this experiment with -maxdepth 1, I decided to stick with the example in the info file.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cannot delete existing folders yusufs Linux - Newbie 1 12-02-2007 08:02 AM
Can't delete folders from Windows Tenover Linux - Networking 8 12-24-2005 12:55 AM
Can't delete Folders. duffmckagan Suse/Novell 1 09-19-2005 12:09 PM
HELP: can't delete files/folders :( kevingpo General 1 12-07-2004 08:51 PM
Am I able to delete folders after install? SuSE_Surfer Linux - General 6 10-18-2003 12:41 AM


All times are GMT -5. The time now is 08:11 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration