[SATLUG] Fun with the mv command

Luis Garza luis at luisgarza.com
Sat Mar 3 14:22:50 CST 2007


Bruce Dubbs wrote:
> RHERMIDA at panam.edu wrote:
>> Gang,
>>
>> One of my co-workers just presented me with an interesting poser:
> what happens when you type the following command in a regular shell
> window:
>>
>> mv test.bs *.sh
>>
>> We are able to see that the file goes away, but we are curious as
> to what really is going on.  And of course, the bonus question: is
> there a way to rename that file back to a more regular name?
>
> It depends on what is in the directory.  Th expansion of *.sh is done by
> the command line interpreter (normally bash) before mv ever gets command
> line to interpret.
>
> You can try it:
>
> $ cd /tmp
> $ for i in 1 2 3 4 5 5; do touch $i.sh; done
> $ touch test.bs
>
> $ echo test.bs *.sh
> test.bs 1.sh 2.sh 3.sh 4.sh 5.sh
> $ mv test.bs *.sh
> mv: target `5.sh' is not a directory
>
> And now, students, why do we get this?
>
> $ ls x
> $ rmdir x
> rmdir: x: Directory not empty
>
>
>   -- Bruce

Well, I think that shell expands the '*' before it executes the mv command.

Thus the command will look like:

mv test.bs 1.sh 2.sh 3.sh 4.sh 5.sh

5.sh is a file not a directory.

Luis



More information about the SATLUG mailing list