Visit Our UNIX and Linux User Community


Copying files with spaces in the filename in a for loop


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Copying files with spaces in the filename in a for loop
# 1  
Old 02-10-2012
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:
CAM-123/500
CAM-2787/600
...
CAM-1197/600

And the files are numbered like so:

VS-123 (1).jpg
VS-2787 (2).jpg
...
VS-1197 (3).jpg

Now I've already used grep to create two lists, one of all the unique CAM numbers and one of all the matching jpeg files, and I'm able to run both of those through grep so I'm left with the filenames that I need to copy, and that's where it falls down: when I try and cp the files with spaces it splits it onto two lines.

Here's what I'm doing:

Code:
for i in `grep -Eo '[0-9]{3,4}' list-2.txt`; do j=`grep -E $i list-3.txt`; k=`echo "$j"`; cp "$k" cameras ; done;

And my output is to the tune of

Code:
cp: cannot stat `VS-D210-1 (1).jpg\nVS-D210-1 (2).jpg\nVS-D210-1 (3).jpg\nVS-D210-1.jpg': No such file or directory

I've tried while loops, using printf... I'd like to know where I'm going wrong and why "$k" still outputs on to two lines... Smilie<--- Where I am Smilie

Nuts thing is, once I've got this cracked, the project is pretty much done!

Anyways, thanks in advance!
# 2  
Old 02-10-2012
Quote:
for i in `grep -Eo '[0-9]{3,4}' list-2.txt`; do j=`grep -E $i list-3.txt`; k=`echo "$j"`; cp "$k" cameras ; done;
The "for" command is unsuitable for open-ended lists or for parameters containing spaces or for lists which might be empty. A "while" loop is the preferred method:

Turning your script round and putting quotes round "$i" . Untested because my "grep" doesn't have these options.
Code:
grep -Eo '[0-9]{3,4}' list-2.txt | while read i
do
      grep -E "$i" list-3.txt | while read j
      do
              cp "$j" cameras
      done
done

To my mind $k is not needed because it is the same as $j.

Last edited by methyl; 02-10-2012 at 12:00 PM.. Reason: mispaste
# 3  
Old 02-10-2012
The trouble is grep -Eo '[0-9]{3,4} wil also match the numbers after the /
This can also be done with plain shell:

Code:
while IFS="-/" read x cam x
do
  while read file
  do 
    case $file in
       *$cam*) cp "$file" cameras
    esac
  done < list3
done < list2

But this reads list3 multiple times. This would be more efficient for example:

Code:
while IFS="-/" read x cam x
do
  cameras="$cameras $cam"
done < list2

while read file
do 
  for cam in $cameras
  do
    case $file in
      *$cam*) cp "$file" cameras
    esac
  done
done < list3


Last edited by Scrutinizer; 02-10-2012 at 12:45 PM..
# 4  
Old 02-10-2012
Thanks for the responses:

@methyl trying it your way and doing this (removing $k as you rightly pointed out):


Code:
grep -Eo '[0-9]{3,4}' list-2.txt | while read i; do j=`grep -E "$i" list-3.txt`; cp "$j" cameras; done

produced errors like this:

Code:
cp: cannot stat `VS-D210-1 (1).jpg\nVS-D210-1 (2).jpg\nVS-D210-1 (3).jpg\nVS-D210-1.jpg': No such file or directory

Somehow it was outputting the '\n'.

@Scrutinizer I have yet to try your method, but I will post back the results, although I do have a question about the "done < list2"/"done < list3" notation... is this something in bash scripting or you commenting something?
# 5  
Old 02-10-2012
@faceonline this is something in shell scripting, it is input redirecting from the files named list2 and list3
 

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. Shell Programming and Scripting

Creating Multiple data files with spaces in the filename

Hi, I have a list of filenames in the format with Spaces in the filename. As an example : Sample File 1.txt Sample File 2.txt Sample File 3.txt.....I have about 100 files like this. I am trying to create a block of code or use an available command to a) Create a file b) Put in some... (2 Replies)
Discussion started by: ban3rj33
2 Replies

4. Shell Programming and Scripting

Issue with copying files into dir inside for loop

Hi , I'm trying to move/copy the files inside the loop into a directory . I tried the below code and the issue is the data is not copying into the created directory but the files are copying into another file file_path="/home/etc" Last_Day=20130930 mkdir $file_path/ARC_${Last_Day} ... (3 Replies)
Discussion started by: smile689
3 Replies

5. Solaris

While copying files from windows to Solaris5.8 with filezilla length of the filename changed

Hello I am copying a tar from windows7 to Solaris with filezilla i could see, some characters in the name of the file get deleted. I could see only substring of the filename eg: original filename :abcdefghijklmnopqrstu.h i can see like abcdefghijklm solution to this problem is highly... (2 Replies)
Discussion started by: Revathi R
2 Replies

6. Shell Programming and Scripting

Rename all files (filename with spaces) to different extension

Hi, I have files with filenames as below. SGM Daily Sales Email-en-us-05312012.xlwa I want to rename it in .xls. I am writing a script to change this, as there can be multiple files in subfolders. I have the following script. #!/bin/ksh for oldfile in $(find... (1 Reply)
Discussion started by: mac4rfree
1 Replies

7. Shell Programming and Scripting

Copying files using $filename

Hi, I've a problem. Here is the code: #!/bin/ksh echo "enter a file name" read a cd /home/linux1/sam if then echo "file exists" cp $a $a_bkp else echo "file doesn't exist" fi when executed the o/p is: enter a file name contact file exists cp: missing destination file (4 Replies)
Discussion started by: sam_bd
4 Replies

8. Shell Programming and Scripting

List files with spaces in filename

I have a text file containing files in a directory structure i.e. /project/hr/raw/jcpdatav/datav_aug03 /project/hr/raw/jcpdatav/comb8121sep02n /project/hr/raw/jcpdatav/datav_feb04_ons /project/hr/raw/jcpdatav/corpsick_jun06 /project/hr/raw/jcpdatav/jcpjoiners200507... (3 Replies)
Discussion started by: mr_crosby
3 Replies

9. Shell Programming and Scripting

copying files with spaces in the filename

Hi I am having difficulty copying files from one dir to another due to a space in the names of the file with an extension .rtf There are a group of files and the command am using is cp `ls -rt /wlblive/home/whiops/ops/RTFs/*.rtf|head -20` /wlblive/home/jamshed Since the files are... (3 Replies)
Discussion started by: jamshedj
3 Replies

10. Shell Programming and Scripting

moving files with spaces in filename from one directory to another

Hello, When I run following script #!/bin/bash cd ~/directory1 mv `ls -trF | grep -v / | tail -10 ` ~/directory2 works fine with filenames not having any space but runs into issues with filenames that have spaces tried with $file variable still doesnot work. Can someone help me (4 Replies)
Discussion started by: asakhare
4 Replies

Featured Tech Videos