* File names with spaces
@ 2003-02-17 19:11 Theo. Sean Schulze
2003-02-17 20:34 ` Brian Jackson
0 siblings, 1 reply; 7+ messages in thread
From: Theo. Sean Schulze @ 2003-02-17 19:11 UTC (permalink / raw)
To: linux-newbie
Hello,
I am trying to write a bash shell script that will translate spaces in file names into underline characters. This is the script as I have it now:
for file in `ls`
do
echo $file
newfile=`ls ${file} | tr '[:space:]' '[_*]'`
echo File is named ${file}
echo The new file is named ${newfile}
# [[ -s $newfile ]] || (mv $file $newfile)
sleep 2
done
The lines that begin with echo and the sleep line are for debugging. What they have shown me is that the $file is getting set to the first word in the file name on the first iteration, the second word on the second interation, etc. (The file names look like "001 of 150 files", "002 of 150 files", etc.) So, on the first iteration, $file is egual to "001", on the second iteration $file is equal to "of", etc. Yet, if I go to the directory and issue `ls`, the filenames are shown as one would expect with the whole four word filename on one line.
Can anyone give me a hint on how to fix this so that the whole filename is loaded into $file?
TIA,
Sean
--
Theo. Sean Schulze
tschulze@teamfinders.org
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: File names with spaces
2003-02-17 19:11 Theo. Sean Schulze
@ 2003-02-17 20:34 ` Brian Jackson
2003-02-18 21:37 ` Theo. Sean Schulze
0 siblings, 1 reply; 7+ messages in thread
From: Brian Jackson @ 2003-02-17 20:34 UTC (permalink / raw)
To: Theo. Sean Schulze, linux-newbie
You can try to adapt this example from The Advanced Bash Scripting Guide:
http://www.tldp.org/LDP/abs/html/moreadv.html#EX57
It deletes the file, but it shouldn't be too hard to adapt to your needs.
--Brian
On Monday 17 February 2003 01:11 pm, Theo. Sean Schulze wrote:
> Hello,
>
> I am trying to write a bash shell script that will translate spaces in file
> names into underline characters. This is the script as I have it now:
>
>
> for file in `ls`
> do
> echo $file
> newfile=`ls ${file} | tr '[:space:]' '[_*]'`
> echo File is named ${file}
> echo The new file is named ${newfile}
> # [[ -s $newfile ]] || (mv $file $newfile)
> sleep 2
> done
>
> The lines that begin with echo and the sleep line are for debugging. What
> they have shown me is that the $file is getting set to the first word in
> the file name on the first iteration, the second word on the second
> interation, etc. (The file names look like "001 of 150 files", "002 of 150
> files", etc.) So, on the first iteration, $file is egual to "001", on the
> second iteration $file is equal to "of", etc. Yet, if I go to the
> directory and issue `ls`, the filenames are shown as one would expect with
> the whole four word filename on one line.
>
> Can anyone give me a hint on how to fix this so that the whole filename is
> loaded into $file?
>
> TIA,
> Sean
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: File names with spaces
@ 2003-02-18 9:36 robin
2003-02-18 13:59 ` Jim Reimer
2003-02-28 23:46 ` Mike Castle
0 siblings, 2 replies; 7+ messages in thread
From: robin @ 2003-02-18 9:36 UTC (permalink / raw)
To: linux-newbie
> I am trying to write a bash shell script that
> will translate spaces in file names into
> underline characters. This is the script as I
> have it now:
>
> for file in `ls`
> do
> echo $file
> newfile=`ls ${file} | tr '[:space:]' '[_*]'`
> echo File is named ${file}
> echo The new file is named ${newfile}
> # [[ -s $newfile ]] || (mv $file $newfile)
> sleep 2
> done
A solution should look like this:
for file in `ls -1`; do
newfile=`echo "$file" | sed 's/ /_/'`
echo "File is named ${file}"
echo "The new file is named ${newfile}"
mv "$file" "$newfile"
done
IHMO in the main-loop it is better to choose "ls -1", so the field
separator is \n and there's only one filename in each line.
The next <big> thing is to put the filename into quotations. Now a
filename, even with spaces, will be interpreted as one word.
Hope it helps,
Robin
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: File names with spaces
2003-02-18 9:36 File names with spaces robin
@ 2003-02-18 13:59 ` Jim Reimer
2003-02-28 23:46 ` Mike Castle
1 sibling, 0 replies; 7+ messages in thread
From: Jim Reimer @ 2003-02-18 13:59 UTC (permalink / raw)
To: robin; +Cc: linux-newbie
robin@robind.de wrote:
>
> A solution should look like this:
>
> for file in `ls -1`; do
> newfile=`echo "$file" | sed 's/ /_/'`
> echo "File is named ${file}"
> echo "The new file is named ${newfile}"
>
> mv "$file" "$newfile"
> done
>
> IHMO in the main-loop it is better to choose "ls -1", so the field
> separator is \n and there's only one filename in each line.
> The next <big> thing is to put the filename into quotations. Now a
> filename, even with spaces, will be interpreted as one word.
Robin, that still doesn't work right - try it and see:
$echo > file\ 001
$echo > file\ 002
$ ./test.sh
File is named file
The new file is named file
File is named 001
The new file is named 001
File is named file
The new file is named file
File is named 002
The new file is named 002
$
The previously referenced Bash Scripting Guide has the answer.
Change the for statement to read:
for file in *; do
and it will work.
$ ./test.sh
File is named file 001
The new file is named file_001
File is named file 002
The new file is named file_002
$
-jdr-
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: File names with spaces
2003-02-17 20:34 ` Brian Jackson
@ 2003-02-18 21:37 ` Theo. Sean Schulze
2003-02-20 10:22 ` J.
0 siblings, 1 reply; 7+ messages in thread
From: Theo. Sean Schulze @ 2003-02-18 21:37 UTC (permalink / raw)
To: linux-newbie
Thanks, that did help, although it didn't solve the problem. I now recognize that the problem is in assigning the variable. Both my version with ls and the version with find in the example give the expected results when printing to the console, but they both fail when used to assign a string including spaces to a variable.
I need to find a way to maintain the integrity of the string as I assign it to the file variable. I tried `echo (ls -1)` and `echo "(ls -1)"`, but neither works. Changing the parentheses to brackets doesn't help either.
Cheers,
Sean
On Mon, Feb 17, 2003 at 02:34:16PM -0600, Brian Jackson hunted and pecked out:
> You can try to adapt this example from The Advanced Bash Scripting Guide:
> http://www.tldp.org/LDP/abs/html/moreadv.html#EX57
>
> It deletes the file, but it shouldn't be too hard to adapt to your needs.
>
> --Brian
>
> On Monday 17 February 2003 01:11 pm, Theo. Sean Schulze wrote:
> > Hello,
> >
> > I am trying to write a bash shell script that will translate spaces in file
> > names into underline characters. This is the script as I have it now:
> >
> >
> > for file in `ls`
> > do
> > echo $file
> > newfile=`ls ${file} | tr '[:space:]' '[_*]'`
> > echo File is named ${file}
> > echo The new file is named ${newfile}
> > # [[ -s $newfile ]] || (mv $file $newfile)
> > sleep 2
> > done
> >
> > The lines that begin with echo and the sleep line are for debugging. What
> > they have shown me is that the $file is getting set to the first word in
> > the file name on the first iteration, the second word on the second
> > interation, etc. (The file names look like "001 of 150 files", "002 of 150
> > files", etc.) So, on the first iteration, $file is egual to "001", on the
> > second iteration $file is equal to "of", etc. Yet, if I go to the
> > directory and issue `ls`, the filenames are shown as one would expect with
> > the whole four word filename on one line.
> >
> > Can anyone give me a hint on how to fix this so that the whole filename is
> > loaded into $file?
> >
> > TIA,
> > Sean
>
--
Theo. Sean Schulze
tschulze@teamfinders.org
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: File names with spaces
2003-02-18 21:37 ` Theo. Sean Schulze
@ 2003-02-20 10:22 ` J.
0 siblings, 0 replies; 7+ messages in thread
From: J. @ 2003-02-20 10:22 UTC (permalink / raw)
To: linux-newbie
On Tue, 18 Feb 2003, Theo. Sean Schulze wrote:
> Thanks, that did help, although it didn't solve the problem.
> I now recognize that the problem is in assigning the variable.
> Both my version with ls and the version with find in the example give
> the expected results when printing to the console,
> but they both fail when used to assign a string including spaces to a
> variable. I need to find a way to maintain the integrity of the string
> as I assign it to the file variable. I tried `echo (ls -1)` and `echo
> "(ls -1)"`, but neither works. Changing the parentheses to brackets
> doesn't help either.
>
> Cheers,
> Sean
Assigning variables does not happen in the `ls -1' statement. This only
generates strings. You need a command that read's the variables correctly.
Use `read', as in:
ls -1 | while read file ; do echo "$file" ; done
or
find . -type f | while read file ; do
echo "$file"
done
or more exotic, array version.
IFS=$'\n' eval 'lines=( $(cat < $file) )'
# now you have a array of lines.... which can be proccessed further..
These all where tested and work just fine.
G00d lUcK
J.
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: File names with spaces
2003-02-18 9:36 File names with spaces robin
2003-02-18 13:59 ` Jim Reimer
@ 2003-02-28 23:46 ` Mike Castle
1 sibling, 0 replies; 7+ messages in thread
From: Mike Castle @ 2003-02-28 23:46 UTC (permalink / raw)
To: linux-newbie
In article <20030218093623.1FB578D8FE@basicbox3.server-home.net>,
<robin@robind.de> wrote:
>for file in `ls -1`; do
> newfile=`echo "$file" | sed 's/ /_/'`
> echo "File is named ${file}"
> echo "The new file is named ${newfile}"
>
> mv "$file" "$newfile"
>done
>
>IHMO in the main-loop it is better to choose "ls -1", so the field
>separator is \n and there's only one filename in each line.
ls should detect that stdout is not a terminal and fall back to -1
automatically. Granted, it can't hurt, but should be unnecessary.
However, a few comments.
First, why use ls at all? Might as well use:
for file in *; do
Of course, since you are only interested in files with spaces in them,
while not limit to that in the first place?
mcastle@dl-mcastle[03:37pm]~/foo(776) for file in *; do echo $file; done
a b
bar
mcastle@dl-mcastle[03:37pm]~/foo(777) for file in *\ *; do echo $file; done
a b
Of course, if you have a lot of files, this technique simply doesn't work
as you'll overflow your max command line length.
You could go back to:
ls | while read; do
But you're processing every file again.
Another alternative might be something like:
find -name '* *' -maxdepth 1
I think maxdepth is gnu find specific, so keep that in mind (I don't have
access to any non-gnu systems to test).
>The next <big> thing is to put the filename into quotations. Now a
>filename, even with spaces, will be interpreted as one word.
Not just quotes, but prepend ./ too, in case any of your file names look
like "- -"
mcastle@dl-mcastle[03:45pm]~/foo(793) find
.
./bar
./a b
./blah
./blah/c d
./ns
./- -
mcastle@dl-mcastle[03:45pm]~/foo(794) ./ns
`././a b' -> `././a_b'
`././- -' -> `././-_-'
mcastle@dl-mcastle[03:45pm]~/foo(795) cat ns
#!/bin/bash
find -name '* *' -maxdepth 1 | while read name; do
newname=$(echo $name | tr ' ' '_')
mv -iv "./$name" ./$newname
done
Heh... forgot that find will already add in the ./ ... clean up as
appropriate.
mrc
--
Mike Castle dalgoda@ix.netcom.com www.netcom.com/~dalgoda/
We are all of us living in the shadow of Manhattan. -- Watchmen
fatal ("You are in a maze of twisty compiler features, all different"); -- gcc
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2003-02-28 23:46 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-02-18 9:36 File names with spaces robin
2003-02-18 13:59 ` Jim Reimer
2003-02-28 23:46 ` Mike Castle
-- strict thread matches above, loose matches on Subject: below --
2003-02-17 19:11 Theo. Sean Schulze
2003-02-17 20:34 ` Brian Jackson
2003-02-18 21:37 ` Theo. Sean Schulze
2003-02-20 10:22 ` J.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox