Process files in loop which have spaces in name


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Process files in loop which have spaces in name
# 1  
Old 04-15-2016
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.

Code:
$ 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 `tcsh`):
Code:
$ foreach file ( `ls "/tmp/File Number"_*` )
foreach? echo ">"$file"<"
foreach? end
>/tmp/File<
>Number_1<
>/tmp/File<
>Number_2<


$ set FILES = ( `ls "/tmp/File Number"_*` )
$ while ( $#FILES )
while? echo ">"$FILES[1]"<"
while? shift FILES
while? end
>/tmp/File<
>Number_1<
>/tmp/File<
>Number_2<

set FILES = "/tmp/File Number"_*
... same as above

Can you help me to solve my problem?

btw, I don't mind to get a solution which renames the files. The files will be renamed anyway.

Best Regards
Wernfried

Last edited by Wernfried; 04-15-2016 at 11:34 AM..
# 2  
Old 04-15-2016
Hello Wernfried,

Could you please try following and let me know if this helps(I haven't tested though.).
Code:
for file in $(find -type f -iname "File Number_*")
do
      do echo $file
done

Thanks,
R. Singh
# 3  
Old 04-16-2016
Quote:
Originally Posted by RavinderSingh13
Hello Wernfried,

Could you please try following and let me know if this helps(I haven't tested though.).
Code:
for file in $(find -type f -iname "File Number_*")
do
      do echo $file
done

Thanks,
R. Singh
Same result:
Code:
./File
Number_2
./File
Number_1

Best Regards

---------- Post updated at 11:25 ---------- Previous update was at 10:40 ----------

I found a working solution:

Code:
for f in /tmp/File\ Number_*; do mv "$f" "${f// /}"; done

Best Regards
# 4  
Old 04-16-2016
Yes, quoting is the solution. Examples with " "
Code:
/tmp/File" "Number_*
"/tmp/File Number_"*
/tmp/"File Number_"*

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. 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

3. UNIX for Dummies Questions & Answers

Writing a loop to process multiple input files by a shell script

I have multiple input files that I want to manipulate using a shell script. The files are called 250.1 through 250.1000 but I only want the script to manipulate 250.300 through 250.1000. Before I was using the following script to manipulate the text files: for i in 250.*; do || awk... (4 Replies)
Discussion started by: evelibertine
4 Replies

4. Shell Programming and Scripting

Loop to process 2 files with same name in different path

Hello forum members, I hope you can help me with this I don't know hot to reach. I have a list of files in "/home/MyPath1/" and in "/home/MyPath2/". The files have the same name in both folders. (but different content, the content doesn't matter here I think) /home/MyPath1/ filename1.txt... (4 Replies)
Discussion started by: Ophiuchus
4 Replies

5. Shell Programming and Scripting

[SOLVED] for loop to process files

I need to process a dirtree containing ms office files such that each file is stored as a variable and also, just the file file stem. Why? They will be using as input and output parameters for another script. For example /path/to/second_script -i filename.docx -o filename Here's what I... (1 Reply)
Discussion started by: graysky
1 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. AIX

loop through the directory for files and sort by date and process the first file

hello i have a requirement where i have a direcotry in which i get files in the format STOCKS.20080114.dat STOCKS.20080115.dat STOCKS.20080117.dat STOCKS.20080118.dat i need to loop through the directory and sort by create date descending order and i need to process the first file. ... (1 Reply)
Discussion started by: dsdev_123
1 Replies
Login or Register to Ask a Question