|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi all,
I have a script which finds all the files edited x days ago. Having found these files, I wish to loop round them, check their name, and if they do not have the same name as the script, remove them. My probelm is once I've executed the find command; find . -mtime ${maxTime} how do I get this into an array? After this I would do something like for f in $array do ..... done where $array is the array containing the find output thanks |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 johnmmcparland wrote: > Hi all, > > I have a script which finds all the files edited x days ago. > > Having found these files, I wish to loop round them, check their name, > and if they do not have the same name as the script, remove them. > > My probelm is once I've executed the find command; > > find . -mtime ${maxTime} > > how do I get this into an array? You don't need that. find . -mtime ${maxTime} -exec echo {} \; > > After this I would do something like > > for f in $array > do > .... > done > > where $array is the array containing the find output > > thanks > np - -- Stephan 'smg' Grein, <stephan at stephan minus rockt dot de> https://stephangrein.de GnuPG-Key-ID: 0xF8C275D4 FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) iQIVAwUBRO7ZsI1LAjD4wnXUAQIh4w/9H1Z2WQSf2bHa9QtI9FYVWrHc4r84QtFb BQREyevHDWlxrDFgv3vhd3tKIgHWmSdvpmrrCUSqPXISrlkxG/1ny26YXYXeJ5/U VydPuBKJG4Olvwl8InPeRlVXioa2fvLAFKd4RmXANW0rZlmB5t vPsmfG8qIIhQpx 9Lbz47Ox7j9r3Vft+7pnpM+zraCbMsNOmChDrC/h9G5tBbhvo34PjReNXhZsR0EQ emqbDzmm0xYn/dpJTrEi4yNWEVbenZFns4Kt+VSu1bCa3jdfLtiJ7Lpr1Y1cHfD 5 xjOB8RuGULKFM1x+l5sBhcpfjEWUm1+nMX23c/iHcehkPhpN1KTSatHJxdH8x1Xk 2CGqMPMOvZIZSS8gR2KFGdBhWZZKsE6lqexo3xAe6gaCpYGSVm 7Rr+epl6dE2l6d NHC+CZ3voiTtAAFPN/mPKX+JtaZ0UVOFdC0npj7+3AoLnkdGwZROjazfYzRFmz7C d7sG6wI9plYx+MJXNCjxPJ0rvaZHvHw3duEIQpk87vZdgLvQNN A/bhnmH9hgw5Wa 5eRkH5nPaCJRJmazbM7YEJodqQW8RAvqFP5Rjp4cy7Kx6cEnNs 7gu87WMF+IDibV ivmDFhj9i+q9MQu9aR8irNaLDt6PGeLOQVU/xnedp6gq/8HK9jRzwrqwJKKt6OgL APMIhUcSoz4= =zar6 -----END PGP SIGNATURE----- |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1 Also you could pipe the find output to a for loop. ![]() Bye. - -- Stephan 'smg' Grein, <stephan at stephan minus rockt dot de> https://stephangrein.de GnuPG-Key-ID: 0xF8C275D4 FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4 -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) iQIVAwUBRO7Z941LAjD4wnXUAQI14RAAkJ5qg2wxajGOH0TJiQ SOQFjJzQV8mNuo IZTsXoVFecAutcLNzjIZRstuBytf+SuKRi7/Yb0ndtEyTkpjngx79o+hLgu4yBLn HXAMFBqB2ILeJcdk2wEfk2FjtIBbB2GP0s4V6VRadyMCfYYTmp s/0wXTW6jWk4Cs p4XLEEk/LIu5soggAU4v1/O3uUbeB3Tn8FwqDALloEIFAwZEJyZAkx+d16Ewrvz+ 8wesp9CjwMjoVUJNHbWMwojHOutDVEdv8pK/6S5PYTtbtv0Iwmh675/QWPrm21DP 7wNeqzrsnGq9Kd6mn8og6nCJgJo1GXIy9GnrYkDk1P9VPrN1lz eJ0MRIa0ce2sqV Y16h1pO91uPFQpCtqM6ROHxWu2bG6gWxk1i6SDxIb9WjK7u4P1 0SQVuYfgKbW1+J NxbXXqX08BDuLdf6A7+CetNClruh6BlQgf6Lbw60ejVYVIBizZ rIQY997IIvzEuG CdyF/V87u+Fx1uWUU1XvHMae5c7bNvMGJvLt4zJoBAvyMQ5Z9gvGVOW 9NwGXNnvA LHDp2VoyiVrQAf0EQwVmSNj+VWup8nwNBu2sWAaeeZHLNZSqmw RRpbOWUiWh4/Np Z2I0O3oJxFSN4JSqm7ObtPo2a38hfvku25h4/3vn9vsC41qXW2L/mvZmWWdsVJ/2 9USFAoNE12c= =00/l -----END PGP SIGNATURE----- |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
when I do the -exec ...
what variable are the filenames in? if want to do; if ${f} != fileNameNotToBeDeleted rm ${f} where f is a filename what should I be using in place of f? Stephan 'smg' Grein wrote: > -----BEGIN PGP SIGNED MESSAGE----- > Hash: SHA1 > > johnmmcparland wrote: > > Hi all, > > > > I have a script which finds all the files edited x days ago. > > > > Having found these files, I wish to loop round them, check their name, > > and if they do not have the same name as the script, remove them. > > > > My probelm is once I've executed the find command; > > > > find . -mtime ${maxTime} > > > > how do I get this into an array? > > You don't need that. > find . -mtime ${maxTime} -exec echo {} \; > > > > After this I would do something like > > > > for f in $array > > do > > .... > > done > > > > where $array is the array containing the find output > > > > thanks > > > np > > - -- > Stephan 'smg' Grein, <stephan at stephan minus rockt dot de> > https://stephangrein.de > GnuPG-Key-ID: 0xF8C275D4 > FingerPrint: 5B6F 134A 189B A24D 342B 0961 8D4B 0230 F8C2 75D4 > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.5 (GNU/Linux) > > iQIVAwUBRO7ZsI1LAjD4wnXUAQIh4w/9H1Z2WQSf2bHa9QtI9FYVWrHc4r84QtFb > BQREyevHDWlxrDFgv3vhd3tKIgHWmSdvpmrrCUSqPXISrlkxG/1ny26YXYXeJ5/U > VydPuBKJG4Olvwl8InPeRlVXioa2fvLAFKd4RmXANW0rZlmB5t vPsmfG8qIIhQpx > 9Lbz47Ox7j9r3Vft+7pnpM+zraCbMsNOmChDrC/h9G5tBbhvo34PjReNXhZsR0EQ > emqbDzmm0xYn/dpJTrEi4yNWEVbenZFns4Kt+VSu1bCa3jdfLtiJ7Lpr1Y1cHfD 5 > xjOB8RuGULKFM1x+l5sBhcpfjEWUm1+nMX23c/iHcehkPhpN1KTSatHJxdH8x1Xk > 2CGqMPMOvZIZSS8gR2KFGdBhWZZKsE6lqexo3xAe6gaCpYGSVm 7Rr+epl6dE2l6d > NHC+CZ3voiTtAAFPN/mPKX+JtaZ0UVOFdC0npj7+3AoLnkdGwZROjazfYzRFmz7C > d7sG6wI9plYx+MJXNCjxPJ0rvaZHvHw3duEIQpk87vZdgLvQNN A/bhnmH9hgw5Wa > 5eRkH5nPaCJRJmazbM7YEJodqQW8RAvqFP5Rjp4cy7Kx6cEnNs 7gu87WMF+IDibV > ivmDFhj9i+q9MQu9aR8irNaLDt6PGeLOQVU/xnedp6gq/8HK9jRzwrqwJKKt6OgL > APMIhUcSoz4= > =zar6 > -----END PGP SIGNATURE----- |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
johnmmcparland wrote:
> Hi all, > > I have a script which finds all the files edited x days ago. > > Having found these files, I wish to loop round them, check their name, > and if they do not have the same name as the script, remove them. > > My probelm is once I've executed the find command; > > find . -mtime ${maxTime} > > how do I get this into an array? > > After this I would do something like > > for f in $array > do > .... > done > > where $array is the array containing the find output > > thanks > find . -type f -mtime ${maxTime} ! -name ${badname} -exec rm {} ';' === find . -type f -mtime ${maxTime} | while read name do #something with "$name" done === array=( $(find . -type f -mtime ${maxTime}) ) for f in ${array[*]} do #something with "$f" done |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
Thanks,
Jon LaBadie wrote: > johnmmcparland wrote: > > Hi all, > > > > I have a script which finds all the files edited x days ago. > > > > Having found these files, I wish to loop round them, check their name, > > and if they do not have the same name as the script, remove them. > > > > My probelm is once I've executed the find command; > > > > find . -mtime ${maxTime} > > > > how do I get this into an array? > > > > After this I would do something like > > > > for f in $array > > do > > .... > > done > > > > where $array is the array containing the find output > > > > thanks > > > > find . -type f -mtime ${maxTime} ! -name ${badname} -exec rm {} ';' > > === > > find . -type f -mtime ${maxTime} | > while read name > do > #something with "$name" > done > > === > > array=( $(find . -type f -mtime ${maxTime}) ) > > for f in ${array[*]} > do > #something with "$f" > done |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
johnmmcparland wrote:
> I have a script which finds all the files edited x days ago. > > Having found these files, I wish to loop round them, check their name, > and if they do not have the same name as the script, remove them. > > My probelm is once I've executed the find command; > > find . -mtime ${maxTime} > > how do I get this into an array? If you use somthing like KSH93 or equivalent, you can do set -A files 0 $(find . -mtime ${maxTime}) This will set up an array files[1] .. files[n]; you can also set up a zero based array by leaving off the leading '0' value. You can then set up a loop: typeset -i filecnt=${#files[*]} typeset -i cnt=0; while cnt=cnt+1 ; (( cnt < filecnt )) ; do # stuff against ${files[cnt]} done |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
"johnmmcparland" <johnmmcparland@googlemail.com> writes:
> I have a script which finds all the files edited x days ago. > Having found these files, I wish to loop round them, check their name, > and if they do not have the same name as the script, remove them. > > My probelm is once I've executed the find command; > find . -mtime ${maxTime} > how do I get this into an array? > > After this I would do something like > for f in $array; do ...; done > where $array is the array containing the find output The problem with find is that it's useless. Without -print0 option it'll just crush and burn but unfortunatelly -print0 is not in the standard. You can therefore do two things, either: find . -type f -mtime $maxTime -exec some_script {} \; where some_script is a script which takes the file name and delete it if you wish; or: find . -type f -mtime $maxTime ! -name 'some-pattern' -exec rm {} \; where some-pattern is a pattern which file must not much. -- Best regards, _ _ .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michal "mina86" Nazarewicz (o o) ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo-- |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
2006-08-26, 13:15(-04), kenneth kahn:
[...] > If you use somthing like KSH93 or equivalent, you can do > > set -A files 0 $(find . -mtime ${maxTime}) ITYM IFS=$'\n' set -f set -A files 0 $(find . -mtime "$maxTime" -print) which is slightly better but not correct yet. You need GNU find and zsh for the task. IFS=$'\0' files=($(find . -mtime $maxTime -print0)) or simply as zsh has find functionality builtin: files=(./**/*(ND.m$maxTime)) -- Stéphane |
|
|
|
#10 |
|
Messages: n/a
Hébergeur: |
> how do I get this into an array?
In the Korn Shell there is an ability to execute a command and put the results into an array: #!/bin/ksh set -A myarray $(find ./ -name "*.pl") let count=0 while (( $count < ${#myarray[*]} )); do echo FILE: ${myarray[count]} let count="count + 1" done Also, you could use perl: #!/usr/bin/perl open FINDEM,'find ./ -name "*.pl" |' or die "Cannot open pipe: $!\n"; @findarr = <FINDEM>; for ($i = 0; $i <= $#findarr; $i++) { print "FILE: $findarr[$i]"; } You can also use this perl: #!/usr/bin/perl @findarr = `find ./ -name "*.pl"`; for ($i = 0; $i <= $#findarr; $i++) { print "FILE: $findarr[$i]"; } But, I think the backtick ( ` ) operator has been deprecated and may not be available in current or future versions of perl. I'm using version 5.8.0 of perl -- not sure if this is current. I didn't use your exact find command for my testing purposes. This will at least give you a head start. Hope this s. Art Ramos |
|
|
|
#11 |
|
Messages: n/a
Hébergeur: |
Your second option was great but it only allows one pass on the
results. > array=( $(find . -type f -mtime ${maxTime}) ) > for f in ${array[*]} > do > #something with "$f" > done I tried this option but it doesn't like the outer parenthesis on the first line for some reason. Is this a mistype? Thanks. Art Ramos |
|
|
|
#12 |
|
Messages: n/a
Hébergeur: |
First Lensman wrote:
> > how do I get this into an array? > > In the Korn Shell there is an ability to execute a command and put the > results into an array: > > #!/bin/ksh > set -A myarray $(find ./ -name "*.pl") > let count=0 > while (( $count < ${#myarray[*]} )); do > echo FILE: ${myarray[count]} > let count="count + 1" > done > > Also, you could use perl: > > #!/usr/bin/perl > open FINDEM,'find ./ -name "*.pl" |' or die "Cannot open pipe: $!\n"; > @findarr = <FINDEM>; > for ($i = 0; $i <= $#findarr; $i++) { > print "FILE: $findarr[$i]"; > } > > You can also use this perl: > > #!/usr/bin/perl > @findarr = `find ./ -name "*.pl"`; > for ($i = 0; $i <= $#findarr; $i++) { > print "FILE: $findarr[$i]"; > } > > But, I think the backtick ( ` ) operator has been deprecated and may > not be available in current or future versions of perl. I'm using > version 5.8.0 of perl -- not sure if this is current. Never heard of that.. my current version is: 5.8.7 pipe or backtick or qx() are not necessary in this case. Perl has modules, like File::Find, to do such job, which should be platform-independent. ################## use strict; use warnings; use File::Find; my @findarr=(); find(\&findpl, @ARGV); sub findpl { push @findarr, $File::Find::name if -f && /\.pl$/; } print map "$_\n", @findarr; ################### Xicheng |
|
|
|
#13 |
|
Messages: n/a
Hébergeur: |
First Lensman wrote:
> Your second option was great but it only allows one pass on the > results. > > > array=( $(find . -type f -mtime ${maxTime}) ) > > for f in ${array[*]} > > do > > #something with "$f" > > done > > I tried this option but it doesn't like the outer parenthesis on the > first line for some reason. > Is this a mistype? > that worked fine in the following shells: 1) Linux 2.6.12-9-386 GNU/Linux, GNU bash, version 3.00.16(1)-release (i486-pc-linux-gnu) 2) SunOS 5.8 GNU bash, version 2.03.0(1)-release (sparc-sun-solaris) not working under 'tcsh'. Also, if there are whitespaces within filenames, it might be better to reset IFS before assigning array, like: IFS= ; arrar=( $(find . -type f -mtime ${maxTime}) ) Xicheng |
|
|
|
#14 |
|
Messages: n/a
Hébergeur: |
First Lensman wrote:
>>how do I get this into an array? > > In the Korn Shell there is an ability to execute a command and put the > results into an array: > > #!/bin/ksh > set -A myarray $(find ./ -name "*.pl") > let count=0 > while (( $count < ${#myarray[*]} )); do > echo FILE: ${myarray[count]} > let count="count + 1" > done > > Also, you could use perl: > > #!/usr/bin/perl > open FINDEM,'find ./ -name "*.pl" |' or die "Cannot open pipe: $!\n"; > @findarr = <FINDEM>; > for ($i = 0; $i <= $#findarr; $i++) { > print "FILE: $findarr[$i]"; > } A while loop is normally used in that situation. Also if 'find' dies for some reason you should capture its error code when you close the filehandle and you can avoid invoking a shell to run find by passing open a list instead of a string: #!/usr/bin/perl open FINDEM, '-|', 'find', './', '-name', '*.pl' or die "Cannot open pipe: $!\n"; while ( my $file = <FINDEM> ) { print "FILE: $file"; } close FINDEM or warn $! ? "Error closing find pipe: $!" : "Exit status $? from find"; > You can also use this perl: > > #!/usr/bin/perl > @findarr = `find ./ -name "*.pl"`; > for ($i = 0; $i <= $#findarr; $i++) { > print "FILE: $findarr[$i]"; > } You don't need a second copy of the list stored in an array: #!/usr/bin/perl for my $file ( `find ./ -name "*.pl"` ) { print "FILE: $file"; } Or without the loop: #!/usr/bin/perl print map "FILE: $_", `find ./ -name "*.pl"`; > But, I think the backtick ( ` ) operator has been deprecated and may > not be available in current or future versions of perl. I'm using > version 5.8.0 of perl -- not sure if this is current. In all the many years I haved used Perl I have never heard that and I doubt very much that that is true. John -- use Perl; program fulfillment |
|
![]() |
| Outils de la discussion | |
|
|