LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-21-2005, 10:59 PM   #1
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Rep: Reputation: 15
how to handle file names with spaces in them


when i use this command

files =ls

any file or directories with spaces in them dont get saved properly in this variable.

like if have a directory named My Documents

only MY will be saved and God knows what happens to Documents.

so how do save the name of the whole document?
 
Old 03-21-2005, 11:30 PM   #2
JohnBoy
Member
 
Registered: Oct 2003
Distribution: Debian, RH, Knoppix, Ubuntu, CentOS
Posts: 62

Rep: Reputation: 15
The trouble is that the spaces need to be escaped. I'm pretty sure the easiest way to do this in a script is to use sed with something like "sed s/ /\ /g". Since I'm new to sed myself bu haven't had a chance yet, to address this problem, I can't offer a working script. I can tell you that there is a perl script named "rename" that exists as a command on some Linux systems. It can be used to replace spaces with something else (e.g. underscore or just deleting the space). I believe it requires a similar syntax.

Hope this helps.
 
Old 03-22-2005, 12:38 AM   #3
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
the problem is that this is a part of our assignment.

and our teacher wants to make life hell for us and wants that we deal with spaces in filenames and directories.
 
Old 03-22-2005, 04:54 AM   #4
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
[jnsahibz@charlie jnsahibz]$ ls -l |awk '/^d/ {print $9}'
CV
Desktop
Favorites
My
assignment
bin
mail
public_html
zakir

[jnsahibz@charlie jnsahibz]$ ls -l
total 5762
drwx------ 2 jnsahibz mc00 512 Mar 14 16:15 CV
drwxr-xr-x 2 jnsahibz mc00 512 Mar 20 22:27 Desktop
drwx------ 3 jnsahibz mc00 512 Feb 17 11:28 Favorites
drwx------ 3 jnsahibz mc00 512 Mar 5 18:14 My Documents
drwx------ 2 jnsahibz mc00 512 Mar 22 16:47 assignment
drwxr-xr-x 3 jnsahibz mc00 512 Mar 22 15:05 bin
-rw------- 1 jnsahibz mc00 2931431 Mar 19 17:21 junaid.zip
drwx------ 2 jnsahibz mc00 512 Mar 19 19:59 mail
drwxr-xr-x 4 jnsahibz mc00 512 Mar 21 19:41 public_html
drwx------ 3 jnsahibz mc00 512 Mar 13 18:45 zakir naik

when the ls is piped into another command. only the first word is recorded in the variable.
 
Old 03-22-2005, 06:29 AM   #5
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276

Rep: Reputation: 31
#!/bin/bash
find . | while read FILE
do
#Now full name including spaces in $FILE
echo $FILE
done




HTH
 
Old 03-22-2005, 06:35 AM   #6
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
buddy the bad thing is that our teacher has instructed us that any one using the find command will be given a zero
 
Old 03-22-2005, 06:59 AM   #7
Marius2
Member
 
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276

Rep: Reputation: 31
Oh stupid me, how could I not think of this. And the assignment itself is very secret, I suppose? Please take care not to be overly specific on it, since we here are all pretty eager to train our ability to read other peoples minds.

BTW you may tell your teacher that stuff like ls | grep | awk | ssh | mencoder | wine | finger | echo | halt is *very* bad style. Never do more than or two things in a single line, don't use a pipe if you can use a loop, and there is no need not to use 'find' if there is one. Always keep things simple and readable.

Last edited by Marius2; 03-22-2005 at 07:05 AM.
 
Old 03-22-2005, 07:12 AM   #8
bullium
Member
 
Registered: Aug 2003
Location: Ohio
Distribution: Ubuntu 12.04, Mint 13, RHES 5.5, RHES 6
Posts: 146

Rep: Reputation: 17
bahadur which school are you attending?
 
Old 03-22-2005, 07:21 AM   #9
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,797

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
I would think you have to play with the Input File Separator env variable (IFS)
 
Old 03-24-2005, 07:21 AM   #10
sandipan
Newbie
 
Registered: Mar 2005
Location: India
Distribution: Red Hat Linux 8.0
Posts: 5

Rep: Reputation: 0
dear bahadur,

the solution is quite easy, u dont need sed or awk.
u can use the following command

ls -l|tr -s " "|cut -d " " -f x-

tr -s " " is used to suppress multiple spaces, so that u can use a single space in the cut command as your delimiter. now x is the column number from where the names of ur files start. thats it hope it solves ur problem. BTW u from india or what.
 
Old 03-24-2005, 08:38 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 240Reputation: 240Reputation: 240
for file in *;do echo "[$file]";done[/b]
 
Old 03-24-2005, 08:39 AM   #12
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516

Rep: Reputation: 240Reputation: 240Reputation: 240
this catches spaces, newlines anything.

for file in *;do echo "[$file]";done
 
Old 04-03-2005, 09:23 PM   #13
krodrigu
LQ Newbie
 
Registered: Jan 2003
Location: denver, colorado, usa
Distribution: rhel,debian,slackware
Posts: 2

Rep: Reputation: 0
using `find` to access files with spaces in their names

Quote:
Originally posted by Marius2
#!/bin/bash
find . | while read FILE
do
#Now full name including spaces in $FILE
echo $FILE
done
thank you, Marius! I found my way here because I was trying to wax a windows installation on a laptop. the owner of the laptop wanted to make sure that no one could read her personal info, so I took your script and embellished a little.


boot knoppix and let it rip. maybe a couple times. of course, windows won't boot, but that's to be expected. it's not so fast, but I think it'll get the job done.

Code:
find /mnt/hda1 -type f | while read FILE
do
    size=$(stat -c %s "$FILE")
    dd if=/dev/random of="$FILE" bs=$size count=1
done
 
Old 04-03-2005, 09:33 PM   #14
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
can u plz explain a little how ur code is working?
 
Old 04-04-2005, 12:04 PM   #15
krodrigu
LQ Newbie
 
Registered: Jan 2003
Location: denver, colorado, usa
Distribution: rhel,debian,slackware
Posts: 2

Rep: Reputation: 0
Quote:
Originally posted by bahadur
can u plz explain a little how ur code is working?
first, I'll say that it looks to me like you're asking us to do your homework for you. at the risk of sounding pedantic, you will learn more if you struggle with this stuff, and not just get the answers handed to you in a forum.

that said, knoppix is a linux distribution that boots from a CD. shove the disk in and restart the computer. very slick. knoppix allows you to mount the existing partitions that exist on the hard drive, and I have mounted the windows partition I'm wiping under /mnt/hda1.

in the script, the output of the find command is piped to the while loop. dd(1) and stat(1) have man pages.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to handle file names with spaces in them bahadur Programming 3 03-23-2005 05:15 AM
du or wc and file names with spaces bramadams Slackware 2 01-27-2005 11:43 AM
Spaces in file names JohnKFT Slackware 3 11-09-2004 03:44 PM
Bash crashes ? File names with () and spaces Danodare Slackware 1 02-27-2004 02:50 PM
File/Direcory Names with Spaces ar1 Linux - General 3 01-15-2004 10:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration