dbaspot
Tags Register FAQ Calendar Search Today's Posts Mark Forums Read

How can I search through directories with spaces? - shell

This is a discussion on How can I search through directories with spaces? - shell ; Hi, I'm trying to execute this command find . -name "*.xml" | xargs grep -i 'fillInBlankTestItem' in a directory where there are sub directories with spaces in the names, for example "Grade 6" is a sub directory name. When I ...


Home > Database Forum > Operating Systems > shell > How can I search through directories with spaces?

Reply

 

LinkBack Thread Tools Display Modes
  #1  
Old 11-05-2008, 11:01 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default How can I search through directories with spaces?

Hi,

I'm trying to execute this command

find . -name "*.xml" | xargs grep -i 'fillInBlankTestItem'

in a directory where there are sub directories with spaces in the
names, for example "Grade 6" is a sub directory name. When I execute
the above command, I get many errors like

grep: ./Grade: No such file or directory
grep: 7/Skills: No such file or directory

It seems like these problems occur because I have spaces in the
directory titles. Without changing the directory naems, how can I
overcome this?

Thanks, - Dave
Reply With Quote
  #2  
Old 11-06-2008, 04:30 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How can I search through directories with spaces?

On Thursday 6 November 2008 04:01, laredotornado wrote:

> Hi,
>
> I'm trying to execute this command
>
> find . -name "*.xml" | xargs grep -i 'fillInBlankTestItem'
>
> in a directory where there are sub directories with spaces in the
> names, for example "Grade 6" is a sub directory name. When I execute
> the above command, I get many errors like
>
> grep: ./Grade: No such file or directory
> grep: 7/Skills: No such file or directory
>
> It seems like these problems occur because I have spaces in the
> directory titles. Without changing the directory naems, how can I
> overcome this?


In addition to the other ways, you can do

find . -name "*.xml" | xargs -d '\n' grep -i 'fillInBlankTestItem'

that should work fine if you don't have newlines in filenames (which
hopefully is unlikely).
Reply With Quote
  #3  
Old 11-07-2008, 09:44 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How can I search through directories with spaces?

laredotornado wrote:

> I'm trying to execute this command
>
> find . -name "*.xml" | xargs grep -i 'fillInBlankTestItem'
>
> in a directory where there are sub directories with spaces in the
> names, for example "Grade 6" is a sub directory name. When I execute
> the above command, I get many errors like
>
> grep: ./Grade: No such file or directory
> grep: 7/Skills: No such file or directory


The best solution, if your "find" accepts the "+" terminator for -exec,
is:

find . -name "*.xml" -exec grep -i 'fillInBlankTestItem' /dev/null {} +

The '+' makes find do the argument aggregation that xargs would have
done, and has been required by POSIX since 2001. (The /dev/null is to
ensure consistent output if the last grep happens to get called with
one pathname.)

Alternatively you can add quoting to the xargs input, e.g.:

find . -name "*.xml" | sed 's/[^[:alnum:]]/\\&/g' |
xargs -E "" grep -i 'fillInBlankTestItem' /dev/null

but this won't work in the unlikely event that it encounters a
filename with a newline in it.

--
Geoff Clare
Reply With Quote
  #4  
Old 11-07-2008, 10:21 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How can I search through directories with spaces?

2008-11-7, 13:44(+00), Geoff Clare:
[...]
> Alternatively you can add quoting to the xargs input, e.g.:
>
> find . -name "*.xml" | sed 's/[^[:alnum:]]/\\&/g' |
> xargs -E "" grep -i 'fillInBlankTestItem' /dev/null


You could as well use sed 's/./\\&/g'

> but this won't work in the unlikely event that it encounters a
> filename with a newline in it.


In which case, you could use this as given in the xargs man page
from the heirloom toolchest:

find .//. -name '*.xml' -print | sed 's/./\\&/g' | awk '{
if (NR > 1) {
printf("%s", line)
if ($0 !~ /\\\/\\\//)
printf("\\")
printf("\n")
}
line = $0
}
END {
print(line)
}' | xargs -E '' grep -i fillInBlankTestItem /dev/null

--
Stéphane
Reply With Quote
  #5  
Old 11-10-2008, 09:49 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How can I search through directories with spaces?

Stephane CHAZELAS wrote:

> 2008-11-7, 13:44(+00), Geoff Clare:
> [...]
>> Alternatively you can add quoting to the xargs input, e.g.:
>>
>> find . -name "*.xml" | sed 's/[^[:alnum:]]/\\&/g' |
>> xargs -E "" grep -i 'fillInBlankTestItem' /dev/null

>
> You could as well use sed 's/./\\&/g'


That will double the line length, increasing the risk of an input
line being too long for xargs. Although unlikely to be a problem
in practice, POSIX only requires xargs to accept lines up to
LINE_MAX (which can be as low as 2048) bytes in length.

Adding backslash before all non-alphanumerics is a compromise
which does increase this risk slightly, but avoids the quoting
contortions that are needed to add backslash before only the
characters that are special to xargs.

--
Geoff Clare
Reply With Quote
  #6  
Old 11-10-2008, 11:01 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How can I search through directories with spaces?

2008-11-10, 13:49(+00), Geoff Clare:
> Stephane CHAZELAS wrote:
>
>> 2008-11-7, 13:44(+00), Geoff Clare:
>> [...]
>>> Alternatively you can add quoting to the xargs input, e.g.:
>>>
>>> find . -name "*.xml" | sed 's/[^[:alnum:]]/\\&/g' |
>>> xargs -E "" grep -i 'fillInBlankTestItem' /dev/null

>>
>> You could as well use sed 's/./\\&/g'

>
> That will double the line length, increasing the risk of an input
> line being too long for xargs. Although unlikely to be a problem
> in practice, POSIX only requires xargs to accept lines up to
> LINE_MAX (which can be as low as 2048) bytes in length.
>
> Adding backslash before all non-alphanumerics is a compromise
> which does increase this risk slightly, but avoids the quoting
> contortions that are needed to add backslash before only the
> characters that are special to xargs.


OK thanks.

Note that it will double the line length in number of characters
but not necessarily in number of bytes in multi-byte locales.

BTW, I don't think there's any guarantee that the output of find
will be valid in the user's charset. For instance, I've seen
many cases of file names encoded in latin1 while the user has a
UTF8 locale. I wonder how the above command line would behave in
such a case. Maybe a more robust version would be:


find . -name '*.xml' |
LC_ALL=C sed 's/[[:blank:]'\''\"]/\\&/g' |
user_locale=$(locale) LC_ALL=C xargs sh -ac '
eval "$user_locale"
exec grep -i fillInBlankTestItem /dev/null "$@"
' inline "$@"

Which has the adverse side effect of potentially having xargs or
sed display their error messages not in the user's language, so
you might want:

run_with_C_CTYPE() (
set -a
eval "$(locale | sed 's/^LC_CTYPE=.*/LC_CTYPE=C/')"
unset LC_ALL
exec "$@"
)
find . -name '*.xml' |
run_with_C_CTYPE sed 's/[[:blank:]'\''\"]/\\&/g' |
user_locale=$(locale) run_with_C_CTYPE xargs sh -ac '
eval "$user_locale"
exec grep -i fillInBlankTestItem /dev/null "$@"
' inline "$@"

At which point anyone sensible would probably start considering
alternatives to shell scripts...

--
Stéphane
Reply With Quote
  #7  
Old 11-11-2008, 09:53 AM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How can I search through directories with spaces?

Stephane CHAZELAS wrote:

>>> find . -name "*.xml" | sed 's/[^[:alnum:]]/\\&/g' |
>>> xargs -E "" grep -i 'fillInBlankTestItem' /dev/null


[snip]
> BTW, I don't think there's any guarantee that the output of find
> will be valid in the user's charset. For instance, I've seen
> many cases of file names encoded in latin1 while the user has a
> UTF8 locale. I wonder how the above command line would behave in
> such a case.


GNU sed and xargs don't seem to care, but Solaris 10 produces:

xargs: Corrupt input file: Illegal byte sequence

(but no error from sed, strangely).

Yet another reason to avoid using find ... | xargs ...
(or any other use of xargs where the input contains pathnames).

> Maybe a more robust version would be:
>
>
> find . -name '*.xml' |
> LC_ALL=C sed 's/[[:blank:]'\''\"]/\\&/g' |
> user_locale=$(locale) LC_ALL=C xargs sh -ac '
> eval "$user_locale"
> exec grep -i fillInBlankTestItem /dev/null "$@"
> ' inline "$@"
>
> Which has the adverse side effect of potentially having xargs or
> sed display their error messages not in the user's language, so
> you might want:
>
> run_with_C_CTYPE() (
> set -a
> eval "$(locale | sed 's/^LC_CTYPE=.*/LC_CTYPE=C/')"
> unset LC_ALL
> exec "$@"
> )
> find . -name '*.xml' |
> run_with_C_CTYPE sed 's/[[:blank:]'\''\"]/\\&/g' |
> user_locale=$(locale) run_with_C_CTYPE xargs sh -ac '
> eval "$user_locale"
> exec grep -i fillInBlankTestItem /dev/null "$@"
> ' inline "$@"
>
> At which point anyone sensible would probably start considering
> alternatives to shell scripts...


Or would vow to always use "find ... -exec ... {} +" instead
(which means either avoiding old systems whose native find doesn't
support the '+' terminator, or installing GNU find on them).

--
Geoff Clare
Reply With Quote
  #8  
Old 11-12-2008, 03:41 PM
Database Bot
 
Join Date: Sep 2009
Posts: 1,236,254
Database Administrator is on a distinguished road
Default Re: How can I search through directories with spaces?

Geoff Clare wrote:

> Or would vow to always use "find ... -exec ... {} +" instead
> (which means either avoiding old systems whose native find doesn't
> support the '+' terminator, or installing GNU find on them).


or AST find (from AT&T/D.Korn at al) [1], large toolchest;
or the Heirloom toolchest (Gunnar Ritter) [2], large toolchest,
additionally researching portability (SUS, SVRx, 4BSD);
or sfind (from Jörg Schilling) [3], just the find utility.

[1] http://www.research.att.com/sw/download/
[2] http://heirloom.sourceforge.net/tools.html
[3] ftp://ftp.berlios.de/pub/sfind/
Reply With Quote
  #9  
Old 02-23-2010, 02:34 AM
Database Expert
 
Join Date: Feb 2010
Posts: 42
HENARRY JAYA is on a distinguished road
Default Re: How can I search through directories with spaces?

Just ensure that you are inserting the proper disk. CD+R medias are different from the DVD. You drive is just working fine. It happens that it detects a CD+R media on the drive that is why it change into DVD+RAM into CD Drive.
I think you should change another better one, but at first you should note, you can't upload .mswmm video to you tube, .mswmm is windows movie maker project file, not video file, so you should publish the video as wmv video in WMM, and you should note you tube only support some video formats, not all, so you should convert you videos to right you tube video format before upload..
Reply With Quote
Reply

Thread Tools
Display Modes



All times are GMT -4. The time now is 06:06 AM.