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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
03-21-2005, 10:59 PM
|
#1
|
Member
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141
Rep:
|
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?
|
|
|
03-21-2005, 11:30 PM
|
#2
|
Member
Registered: Oct 2003
Distribution: Debian, RH, Knoppix, Ubuntu, CentOS
Posts: 62
Rep:
|
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.
|
|
|
03-22-2005, 12:38 AM
|
#3
|
Member
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141
Original Poster
Rep:
|
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.
|
|
|
03-22-2005, 04:54 AM
|
#4
|
Member
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141
Original Poster
Rep:
|
[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.
|
|
|
03-22-2005, 06:29 AM
|
#5
|
Member
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276
Rep:
|
#!/bin/bash
find . | while read FILE
do
#Now full name including spaces in $FILE
echo $FILE
done
HTH
|
|
|
03-22-2005, 06:35 AM
|
#6
|
Member
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141
Original Poster
Rep:
|
buddy the bad thing is that our teacher has instructed us that any one using the find command will be given a zero
|
|
|
03-22-2005, 06:59 AM
|
#7
|
Member
Registered: Jan 2004
Location: Munich
Distribution: SuSE 9.2, 10.2, 10.3, knoppix
Posts: 276
Rep:
|
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.
|
|
|
03-22-2005, 07:12 AM
|
#8
|
Member
Registered: Aug 2003
Location: Ohio
Distribution: Ubuntu 12.04, Mint 13, RHES 5.5, RHES 6
Posts: 146
Rep:
|
bahadur which school are you attending?
|
|
|
03-22-2005, 07:21 AM
|
#9
|
LQ Guru
Registered: Mar 2004
Distribution: Slackware
Posts: 6,797
|
I would think you have to play with the Input File Separator env variable (IFS)
|
|
|
03-24-2005, 07:21 AM
|
#10
|
Newbie
Registered: Mar 2005
Location: India
Distribution: Red Hat Linux 8.0
Posts: 5
Rep:
|
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.
|
|
|
03-24-2005, 08:38 AM
|
#11
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
for file in *;do echo "[$file]";done[/b]
|
|
|
03-24-2005, 08:39 AM
|
#12
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,516
|
this catches spaces, newlines anything.
for file in *;do echo "[$file]";done
|
|
|
04-03-2005, 09:23 PM
|
#13
|
LQ Newbie
Registered: Jan 2003
Location: denver, colorado, usa
Distribution: rhel,debian,slackware
Posts: 2
Rep:
|
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
|
|
|
04-03-2005, 09:33 PM
|
#14
|
Member
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141
Original Poster
Rep:
|
can u plz explain a little how ur code is working?
|
|
|
04-04-2005, 12:04 PM
|
#15
|
LQ Newbie
Registered: Jan 2003
Location: denver, colorado, usa
Distribution: rhel,debian,slackware
Posts: 2
Rep:
|
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 03:24 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|