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? |
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. |
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. |
[jnsahibz@charlie jnsahibz]$ ls -l |awk '/^d/ {print $9}'
CV Desktop Favorites My assignment bin 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. |
#!/bin/bash
find . | while read FILE do #Now full name including spaces in $FILE echo $FILE done HTH |
buddy the bad thing is that our teacher has instructed us that any one using the find command will be given a zero
|
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. |
bahadur which school are you attending?
|
I would think you have to play with the Input File Separator env variable (IFS)
|
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. |
for file in *;do echo "[$file]";done[/b]
|
this catches spaces, newlines anything.
for file in *;do echo "[$file]";done |
using `find` to access files with spaces in their names
Quote:
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 |
can u plz explain a little how ur code is working?
|
Quote:
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. |