spaces in filenames

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting spaces in filenames
# 1  
Old 07-05-2002
spaces in filenames


I hope someone will be able to resolve this little teaser!

I am running a script

for file in `ls directory`
echo "$file"
...other code here....

this works fine unless we receive a file with a name which has a space in it


"filena me"

(I know its not good to have a space in a filename but we have no control over the filenames which are ftp'd to us.)

If the file "filena me" is in the directory, the above script will do the following:

filena other stuff....

me other stuff....

whereas I would like it to do

filena me other stuff....

Can anyone help me please?

Helen Smilie
# 2  
Old 07-05-2002
for file in `ls directory`
cd directory
for file in *
# 3  
Old 07-05-2002

Thanks Perderabo, This works fine!Smilie
# 4  
Old 07-06-2002
1 more solution

u can also use

for fine in `ls directory | tr -s " "`
# 5  
Old 09-29-2008
filenames starting with . and having spaces in filename

I am trying to run the following

for fname in *
echo $fname

to list all the files in a specific directory. This does not seem to include the files starting with . character (e.g .classpath)

then i modified the program to include ls -A

for fname in $(ls -A)
echo $fname

This is including the . files. But it splits the file containing spaces into multiple entries (Hello name.txt => Hello, name.txt)

Just wondering is there a way to modify the * option to include the hidden files.

Any help is very much appreciated.

# 6  
Old 09-29-2008
for name in * .* ; do

Get the spacing right! First asterisk is surrounded by spaces, 2nd is not.
# 7  
Old 09-29-2008
Think the other way round...

Hi Ben,
I think "ls -A" is the best way to go, using "ls * .*" will also list contents of directories, even above, and I guess You don't want that. Well, just a guess anyway.

One of the beauties of ls (and many other utilities) is that when piped to another program it will regard each file/entry as a line, as opposed to when written to screen where it will all appear continuously, also when produced as a string for the "for fname in " construct.

So if You use "while" and try something like this instead,

ls -A | while read fname ; do echo $fname ; done

maybe You will be a bit closer to a solution? It of course depends on what You want to do with the names, are the entries interesting only if they are files, then use something like
ls -A|while read line;do [ -f "$line" ]  && echo $line;done

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. UNIX for Dummies Questions & Answers

Reading filenames with spaces

Hello I've got a certain no. of files in a directory whose names I'm reading and redirecting into a temporary text file using the command below: ls -l | grep ^- | awk '{print $9}'However, whenever the file names contain spaces the above command considers only the part of the file name up to... (5 Replies)
Discussion started by: S. BASU
5 Replies

3. Shell Programming and Scripting

Remove spaces in filenames

Hi, I have files like below, In files coming as spaces. Before transfering those files into ftp server. I want to remove the spaces and then can transfer the files into unix server. e.g: filenames are 1) SHmail _profile001_20120908.txt 2) SHmail_profile001 _20120908.txt 3) sh... (3 Replies)
Discussion started by: kirankumar
3 Replies

4. Shell Programming and Scripting

removing spaces in filenames

I have a problem mounting images because of the spaces in the filenames. Does anyone know how to rename files by removing the spaces with the find command? find Desktop/$dir -name "*.dmg" -print -exec ??? (4 Replies)
Discussion started by: ianebaj
4 Replies

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

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

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