LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to handle file names with spaces in them (https://www.linuxquestions.org/questions/programming-9/how-to-handle-file-names-with-spaces-in-them-304557/)

bahadur 03-21-2005 10:59 PM

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?

JohnBoy 03-21-2005 11:30 PM

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.

bahadur 03-22-2005 12:38 AM

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.

bahadur 03-22-2005 04:54 AM

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

Marius2 03-22-2005 06:29 AM

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




HTH

bahadur 03-22-2005 06:35 AM

buddy the bad thing is that our teacher has instructed us that any one using the find command will be given a zero

Marius2 03-22-2005 06:59 AM

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.

bullium 03-22-2005 07:12 AM

bahadur which school are you attending?

keefaz 03-22-2005 07:21 AM

I would think you have to play with the Input File Separator env variable (IFS)

sandipan 03-24-2005 07:21 AM

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.

bigearsbilly 03-24-2005 08:38 AM

for file in *;do echo "[$file]";done[/b]

bigearsbilly 03-24-2005 08:39 AM

this catches spaces, newlines anything.

for file in *;do echo "[$file]";done

krodrigu 04-03-2005 09:23 PM

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


bahadur 04-03-2005 09:33 PM

can u plz explain a little how ur code is working?

krodrigu 04-04-2005 12:04 PM

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.


All times are GMT -5. The time now is 02:54 PM.