Looping through filenames with spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Looping through filenames with spaces
# 1  
Old 11-28-2008
Looping through filenames with spaces

I need to loop through the files in a directory and process the files. But some of the filenames contain spaces.

Here is a little test script I've been using to experiment. (I'm not really going to call 'echo', I'm doing some other processing.) Everything I try fails. How can I do this??
Code:
#!/bin/sh
# spacetest
DIR="/tmp/spacetest$$"
rm -rf $DIR
mkdir -p $DIR || exit 1
touch $DIR/{"a b","c d"}

# This works, when I itemize filenames a priori
set "a b" "c d"
for x in "$@"; do
    echo $x
done

echo Test 1:    # Fails
for x in $(find $DIR -mindepth 1 -printf '"%f" '); do
    echo $x
done

echo Test 2:    # Fails
set $(find $DIR -mindepth 1 -printf '%f ')
for x in "$@"; do
    echo $x
done

echo Test 3:    # Fails
set $(find $DIR -mindepth 1 -printf '"%f" ')
for x in "$@"; do
    echo $x
done

echo Test 4:    # Fails
_list="$(find $DIR -mindepth 1 -printf '"%f" ')"
set $_list
for x in "$@"; do
    echo $x
done

rm -rf $DIR

# 2  
Old 11-28-2008
Quote:
Originally Posted by KenJackson
I need to loop through the files in a directory and process the files. But some of the filenames contain spaces.
...
Code:
#!/bin/sh
# spacetest
DIR="/tmp/spacetest$$"
####
echo Test 1:    # Fails
for x in $(find $DIR -mindepth 1 -printf '"%f" '); do
    echo $x
done


Assuming there are no newlines in any filenames:

Code:
find $DIR -mindepth 1 -printf '"%f" ' |
while IFS= read -r file
do
  echo "$x"
done

# 3  
Old 11-28-2008
Hammer & Screwdriver A couple of thoughts

use of quotes and use of IFS

Code:
> cat see_blanks
ls -l file* 
ls | grep "file" >f_list

while read filename
  do
  echo "### echo"
  echo $filename
  echo "### ls"
  ls $filename
  echo "### ls in quotes"
  ls "$filename"
  echo "### play with IFS"
  OLDIFS=$IFS
  IFS="~"
  ls $filename
  IFS=$OLDIFS

done<f_list

prorgam execution

Code:
> see_blanks
-rw-rw---- 1 xxx dp 0 Nov 28 10:04 file 04
-rw-rw---- 1 xxx dp 0 Nov 28 10:04 file01
### echo
file 04
### ls
ls: cannot access file: No such file or directory
ls: cannot access 04: No such file or directory
### ls in quotes
file 04
### play with IFS
file 04
### echo
file01
### ls
file01
### ls in quotes
file01
### play with IFS
file01

So, if I use the variable inside quotes, it works. And if I reset the IFS setting then I do not break-apart the commands at each space.
# 4  
Old 01-07-2009
I made some progress with input from this forum, thank you, but I'm down to one stick point. Consider this simpler script:
Code:
#!/bin/sh
VAR=
echo 'A A
B B
C C' | while read f; do
    VAR="$VAR:$f"
    echo $f
done
echo "VAR=${VAR}"

The output is
Code:
A A
B B
C C
VAR=

The f variable echos properly in the loop, but I just can't seem to assign it's value to any other variable.
Why not?
# 5  
Old 01-07-2009
Quote:
Originally Posted by KenJackson
I made some progress with input from this forum, thank you, but I'm down to one stick point. Consider this simpler script:
Code:
#!/bin/sh
VAR=
echo 'A A
B B
C C' | while read f; do
    VAR="$VAR:$f"
    echo $f
done
echo "VAR=${VAR}"

The output is
Code:
A A
B B
C C
VAR=

The f variable echos properly in the loop, but I just can't seem to assign it's value to any other variable.
Why not?

Each element of a pipeline is executed in its own subshell and therefore cannot affect the parent shell.

Try echo "$VAR" inside the loop, and you'll see that it does get assigned to.
# 6  
Old 01-08-2009
Quote:
Originally Posted by cfajohnson
Each element of a pipeline is executed in its own subshell and therefore cannot affect the parent shell.
Oh. How terribly inconvenient.

Oh, well. I may have stumbled upon a different solution:
Code:
$!/bin/sh
VAR="$(ls | tr '\n' :)"
echo "VAR=$VAR"

If I have files "A A", "B B" and "C C", I'll get this result:
Code:
VAR=A A:B B:C C:

Now I can work with VAR like this:
Code:
#!/bin/sh
while [ -n "$VAR" ]; do
    f="${VAR%%:*}"
    VAR="${VAR#*:}"
    # process "$f" ...
done

# 7  
Old 01-08-2009
Quote:
Originally Posted by KenJackson
Oh. How terribly inconvenient.

Oh, well. I may have stumbled upon a different solution:
Code:
$!/bin/sh
VAR="$(ls | tr '\n' :)"
echo "VAR=$VAR"


You don't need any external commands to do that:

Code:
VAR=$( printf "%s:" * )

In bash, 3.1 and later, you can do it with:

Code:
printf -v VAR "%s:" *

Quote:
If I have files "A A", "B B" and "C C", I'll get this result:
Code:
VAR=A A:B B:C C:

Now I can work with VAR like this:
Code:
#!/bin/sh
while [ -n "$VAR" ]; do
    f="${VAR%%:*}"
    VAR="${VAR#*:}"
    # process "$f" ...
done


What's wrong with:

Code:
for f in *
do
    : # process "$f" ...
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Handling filenames with spaces

I'm trying to handle some files with spaces in their name using "" or \ . Like "file 1" or file\ 1. My current confusion can be expressed by the following shell script: #!/bin/bash touch "file 1" "file 2" echo -n "ls: " ; ls echo --- for file in "file 1" "file 2" ; do echo $file... (9 Replies)
Discussion started by: Ralph
9 Replies

2. UNIX for Dummies Questions & Answers

Reading filenames with spaces

Hello I've got a certain no. of files in a directory whose names I'm reading and redirecting into a temporary text file using the command below: ls -l | grep ^- | awk '{print $9}'However, whenever the file names contain spaces the above command considers only the part of the file name up to... (5 Replies)
Discussion started by: S. BASU
5 Replies

3. Shell Programming and Scripting

removing spaces in filenames

I have a problem mounting images because of the spaces in the filenames. Does anyone know how to rename files by removing the spaces with the find command? find Desktop/$dir -name "*.dmg" -print -exec ??? (4 Replies)
Discussion started by: ianebaj
4 Replies

4. Shell Programming and Scripting

Moving filenames containing spaces

I want to ftp all the sh files in the directory. Also if any of the file name contains spaces in them, it should be converted to underscores before it is ftped. I wrote the following code below: FILESSH=$(ls /mysh/*.sh) --- FILESH being used here for some other task --- echo "$FILESSH" |... (3 Replies)
Discussion started by: amicon007
3 Replies

5. Shell Programming and Scripting

spaces in filenames

Hi I hope someone will be able to resolve this little teaser! I am running a script for file in `ls directory` do echo "$file" ...other code here.... done this works fine unless we receive a file with a name which has a space in it ie "filena me" (I know its not good... (8 Replies)
Discussion started by: Bab00shka
8 Replies

6. Shell Programming and Scripting

looping thru filenames in a directory

Hi, i am very new to UNIX, i am trying to loop thru the files in a directory. I got the filenames into a variable using $files=`ls` Here $files will contain <filename1> <filename2> <filename3> I want to get one filename at a time and append it to some some text. forexample, ... (1 Reply)
Discussion started by: silas.john
1 Replies

7. Shell Programming and Scripting

spaces in filenames, for do

Hi All, I see similar problems in past threads but so far no answers have worked for me. I am trying to write a script which parses a txt file that contains one filename per line, then finds those files on the local disk and copies them to a specified directory. What I have: ... (4 Replies)
Discussion started by: naviztirf
4 Replies

8. Shell Programming and Scripting

Unix filenames and spaces

I have files on my unix boxes that users have created with spaces. Example: /tmp/project plan ls -l "/tmp/project plan" works fine. $/tmp>ls -l "/tmp/project plan" -rw-r--r-- 1 root other 0 Jan 31 12:32 /tmp/project plan I created a file called test and put just the... (2 Replies)
Discussion started by: x96riley3
2 Replies

9. Shell Programming and Scripting

how to handle spaces in filenames

I'm trying to do something like that: for $filename in `ls -1` do some_command $filename done but it doesn't work properly for file names with spaces, for...in splits at spaces. Anyway around? (4 Replies)
Discussion started by: rayne
4 Replies

10. Shell Programming and Scripting

spaces in filenames

I have a problem with the script below #!/bin/sh for vo in `find -maxdepth 1 -type f -regex "^\./*$"` do ls -l "$vo" some other commands done It works fine until `find ...` returns files with spaces. I've tryed to change IFS but haven't succeed Any solutions? (4 Replies)
Discussion started by: Hitori
4 Replies
Login or Register to Ask a Question