[comp.misc] [] in Unix filenames

magnus@THEP.LU.SE (Magnus Olsson) (09/19/90)

I was going to post the following question to comp.unix.shell, but I'm posting
via mail to ucbvax, and apparently ucbvax hasn't got an alias for that newsgroup
yet, so the letter just bounced. Hence I'm posting to comp.misc instead.

I'm sorry for the inconvenience this may cause all the non-Unix readers of
comp.misc. Please don't flame me, it's not my fault.

_________________________________________________________________________________


Today, I was asked to help a colleague with the following problem:

He'd used ftp to get some files from a VAX under VMS. Now, the VAX files were
in a subdirectory, so he wrote something like

mget [.subdir]*.* 

only to find that his Unix directory had a lot of files called things like

[.subdir]file1
[.subdir]file2

etc (about 20 of them), and that he couldn't access them (since the shell
catches the [] pair).

OK, I thought, I;ll just use the mved script published here some time
ago and do

mved \[*\]= =

(the = is a special wildcard recognized by mved - the command would mean
the same as
mv \[.subdir\]file1 file1
etc.)

But to my surprise, the shell just said "[: no such file or directory".

Actually, I couldn't find out *any* combination of backslashes and or quotes
that enabled me to get what I wanted (what [*] would have meant if [] weren't
special characters).

Finally, I gave up and wrote a C program to do the job.

Is there really no way of telling the shell that I mean
"all files that start with a [, followed by an arbitrary string,
followed by a ], etc? Or is this a bug in the shell (we're using
newcsh)?


Magnus Olsson		     	| \e+ 	   /_	      
Dept. of Theoretical Physics 	|  \  Z	  / q	      
University of Lund, Sweden	|   >----<	      
Internet: magnus@thep.lu.se	|  /	  \===== g    
Bitnet: THEPMO@SELDC52 		| /e-	   \q	      

barmar@think.com (Barry Margolin) (09/20/90)

In article <9009191420.AA10971@thep.lu.se> magnus@THEP.LU.SE (Magnus Olsson) writes:
>Is there really no way of telling the shell that I mean
>"all files that start with a [, followed by an arbitrary string,
>followed by a ], etc? Or is this a bug in the shell (we're using
>newcsh)?

I just created such a file, and then did:

	echo \[*\]*

in /bin/csh on a Sun-4 running SunOS 4.0.3 and it worked fine.

The problem may not be in the shell, but in the mved script.  I'm guessing
it replaces the "=" with "*" in order to find all the matching files; if it
doesn't requote the "[" and "]" then it will lose.

--
Barry Margolin, Thinking Machines Corp.

barmar@think.com
{uunet,harvard}!think!barmar

fuchs@it.uka.de (Harald Fuchs) (09/20/90)

magnus@THEP.LU.SE (Magnus Olsson) writes:
>OK, I thought, I;ll just use the mved script published here some time
>ago and do

>mved \[*\]= =

>(the = is a special wildcard recognized by mved - the command would mean
>the same as
>mv \[.subdir\]file1 file1
>etc.)

>But to my surprise, the shell just said "[: no such file or directory".

>Actually, I couldn't find out *any* combination of backslashes and or quotes
>that enabled me to get what I wanted (what [*] would have meant if [] weren't
>special characters).

>Finally, I gave up and wrote a C program to do the job.

I use a tool called 'rename' for tasks like this:
   rename 's/\[\.subdir\]//' \[*

And here's 'rename':

#!/usr/local/bin/perl
$0 =~ s:.*/::;
die "Usage: $0 action file...\n" unless $#ARGV > 0;
$op = shift;
for (@ARGV) {
  $was = $_;
  eval $op;
  die $@ if $@;
  if ($was ne $_) {
    rename ($was, $_) || die "$0: can't rename $was to $_: $!\n";
  }
}

What? You don't have perl? Ask your next friendly FTP site...
--

Harald Fuchs <fuchs@it.uka.de>