How do I get a list of filenames with spaces on bash?
How do I get a list of filenames with spaces on bash?
am 24.01.2008 05:42:46 von grocery_stocker
A long time ago, some BioChemisty Professor at Vanderbilt University
used some kind of 'echo' combination on Unix to get a list of filename
with space. I no longer have a copy of the script. I tried looking for
his webpage that had the information, but he web page has long since
vanished.
Does anyone know this magical combination?
I was tried something like the following
[cdalten@localhost ~]$ echo */
aaa-TIJ3-distribution/ apue/ award-1.0/ backup/ class/ college/
Desktop/ doc/ electric-fence-2.1.13/ fuckwork/ gcc-4.2.0/ hello-2.1.1/
idlekill/ ispell-3.3.02/ krc/ lib/ libevent-1.3a/ libevent-1.3b/
orville-write-2.55/ party-2.12/ perl/ phrack/ porn/ psybnc/
putty-0.60/ RCS/ rubyshit/ shells/ shellshit/ socat-2.0.0-b1/ src/
structure/ tor-0.1.2.14/ tsocks-1.8/ unpv12e/ valgrind-3.2.3/ viagra/
worldpac/
But that only gives me directory listings.
Re: How do I get a list of filenames with spaces on bash?
am 24.01.2008 07:42:27 von Bill Marcum
On 2008-01-24, Chad wrote:
>
>
> A long time ago, some BioChemisty Professor at Vanderbilt University
> used some kind of 'echo' combination on Unix to get a list of filename
> with space. I no longer have a copy of the script. I tried looking for
> his webpage that had the information, but he web page has long since
> vanished.
>
> Does anyone know this magical combination?
>
> I was tried something like the following
>
> [cdalten@localhost ~]$ echo */
>
> But that only gives me directory listings.
echo *\ *
Re: How do I get a list of filenames with spaces on bash?
am 24.01.2008 08:17:00 von jellybean stonerfish
On Wed, 23 Jan 2008 20:42:46 -0800, Chad wrote:
> A long time ago, some Professor used some kind of 'echo' combination on
> Unix to get a list of filename with space. I no longer have a copy of
> the script.
> I was tried something like the following
> [cdalten@localhost ~]$ echo */
>
In bash you could use
[cdalten@localhost ~]$ ls *\ *
Note that is a \ not a /
The \ makes the shell treat the following space like a regular
character, not a word separator.
If you prefer to use echo
[cdalten@localhost ~]$ echo *\ *
Would also work, but the filenames would all come
out on one line, making them hard to read.
What do you want to do, just list filenames with spaces, or do something
to the filenames with spaces?
Re: How do I get a list of filenames with spaces on bash?
am 24.01.2008 14:45:36 von grocery_stocker
On Jan 23, 10:42 pm, Bill Marcum wrote:
> On 2008-01-24, Chad wrote:
>
>
>
> > A long time ago, some BioChemisty Professor at Vanderbilt University
> > used some kind of 'echo' combination on Unix to get a list of filename
> > with space. I no longer have a copy of the script. I tried looking for
> > his webpage that had the information, but he web page has long since
> > vanished.
>
> > Does anyone know this magical combination?
>
> > I was tried something like the following
>
> > [cdalten@localhost ~]$ echo */
>
> > But that only gives me directory listings.
>
> echo *\ *
Yes. That is the magical combination. Thank you.
Re: How do I get a list of filenames with spaces on bash?
am 24.01.2008 14:49:40 von grocery_stocker
On Jan 23, 11:17 pm, jellybean stonerfish
wrote:
> On Wed, 23 Jan 2008 20:42:46 -0800, Chad wrote:
> > A long time ago, some Professor used some kind of 'echo' combination on
> > Unix to get a list of filename with space. I no longer have a copy of
> > the script.
> > I was tried something like the following
> > [cdalten@localhost ~]$ echo */
>
> In bash you could use
>
> [cdalten@localhost ~]$ ls *\ *
>
> Note that is a \ not a /
> The \ makes the shell treat the following space like a regular
> character, not a word separator.
>
> If you prefer to use echo
>
> [cdalten@localhost ~]$ echo *\ *
>
> Would also work, but the filenames would all come
> out on one line, making them hard to read.
>
> What do you want to do, just list filenames with spaces, or do something
> to the filenames with spaces?
I was trying to rename all the filenames with spaces in them.
Re: How do I get a list of filenames with spaces on bash?
am 24.01.2008 16:00:27 von Ed Morton
On 1/24/2008 7:49 AM, Chad wrote:
> On Jan 23, 11:17 pm, jellybean stonerfish
> wrote:
>
>>On Wed, 23 Jan 2008 20:42:46 -0800, Chad wrote:
>>
>>>A long time ago, some Professor used some kind of 'echo' combination on
>>>Unix to get a list of filename with space. I no longer have a copy of
>>>the script.
>>>I was tried something like the following
>>>[cdalten@localhost ~]$ echo */
>>
>>In bash you could use
>>
>>[cdalten@localhost ~]$ ls *\ *
>>
>>Note that is a \ not a /
>>The \ makes the shell treat the following space like a regular
>>character, not a word separator.
>>
>>If you prefer to use echo
>>
>>[cdalten@localhost ~]$ echo *\ *
>>
>>Would also work, but the filenames would all come
>>out on one line, making them hard to read.
>>
>>What do you want to do, just list filenames with spaces, or do something
>>to the filenames with spaces?
>
>
> I was trying to rename all the filenames with spaces in them.
Assuming you want to replace single blank chars with underscores:
for f in *\ *
do
n="${f// /_}"
[ ! -f "$n" ] &&
cp -- "$f" "$n" &&
rm -- "$f"
done
If your file names can have multiple spaces or tabs or... you may need a
modified solution.
Regards,
Ed.
Re: How do I get a list of filenames with spaces on bash?
am 24.01.2008 17:32:00 von Stephane CHAZELAS
On Thu, 24 Jan 2008 09:00:27 -0600, Ed Morton wrote:
[...]
> for f in *\ *
> do
> n="${f// /_}"
> [ ! -f "$n" ] &&
[ -f ... ] test whether the file exists *and* is a regular file
(or symlink to regular file). What if it exists and is a
directory?
> cp -- "$f" "$n" &&
> rm -- "$f"
> done
[...]
Why not mv? cp won't work great for non-regular files.
A better way to avoid overwriting files is to use the -i option
of cp/mv. See also (set -C; cat < in > out)
--
Stephane
Re: How do I get a list of filenames with spaces on bash?
am 24.01.2008 18:04:29 von Ed Morton
On 1/24/2008 10:32 AM, Stephane Chazelas wrote:
> On Thu, 24 Jan 2008 09:00:27 -0600, Ed Morton wrote:
> [...]
>
>>for f in *\ *
>>do
>> n="${f// /_}"
>> [ ! -f "$n" ] &&
>
>
> [ -f ... ] test whether the file exists *and* is a regular file
> (or symlink to regular file). What if it exists and is a
> directory?
The OP said "I was trying to rename all the filenames with spaces in them." and
I took him literally. If you look at the list of dirs he got with his echo
command earlier in the thread you'll see that while some have very interesting
names, none contain spaces or underscores so they won't clash with the new file
names all of which will contain underscores.
>
>> cp -- "$f" "$n" &&
>> rm -- "$f"
>>done
>
> [...]
>
> Why not mv? cp won't work great for non-regular files.
To ensure that in the event of a failure the original file is unaffected.
> A better way to avoid overwriting files is to use the -i option
> of cp/mv. See also (set -C; cat < in > out)
"cp -i" can still fail if the target file is unwritable and any use of "-i"
means you have to sit around while the scripts running which may not be desirable.
Ed.
Re: How do I get a list of filenames with spaces on bash?
am 24.01.2008 18:38:26 von Stephane CHAZELAS
On Thu, 24 Jan 2008 11:04:29 -0600, Ed Morton wrote:
[...]
>>> cp -- "$f" "$n" &&
>>> rm -- "$f"
[...]
>> Why not mv? cp won't work great for non-regular files.
>
> To ensure that in the event of a failure the original file is unaffected.
in that case, mv doesn't affect the file not even its inode, it
affects the current directory, it's just a rename. A failing
rename just leaves things as they are. A failing cp may leave an
unfinished copy around.
Also note that cp may not preserve file attributes like
permissions, ACLs, ownership, time stamps... (see -p option for
some of those).
>
>> A better way to avoid overwriting files is to use the -i option
>> of cp/mv. See also (set -C; cat < in > out)
>
> "cp -i" can still fail if the target file is unwritable and any use of "-i"
Yes, which is the intention, if the target file exists, writable
or not, you want cp to fail.
> means you have to sit around while the scripts running which may not be desirable.
[...]
If the answer is always no, then:
cp -i -- "$f" "$n" <> /dev/null 2>&0
But the problem is that "cp" may not return a non-zero exit
status if the file is not copied (POSIX is ambiguous about that
and at least GNU and Solaris cp return 0 if the user doesn't
answer yes), so cat with set -C may be a better solution.
--
Stephane
Re: How do I get a list of filenames with spaces on bash?
am 25.01.2008 00:16:01 von Ed Morton
On 1/24/2008 11:38 AM, Stephane Chazelas wrote:
> On Thu, 24 Jan 2008 11:04:29 -0600, Ed Morton wrote:
> [...]
>
>>>> cp -- "$f" "$n" &&
>>>> rm -- "$f"
>>>
> [...]
>
>>>Why not mv? cp won't work great for non-regular files.
>>
>>To ensure that in the event of a failure the original file is unaffected.
>
>
> in that case, mv doesn't affect the file not even its inode, it
> affects the current directory, it's just a rename. A failing
> rename just leaves things as they are.
Huh, I didn't realise that "mv" was totally safe. Then I'd just use:
for f in *\ *
do
n="${f// /_}"
[ ! -f "$n" ] &&
mv -- "$f" "$n"
done
Thanks for the tip.
Ed.
Re: How do I get a list of filenames with spaces on bash?
am 25.01.2008 04:48:41 von grocery_stocker
On Jan 24, 9:38 am, Stephane Chazelas
wrote:
> On Thu, 24 Jan 2008 11:04:29 -0600, Ed Morton wrote:
>
> [...]
>
> >>> cp -- "$f" "$n" &&
> >>> rm -- "$f"
> [...]
> >> Why not mv? cp won't work great for non-regular files.
>
> > To ensure that in the event of a failure the original file is unaffected.
>
> in that case, mv doesn't affect the file not even its inode, it
> affects the current directory, it's just a rename. A failing
> rename just leaves things as they are. A failing cp may leave an
> unfinished copy around.
>
> Also note that cp may not preserve file attributes like
> permissions, ACLs, ownership, time stamps... (see -p option for
> some of those).
>
And mv will preserve file attributes?
Re: How do I get a list of filenames with spaces on bash?
am 25.01.2008 10:47:20 von Stephane CHAZELAS
On Thu, 24 Jan 2008 19:48:41 -0800 (PST), Chad wrote:
[...]
> And mv will preserve file attributes?
As I said, mv only updates the directory because it does a
rename(2) system call in that case.
A directory is like a phone directory, it's a definitely more
accurate term than "folder".
It's a list of
- name -> phone number
the phone number identifies a phone line. If you send a mail to
the directory publisher that you have changed your name, the
directory publisher won't change your phone line, just your
entry in the dictory.
For files,
- name -> inode number
it's the same thing.
Doing "mv foo bar" only edits the directory entry to change the
name associated with the inode.
And it's meant to be an atomic operations.
Note that there are cases where mv doesn't do a rename(2)
because it can't. That's when you're moving a file from to a
different file system. Of course, in that case a new file has to
be created in the new filesystem. So it's like a cp followed by
a rm. But even then mv won't remove the original file if it
doesn't manage to create the copy.
--
Stephane