File names with spaces
Quoted 5 lines
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:
Quoted 9 lines
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Quoted 8 lines · [email protected] 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}"
Quoted 7 lines
mv "$file" "$newfile"
doneIHMO 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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
In article <[email protected]>,
Quoted 11 lines · <[email protected]> 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"
doneIHMO 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).
Quoted 2 lines
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" ./$newnamedone
Heh... forgot that find will already add in the ./ ... clean up as appropriate.
mrc
Signature · 8 lines
Mike Castle [email protected] 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 [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.linux-learn.org/faqs