Spaces in File Name issue

Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Spaces in File Name issue
# 1  
Old 10-12-2009
Spaces in File Name issue


I have a small issue while using variables for a file name in UNIX.

I have file name like "abc def 123.txt"
Folder will be in /zzz/xxx/yyy

I created 2 variables Folder = '/zzz/xxx/yyy'
file = 'abc def 123.txt'

While using this in grep command i am getting an error

i used like '$Folder"/"$file'

could u quote me where i am going wrong.I think small tweak will serve the purpose.

But i am missing it somewhere

Thanks in Advance,
San Smilie
# 2  
Old 10-12-2009
Always helps to see the actual script and the actual error message.

Two points:

Folder = '/zzz/xxx/yyy'
file = 'abc def 123.txt'
No spaces before/after the equals sign
file='abc def 123.txt'

Which quotes?
Anything between single quotes is protected from the shell and no substitution will take place. You need double quotes to preserve the spaces but definitely not single quotes when there is a variable involved.

grep "mystring" "$Folder"/"$file"

# 3  
Old 10-12-2009
# 4  
Old 10-12-2009
Thanks for the Help
"$Folder"/"$file" this serve the purpose

I am giving in other way

Thanks again for quick response

# 5  
Old 10-15-2009
I can't take credit for the meat and potatoes of this script, but I can't remember who gave it to me and from what forum. But it does replace : in file names and replaces them with a - and then changes the file extention to dat. This might work for you. I hope this helps. I use this to take files from UNIX and make them readable in DOS, and then clean up after itself. Smilie

cd /home/files
# cp $1 /tmp
cp *DH /tmp
cd /tmp
ls -1 *.DH > /tmp/DH.list
set filelist=`cat /tmp/DH.list`
foreach x ($filelist)
        set newname=`echo $x|sed 's/:/-/g'`
        echo $newname
        cp $x /tmp/$newname
cd /tmp
ls -1 *.DH > /tmp/DH1.list
set filelist=`cat /tmp/DH1.list`
foreach x ($filelist)
        set newname=`echo $x|sed 's/DH/dat/g'`
        echo $newname
        cp $x /tmp/$newname
rm *:*:*.dat
zip dh *.dat
allocate floppy_0 
rm -r /floppy/floppy0/*
cp /floppy/floppy0
deallocate floppy_0
rm *.DH
rm *.dat
rm DH*

Subsitute your file names and spaces.

Last edited by bakunin; 11-03-2009 at 06:26 AM.. Reason: added code-tags
# 6  
Old 10-29-2009
best way to retrieve a variable is to use the below syntax:-


# 7  
Old 11-03-2009
the "${Varname}" is available in bash only.

---------- Post updated at 07:19 AM ---------- Previous update was at 07:18 AM ----------

Originally Posted by ahmad.diab
the "${Varname}" is available in bash only.
sorry not only in bash but also in KSH.

---------- Post updated at 07:22 AM ---------- Previous update was at 07:19 AM ----------

kindly find some details and example below:-

Quoting means just that, bracketing a string in quotes. This has the effect of protecting special characters in
the string from reinterpretation or expansion by the shell or shell script. (A character is "special" if it has an
interpretation other than its literal meaning, such as the wild card character -- *.)
bash$ ls -l [Vv]*
-rw-rw-r-- 1 bozo bozo 324 Apr 2 15:05 VIEWDATA.BAT
-rw-rw-r-- 1 bozo bozo 507 May 4 14:25
-rw-rw-r-- 1 bozo bozo 539 Apr 14 17:11
bash$ ls -l '[Vv]*'
ls: [Vv]*: No such file or directory

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need assistance with a file issue and a terminal issue

Hello everyone, I'm in need of some assistance. I'm currently enrolled in an introductory UNIX shell programming course and, well halfway through the semester, we are receiving our first actual assignment. I've somewhat realized now that I've fallen behind, and I'm working to get caught up, but for... (1 Reply)
Discussion started by: MrMagoo22
1 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

Conversion of spaces Text file into CSV format file

Input file (each line is separaed by spaces )given below: Name Domain Contact Phone Email Location ----------------------- ------------------------------------------------ ------- -----... (18 Replies)
Discussion started by: sreenath1037
18 Replies

4. Shell Programming and Scripting

Issue with spaces in Java command line options

Hi, I am writing a shell script to build Java options dynamically in a variable array and pass them to java.exe. If an option value contains a space, I cannot find a way to get it interpreted correctly. Here is my example: #!/bin/bash JAVA_HOME=/opt/jvm/jre1.5.0_18 JAVA_OPTS=("-Xms256m... (4 Replies)
Discussion started by: Romain
4 Replies

5. Shell Programming and Scripting

File Name with spaces

I need to read pdf files copied in a unix directory.I tried using the for loop with find command but the file names is cutting off at the spaces.Below is the code I tried. for FILENAME in `find $DIR_FILE/ -name "*.pdf" -mindepth 1 -maxdepth 1 -mmin +60 ` ##Needs to be changed later +60 to -60 do... (4 Replies)
Discussion started by: Beena
4 Replies

6. UNIX for Dummies Questions & Answers

cleaning up spaces from fixed width file while converting to csv file

Open to a sed/awk/or perl alternative so that i can stick command into my bash script. This is a problem I resolve using a combination of cut commands - but that is getting convoluted. So would really appreciate it if someone could provide a better solution which basically replaces all... (3 Replies)
Discussion started by: svn
3 Replies

7. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

8. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

9. Shell Programming and Scripting

Directories with spaces in name - looking for help with questions in that issue

I need to work with 'nice' directory names which have some spaces in name, and that brings some questions and not-understanding. Would some expert help me out how to deal with that?! My task is to document some processing, running from some script. I do need to have spaces in directories... (2 Replies)
Discussion started by: alex_5161
2 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question