Spaces in filenames located in variables in shell.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Spaces in filenames located in variables in shell.
# 1  
Old 11-07-2010
Spaces in filenames located in variables in shell.

Greetings.

I am trying to do a script that will do some file copying for me. Unfortunately I have spaces in the directory names (which I cannot change) and the result is someone hard to achieve in shell scripts. I have searched everywhere on the web but does not manage to find the answer to this:

Code:
orig_dir='/Applications/My cool app/'$1'/'
`cp $orig_dir'file1' .`

(the above example is just a fake one to get help with my actual problem).

It will generate the error:

Code:
cp: /Applications/My: No such file or directory

I have tried with \, with \\ and with \\\. I also found an article on the web that claimed that "My' 'cool' 'app" should work so I tried that too. Nothing works.

Help?
# 2  
Old 11-07-2010
Hi, welcome to the forum. Perhaps you could try this:
Code:
orig_dir="/Applications/My cool app/$1"
cp "$orig_dir/file1" .

# 3  
Old 11-07-2010
Impressive!

It worked! Smilie

... but I got another problem Smilie

So, now it works with the primary copying, but the secondary copying has * in it, and that doesn't work now...

Code:
orig_dir="/Applications/My cool app/$1/"
file_to_copy="$orig_dir$one_single_file"
# This works, yea, thanks!:
cp -v "$file_to_copy" .

file_array=( "lotsoffiles1*" "lotsoffiles2\*" "lotsoffiles3\\*" lotsoffiles4\\\* )
for a_bunch_of_files in ${file_array[@]}
do
	file_to_copy="$orig_dir$a_bunch_of_files"
	cp -v "$file_to_copy" .
done

The second cp (inside the for loop), hates me. As you can see in the file_array I tried lots of different tactics, with these results:

Code:
/Applications/My Cool App/andersdirectory/lotsoffiles1*: No such file or directory.
/Applications/My Cool App/andersdirectory/lotsoffiles2\*: No such file or directory.
/Applications/My Cool App/andersdirectory/lotsoffiles3\*: No such file or directory.
/Applications/My Cool App/andersdirectory/lotsoffiles4\*: No such file or directory.

Can you (or anyone) solve that too?

---------- Post updated at 11:23 AM ---------- Previous update was at 11:18 AM ----------

Noticed that I had forgot "-signs on nr. 4 so I tested:

Code:
file_array=( "lotsoffiles1*" "lotsoffiles2\*" "lotsoffiles3\\*" "lotsoffiles4\\\*" )

Which resulted in:

Code:
/Applications/My Cool App/andersdirectory/lotsoffiles1*: No such file or directory.
/Applications/My Cool App/andersdirectory/lotsoffiles2\*: No such file or directory.
/Applications/My Cool App/andersdirectory/lotsoffiles3\*: No such file or directory.
/Applications/My Cool App/andersdirectory/lotsoffiles4\\*: No such file or directory.

And:

Code:
file_array=( lotsoffiles1* lotsoffiles2\* lotsoffiles3\\* lotsoffiles4\\\* )

which gave:

Code:
/Applications/My Cool App/andersdirectory/lotsoffiles1*: No such file or directory.
/Applications/My Cool App/andersdirectory/lotsoffiles2*: No such file or directory.
/Applications/My Cool App/andersdirectory/lotsoffiles3\*: No such file or directory.
/Applications/My Cool App/andersdirectory/lotsoffiles4\*: No such file or directory.

---------- Post updated at 11:36 AM ---------- Previous update was at 11:23 AM ----------

AHA! Smilie

Removed the * in the Array and did:

Code:
cp -v "$file_to_copy"* .

Inside the loop worked wonders.

Thanks for the help! Smilie
# 4  
Old 11-07-2010
Try this:
Code:
orig_dir="/Applications/My cool app/$1/"
file_array=( "$origdir"lotsoffiles[1-4]* )
for file in "${file_array[@]}"; do
  cp -v "$file" .
done

Edit: cool. you found out yourself...
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. Shell Programming and Scripting

White spaces issue with shell variables

Hi all, I've a requirement as below Source file src.txt sample data: A<10 white spaces>B12<5 white spaces>C<17 white spaces> A1<5 white spaces>B22<5 white spaces>C13<17 white spaces> when I'm fetching a record from this file into a shell variable like below: vRec=`head -1 src.txt... (2 Replies)
Discussion started by: madhu_1126
2 Replies

3. Shell Programming and Scripting

awk and spaces in filenames

Hey there, this is my first post and I'll try to explain my situation as best I can.Here is a sample of the input file: ADO Sample.h,v ADO Sample 2010-05-21 lyonsb /repository/patents/TSCommon/OpenSource/Dundass/ug6mfc/DataSources/Ado/ADO Sample ADO SampleDoc.h,v ADO SampleDoc 2010-05-21... (3 Replies)
Discussion started by: rodan90
3 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

How to list filenames with spaces in shell script

Hi, I want to list all the files matching in a directory directory given below Here one of the folder has a space in the path. /MAS02/RMS/WBDev/Krishna/Krishna Mohan/FMSplitByKeyAfterChanges1000075383.fileman.*.txt.out.1000075383.0 The following works from command line... (1 Reply)
Discussion started by: hikrishn
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