Loop with Find—damn spaces!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop with Find—damn spaces!
# 1  
Old 09-19-2011
Loop with Find—damn spaces!

Hi Guys,
I'm trying to find all files with a particular extension and then loop some actions. The problem is that if the files have spaces in their names I get end up being each word as a separate result rather than the entire file.

Code:
ext=".txt"
out=".rtf"
for i in $( find "$1" -name "*$ext" ); do
    path=`dirname "$i"`
    filename=`basename "$i" $ext`
    
    echo "Path: $path"
    echo "Filename: $filename"
    echo "Out: $filename$out"
done

Running the script with:
$ ./scriptname.sh /path/to/folder\ with\ space/

The results would be something like:
Path: /path/to/folder
Filename: with
Out: with.rtf
Path: with
Filename: space
Out: space.rtf
Path: .
Filename: file
Out: file.rtf
Path: .
Filename: of
Out: of.rtf
Path: .
Filename: the
Out: the.rtf
Path: .
Filename: folder
Out: folder.rtf

Any ideas on what I'm doing wrong here?

Regards
# 2  
Old 09-20-2011
# 3  
Old 09-20-2011
Try something like this:

Code:
find "$1" -name "*$ext" | while read fname
do
   path="${fname%/*}"
   base="${fname##*/}"

   echo "path: $path"
   echo "base: $base"
   echo "filename: $fname"
done

Small test I ran in Kshell echoed one line per filename as I think you want.

Last edited by agama; 09-20-2011 at 06:36 PM.. Reason: Corrected happy fingers typo
This User Gave Thanks to agama For This Post:
# 4  
Old 09-20-2011
Hi Agama, Thanks that works well. However $path is showing a a directory two levels up, not the one containing the file. How can I fix this?

I've never see those substitutions [or whatever] you've used in the variables before, Is there anywhere you can point me to to learn more?


Thanks again
# 5  
Old 09-20-2011
Quote:
Originally Posted by imonkey
I've never see those substitutions [or whatever] you've used in the variables before, Is there anywhere you can point me to to learn more?
The Advanced BASH Scripting Guide has a nice table of String Operations which should mostly apply to both modern KSH and BASH.
# 6  
Old 09-20-2011
Just add both of these lines and these "damn spaces" won't bother you any more:
Code:
ext=".txt"
out=".rtf"
OIFS="$IFS"
IFS=""
for i in $(find "$1" -name "*$ext" ); do
     path=`dirname "$i"`
     filename=`basename "$i" $ext`

     echo "Path: $path"
     echo "Filename: $filename"
     echo "Out: $filename$out"
done
IFS="$OIFS"
...

# 7  
Old 09-20-2011
Quote:
Originally Posted by jlliagre
Just add both of these lines and these "damn spaces" won't bother you any more:
Code:
ext=".txt"
out=".rtf"
OIFS="$IFS"
IFS=""
for i in $(find "$1" -name "*$ext" ); do
...

That will not work at all. The entire output of the find command substitution will not undergo field splitting, resulting in one long word containing possibly many pathnames.

If there is no field splitting (whether because IFS is empty or the command substitution is double-quoted), it is pointless to use a loop, since there will only ever be a single word in the for-loop list. The following will yield an identical result:
Code:
i=$(find ...)

Quotes are unnecessary and the value of IFS is irrelevant since during parameter assignment field splitting and pathname expansion are bypassed (as is any operation that can yield additional fields/words).

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop Script and not opening files containing spaces

Hello, I wrote a simple script, that basically wait for a *.dat-file in a certain folder, which is always a zipped file and extracts it. It worked before and i changed nothing in the script, but since last week i have the problem, that it doesnt extract files containing a space. How do i make... (4 Replies)
Discussion started by: blend_in
4 Replies

2. Shell Programming and Scripting

Process files in loop which have spaces in name

I have a folder with files and I have to process them in a loop. However the filenames have space characters, so the list get split. $ touch "File Number_1" $ touch "File Number_2" $ ls "/tmp/File Number"_* /tmp/File Number_1 /tmp/File Number_2 I tried following (sorry for using the... (3 Replies)
Discussion started by: Wernfried
3 Replies

3. AIX

Find and rm files with spaces in the name

I'm sure this has been answered before, but my searches have not turned up the right solution. :confused: I need to remove files in a directory, without descending into subdirectories, older than n days. Some of the filenames contain spaces or other special characters: E10403 (2) E20402 (2)... (15 Replies)
Discussion started by: Papa Lee
15 Replies

4. Shell Programming and Scripting

Loop through array of arrays of string with spaces

Hi I'm trying to loop through an array that contains other arrays and these arrays consist of strings with spaces. The problem is that I can't seem to preserve the spacing in the string. The string with spaces are either divided into multiple items if I change IFS to \n or all the elements of... (4 Replies)
Discussion started by: kidmanos
4 Replies

5. UNIX for Dummies Questions & Answers

Copying files with spaces in the filename in a for loop

Hi all, I've been tangoing with this one for a couple of days now and I'm still not making any progress. Basically I'm trying to match three numbers in a string from a text file with matching numbers in a jpeg, and then copying the results to another folder. Data looks like this: Model:... (4 Replies)
Discussion started by: faceonline
4 Replies

6. Shell Programming and Scripting

Having a for loop read in lines with spaces?

Is this possible? I have a for loop in a shell script reading a list, but I want each line to be a loop, not each thing with a space. Here is the example: HOSTLIST="\ 1.2.3.4 serverA 1.2.3.5 serverB" for NBUHOST in `echo $HOSTLIST` do ssh ${SERVERNAME} "echo "${NBUHOST}"... (3 Replies)
Discussion started by: LordJezoX
3 Replies

7. Shell Programming and Scripting

for loop ( string having spaces )

Dear All, i facing problem to use string having spaces in for loop.. file used for FOR LOOP command.txt rpm -t -v ttm -D -r RJLL -h YELP rpm -t -v ttm -D -r RJLL -h ERRT rpm -t -v ttm -D -r RJLL -h TYYE rpm -t -v ttm -D -r RJLL -h POOL CODE using for execute above command... (3 Replies)
Discussion started by: arvindng
3 Replies

8. Shell Programming and Scripting

problem with for loop and a variable with spaces...Hi

Hi there, I don't understand the following behavior: toto:~$ for word in un "deux trois"; do echo $word; done un deux trois toto:~$ sentence='un "deux trois"' toto:~$ for word in $sentence; do echo $word; done un "deux trois" toto:~$ sentence="un 'deux trois'" toto:~$ for word in... (10 Replies)
Discussion started by: chebarbudo
10 Replies

9. UNIX for Dummies Questions & Answers

For loop using find with file name spaces

Hello All, This question is actually for the service console of VMware ESX 3.5 but is relevant to this forum I think. I have been advised to use the following commands: for i in `find /vmfs/volumes/Test_VMFS/ -name "*.vmx"` do echo "$i" #sed -i 's/scsi1:0.present =... (3 Replies)
Discussion started by: mronsman
3 Replies

10. UNIX for Dummies Questions & Answers

Find fields with no spaces in value

This is what I need to do I have a file that has a field with values like this 1111 2222 3333 4444 55555 666 333333333 444444444 I need for my command to out put only those fields that do not have spaces in them. So my output for the above file would be 333333333 444444444 how... (10 Replies)
Discussion started by: alfredo123
10 Replies
Login or Register to Ask a Question