|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I'm using Cygwin, which is mostly gnu.
To test if a directory has any nondirectory files in its sub- hierarchy, I create a script "CountFiles" containing: #!/bin/bash (( ` find "$@" -type f | wc -l ` > 0 )) and run the following from the command line: find DirectoryName -exec CountFiles '{}' \; -print I intend to go through all the directories of a file tree of interest and apply this. It is obviously inefficient, since the command-line "find" starts at the top and applies the scripted "find", thus ensuring that the directories near the bottom of the tree get tested many times. Suggestions for a better way are welcome! As well, I thought I'd speed it up by replacing the script with a function: function CountFiles() { (( ` find "$@" -type f | wc -l ` > 0 )) ; } Unfortunately, the function is not recognized inside the -exec command for the command-line find. So in addition to a more efficient way to search, I'd appreciate any suggestions to maing the function visible from the -exec of a a command-line "find". Thanks! |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
I'd do something like:
find . -type d -printf '%p\n' -o -printf '%h\n' | sort | uniq -u (GNU specific). -- Stéphane |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Mister.Fred.Ma@gmail.com wrote:
> I'm using Cygwin, which is mostly gnu. > > To test if a directory has any nondirectory files in its sub- > hierarchy, I create a script "CountFiles" containing: > > #!/bin/bash > (( ` find "$@" -type f | wc -l ` > 0 )) > > and run the following from the command line: > > find DirectoryName -exec CountFiles '{}' \; -print > > I intend to go through all the directories of a file tree of interest > and apply this. It is obviously inefficient, since the command-line > "find" starts at the top and applies the scripted "find", thus > ensuring that the directories near the bottom of the tree get tested > many times. > > Suggestions for a better way are welcome! > > As well, I thought I'd speed it up by replacing the script with a > function: > > function CountFiles() > { (( ` find "$@" -type f | wc -l ` > 0 )) ; } > > Unfortunately, the function is not recognized inside the -exec command > for the command-line find. So in addition to a more efficient way to > search, I'd appreciate any suggestions to maing the function visible > from the -exec of a a command-line "find". > > Thanks! > You say you want to list directories that contain files, but IMHO your example lists files, too. The following lists directories where the tree contains one or more files: find DirectoryName -type f | awk -F/ '{$NF=""} d[$0]++==0' OFS=/ -- Michael Tosch @ hp : com |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On Jul 25, 4:50 pm, Stephane CHAZELAS <this.addr...@is.invalid> wrote:
> I'd do something like: > > find . -type d -printf '%p\n' -o -printf '%h\n' | sort | uniq -u > > (GNU specific). Thanks Stephane. I looked up -printf at http://www.gnu.org/software/findutil...mono/find.html. Your command locates each directory, prints its relative path, and the relative path of its parent directory. If a path only shows up once, it represents a directory with no subdirectories, and these are the ones I am shown. However, it might contain nondirectory files, and I don't want to target such directories. I could solve this problem by removint "-type d", but the number of lines to "sort" could be quite large. Leaf-node directories are not checked in the find command above. If they are completely empty, I'd like to identify them. I'll mull over your approach to see if it can be made to identify directories containing nothing but possibly empty directories in their subtrees. Thanks! |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Jul 25, 5:58 pm, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se>
wrote: > Mister.Fred...@gmail.com wrote: > > I'm using Cygwin, which is mostly gnu. > > > To test if a directory has any nondirectory files in its sub- > > hierarchy, I create a script "CountFiles" containing: > > > #!/bin/bash > > (( ` find "$@" -type f | wc -l ` > 0 )) > > > and run the following from the command line: > > > find DirectoryName -exec CountFiles '{}' \; -print > > > I intend to go through all the directories of a file tree of interest > > and apply this. It is obviously inefficient, since the command-line > > "find" starts at the top and applies the scripted "find", thus > > ensuring that the directories near the bottom of the tree get tested > > many times. > > > Suggestions for a better way are welcome! > > > As well, I thought I'd speed it up by replacing the script with a > > function: > > > function CountFiles() > > { (( ` find "$@" -type f | wc -l ` > 0 )) ; } > > > Unfortunately, the function is not recognized inside the -exec command > > for the command-line find. So in addition to a more efficient way to > > search, I'd appreciate any suggestions to maing the function visible > > from the -exec of a a command-line "find". > > > Thanks! > > You say you want to list directories that contain files, > but IMHO your example lists files, too. > The following lists directories where the tree contains one or more files: > > find DirectoryName -type f | awk -F/ '{$NF=""} d[$0]++==0' OFS=/ Sorry. I oversimplified the example for posting. I am actually trying to find all directories whose subtrees contain nothing but possibly more empty directories. So I'd use find * -type d \! -exec CountFiles '{}' \; -print Actual usage as more find arguments to echo each directory as it is being checked, but that obscures the purpose. I'm not familiar with awk, but I'll look into it. Thanks. Fred |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On Jul 25, 6:12 pm, Mister.Fred...@gmail.com wrote:
> On Jul 25, 4:50 pm, Stephane CHAZELAS <this.addr...@is.invalid> wrote: > > > I'd do something like: > > > find . -type d -printf '%p\n' -o -printf '%h\n' | sort | uniq -u > > > (GNU specific). > > Thanks Stephane. I looked up -printf athttp://www.gnu.org/software/findutils/manual/html_mono/find.html. > Your command locates each directory, prints its relative path, and the > relative path of its parent directory. If a path only shows up once, > it represents a directory with no subdirectories, and these are the > ones I am shown. However, it might contain nondirectory files, and I > don't want to target such directories. I could solve this problem by > removint "-type d", but the number of lines to "sort" could be quite > large. > > Leaf-node directories are not checked in the find command above. If > they are completely empty, I'd like to identify them. > > I'll mull over your approach to see if it can be made to identify > directories containing nothing but possibly empty directories in their > subtrees. Stephane, I caused some confusion by neglecting to clarify that I only want to identify directories that contain nothing but possibly more subdirectories. So my actual usage would be more like find * -type d \! -exec CountFiles '{}' \; -print Fred |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
On Jul 25, 6:22 pm, Mister.Fred...@gmail.com wrote:
> On Jul 25, 6:12 pm, Mister.Fred...@gmail.com wrote: > > > > > On Jul 25, 4:50 pm, Stephane CHAZELAS <this.addr...@is.invalid> wrote: > > > > I'd do something like: > > > > find . -type d -printf '%p\n' -o -printf '%h\n' | sort | uniq -u > > > > (GNU specific). > > > Thanks Stephane. I looked up -printf athttp://www.gnu.org/software/findutils/manual/html_mono/find.html. > > Your command locates each directory, prints its relative path, and the > > relative path of its parent directory. If a path only shows up once, > > it represents a directory with no subdirectories, and these are the > > ones I am shown. However, it might contain nondirectory files, and I > > don't want to target such directories. I could solve this problem by > > removint "-type d", but the number of lines to "sort" could be quite > > large. > > > Leaf-node directories are not checked in the find command above. If > > they are completely empty, I'd like to identify them. > > > I'll mull over your approach to see if it can be made to identify > > directories containing nothing but possibly empty directories in their > > subtrees. > > Stephane, I caused some confusion by neglecting to clarify that I only > want to identify directories that contain nothing but possibly more > subdirectories. So my actual usage would be more like > > find * -type d \! -exec CountFiles '{}' \; -print Also, upon mulling over your statement, I realize that my explanation of it (above) is not correct. Continuing to mull.... |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
Mister.Fred.Ma@gmail.com wrote:
> On Jul 25, 5:58 pm, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se> > wrote: >> Mister.Fred...@gmail.com wrote: >>> I'm using Cygwin, which is mostly gnu. >>> To test if a directory has any nondirectory files in its sub- >>> hierarchy, I create a script "CountFiles" containing: >>> #!/bin/bash >>> (( ` find "$@" -type f | wc -l ` > 0 )) >>> and run the following from the command line: >>> find DirectoryName -exec CountFiles '{}' \; -print >>> I intend to go through all the directories of a file tree of interest >>> and apply this. It is obviously inefficient, since the command-line >>> "find" starts at the top and applies the scripted "find", thus >>> ensuring that the directories near the bottom of the tree get tested >>> many times. >>> Suggestions for a better way are welcome! >>> As well, I thought I'd speed it up by replacing the script with a >>> function: >>> function CountFiles() >>> { (( ` find "$@" -type f | wc -l ` > 0 )) ; } >>> Unfortunately, the function is not recognized inside the -exec command >>> for the command-line find. So in addition to a more efficient way to >>> search, I'd appreciate any suggestions to maing the function visible >>> from the -exec of a a command-line "find". >>> Thanks! >> You say you want to list directories that contain files, >> but IMHO your example lists files, too. >> The following lists directories where the tree contains one or more files: >> >> find DirectoryName -type f | awk -F/ '{$NF=""} d[$0]++==0' OFS=/ > > Sorry. I oversimplified the example for posting. I am actually > trying to find all directories whose subtrees contain nothing but > possibly more empty directories. So I'd use > > find * -type d \! -exec CountFiles '{}' \; -print > Then I think Stephane has understood you right and his solution is ok: print directories and print directories that have files, then sort out the directories that occur two or more times. The remaining question is what do you do with the result? If you only want to remove empty directories, this is simply find dir -depth -type d -empty -exec rmdir {} \; -- Michael Tosch @ hp : com |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
Mister.Fred.Ma@gmail.com wrote:
> On Jul 25, 5:58 pm, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se> > wrote: >> Mister.Fred...@gmail.com wrote: >>> I'm using Cygwin, which is mostly gnu. >>> To test if a directory has any nondirectory files in its sub- >>> hierarchy, I create a script "CountFiles" containing: >>> #!/bin/bash >>> (( ` find "$@" -type f | wc -l ` > 0 )) >>> and run the following from the command line: >>> find DirectoryName -exec CountFiles '{}' \; -print >>> I intend to go through all the directories of a file tree of interest >>> and apply this. It is obviously inefficient, since the command-line >>> "find" starts at the top and applies the scripted "find", thus >>> ensuring that the directories near the bottom of the tree get tested >>> many times. >>> Suggestions for a better way are welcome! >>> As well, I thought I'd speed it up by replacing the script with a >>> function: >>> function CountFiles() >>> { (( ` find "$@" -type f | wc -l ` > 0 )) ; } >>> Unfortunately, the function is not recognized inside the -exec command >>> for the command-line find. So in addition to a more efficient way to >>> search, I'd appreciate any suggestions to maing the function visible >>> from the -exec of a a command-line "find". >>> Thanks! >> You say you want to list directories that contain files, >> but IMHO your example lists files, too. >> The following lists directories where the tree contains one or more files: >> >> find DirectoryName -type f | awk -F/ '{$NF=""} d[$0]++==0' OFS=/ > > Sorry. I oversimplified the example for posting. I am actually > trying to find all directories whose subtrees contain nothing but > possibly more empty directories. So I'd use > > find * -type d \! -exec CountFiles '{}' \; -print > Then I think Stephane has understood you right and his solution is ok: print directories and print directories that have files, then sort out the directories that occur two or more times. The remaining question is what do you do with the result? If you only want to remove empty directories, this is simply find dir -depth -type d -empty -exec rmdir {} \; -- Michael Tosch @ hp : com |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
On Jul 25, 7:14 pm, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se>
wrote: > Mister.Fred...@gmail.com wrote: > > On Jul 25, 5:58 pm, Michael Tosch <eed...@NO.eed.SPAM.ericsson.PLS.se> > > wrote: > >> Mister.Fred...@gmail.com wrote: > >>> I'm using Cygwin, which is mostly gnu. > >>> To test if a directory has any nondirectory files in its sub- > >>> hierarchy, I create a script "CountFiles" containing: > >>> #!/bin/bash > >>> (( ` find "$@" -type f | wc -l ` > 0 )) > >>> and run the following from the command line: > >>> find DirectoryName -exec CountFiles '{}' \; -print > >>> I intend to go through all the directories of a file tree of interest > >>> and apply this. It is obviously inefficient, since the command-line > >>> "find" starts at the top and applies the scripted "find", thus > >>> ensuring that the directories near the bottom of the tree get tested > >>> many times. > >>> Suggestions for a better way are welcome! > >>> As well, I thought I'd speed it up by replacing the script with a > >>> function: > >>> function CountFiles() > >>> { (( ` find "$@" -type f | wc -l ` > 0 )) ; } > >>> Unfortunately, the function is not recognized inside the -exec command > >>> for the command-line find. So in addition to a more efficient way to > >>> search, I'd appreciate any suggestions to maing the function visible > >>> from the -exec of a a command-line "find". > >>> Thanks! > >> You say you want to list directories that contain files, > >> but IMHO your example lists files, too. > >> The following lists directories where the tree contains one or more files: > > >> find DirectoryName -type f | awk -F/ '{$NF=""} d[$0]++==0' OFS=/ > > > Sorry. I oversimplified the example for posting. I am actually > > trying to find all directories whose subtrees contain nothing but > > possibly more empty directories. So I'd use > > > find * -type d \! -exec CountFiles '{}' \; -print > > Then I think Stephane has understood you right and his solution is ok: > print directories and print directories that have files, > then sort out the directories that occur two or more times. > > The remaining question is what do you do with the result? > > If you only want to remove empty directories, this is simply > > find dir -depth -type d -empty -exec rmdir {} \; OMG. I can't believe that I spent so much time scripting a cleanup of my file systems when it's built right into the find command. I thank you for your magical solution. And I leave a broken person. |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
2007-07-25, 17:58(-07), Mister.Fred.Ma@gmail.com:
[...] >> find dir -depth -type d -empty -exec rmdir {} \; > > OMG. I can't believe that I spent so much time scripting a cleanup of > my file systems when it's built right into the find command. > > I thank you for your magical solution. And I leave a broken person. Note that -empty is not standard and not necessary here. find . -depth -type d -exec rmdir {} \; will work as well. -- Stéphane |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
2007-07-25, 15:22(-07), Mister.Fred.Ma@gmail.com:
> On Jul 25, 6:12 pm, Mister.Fred...@gmail.com wrote: >> On Jul 25, 4:50 pm, Stephane CHAZELAS <this.addr...@is.invalid> wrote: >> >> > I'd do something like: >> >> > find . -type d -printf '%p\n' -o -printf '%h\n' | sort | uniq -u [...] >> I'll mull over your approach to see if it can be made to identify >> directories containing nothing but possibly empty directories in their >> subtrees. > > > Stephane, I caused some confusion by neglecting to clarify that I only > want to identify directories that contain nothing but possibly more > subdirectories. So my actual usage would be more like [...] Oh, I see, I'd go for a similar approach, something like: find . -type d -printf '%p/\n' -o -printf '%p\n' | sed -e '/\/$/b' -e :1 -e 's:\(.*/\)..*:\1:p;t1' -e d| sort | uniq -u -- Stéphane |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
2007-07-25, 15:22(-07), Mister.Fred.Ma@gmail.com:
> On Jul 25, 6:12 pm, Mister.Fred...@gmail.com wrote: >> On Jul 25, 4:50 pm, Stephane CHAZELAS <this.addr...@is.invalid> wrote: >> >> > I'd do something like: >> >> > find . -type d -printf '%p\n' -o -printf '%h\n' | sort | uniq -u [...] >> I'll mull over your approach to see if it can be made to identify >> directories containing nothing but possibly empty directories in their >> subtrees. > > > Stephane, I caused some confusion by neglecting to clarify that I only > want to identify directories that contain nothing but possibly more > subdirectories. So my actual usage would be more like [...] Oh, I see, I'd go for a similar approach, something like: find . -type d -printf '%p/\n' -o -printf '%p\n' | sed -e '/\/$/b' -e :1 -e 's:\(.*/\)..*:\1:p;t1' -e d| sort | uniq -u -- Stéphane |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
Stephane CHAZELAS wrote:
> 2007-07-25, 15:22(-07), Mister.Fred.Ma@gmail.com: >> On Jul 25, 6:12 pm, Mister.Fred...@gmail.com wrote: >>> On Jul 25, 4:50 pm, Stephane CHAZELAS <this.addr...@is.invalid> wrote: >>> >>>> I'd do something like: >>>> find . -type d -printf '%p\n' -o -printf '%h\n' | sort | uniq -u > [...] >>> I'll mull over your approach to see if it can be made to identify >>> directories containing nothing but possibly empty directories in their >>> subtrees. >> >> Stephane, I caused some confusion by neglecting to clarify that I only >> want to identify directories that contain nothing but possibly more >> subdirectories. So my actual usage would be more like > [...] > > Oh, I see, I'd go for a similar approach, something like: > > find . -type d -printf '%p/\n' -o -printf '%p\n' | > sed -e '/\/$/b' -e :1 -e 's:\(.*/\)..*:\1:p;t1' -e d| > sort | uniq -u Thanks, Stephane. I'm going to sit down in a cafe or something and scrutinize that sed command. |
|
|
|
#15 |
|
Messages: n/a
Hébergeur: |
Stephane CHAZELAS wrote:
> 2007-07-25, 17:58(-07), Mister.Fred.Ma@gmail.com: > [...] >>> find dir -depth -type d -empty -exec rmdir {} \; >> OMG. I can't believe that I spent so much time scripting a cleanup of >> my file systems when it's built right into the find command. >> >> I thank you for your magical solution. And I leave a broken person. > > Note that -empty is not standard and not necessary here. > > find . -depth -type d -exec rmdir {} \; > > will work as well. Would it not remove nonempty directories as well? |
|
|
|
#16 |
|
Messages: n/a
Hébergeur: |
On Sun, 29 Jul 2007 09:44:34 -0400, Fred Ma
<fma@REMOVE_NOSPAM.doe.carleton.ca> wrote: > >> >> find . -depth -type d -exec rmdir {} \; >> >> will work as well. > > Would it not remove nonempty directories as well? SYNOPSIS rmdir [OPTION]... DIRECTORY... DESCRIPTION Remove the DIRECTORY(ies), if they are empty. -- The absence of labels [in ECL] is probably a good thing. -- T. Cheatham |
|
|
|
#17 |
|
Messages: n/a
Hébergeur: |
Bill Marcum wrote:
> On Sun, 29 Jul 2007 09:44:34 -0400, Fred Ma > <fma@REMOVE_NOSPAM.doe.carleton.ca> wrote: >>> find . -depth -type d -exec rmdir {} \; >>> >>> will work as well. >> Would it not remove nonempty directories as well? > > SYNOPSIS > rmdir [OPTION]... DIRECTORY... > > DESCRIPTION > Remove the DIRECTORY(ies), if they are empty. Of course! I've been using 'rm' -rf Directories And Files for so long that I forgot that one of the reasons was to get around the cautious behaviour of rmdir. Thank you for the reminder. |
|
![]() |
| Outils de la discussion | |
|
|