Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "mv file1 *.file1" do?

Tags:

linux

bash

mv

When I issue the command

mv file1 *.file1

in the directory where file1 is located, it disappears. Where does it end up? I know * is a valid character in file names under Linux and that I should have escaped the * character to have the expected result like

mv file1 \*.file1

but if didn't escape it, where was it moved to?

like image 848
marekful Avatar asked Feb 17 '23 09:02

marekful


1 Answers

The result of mv file1 *.file1 depends on what is matched by *.file1

  • If *.file1 matches nothing, then file1 is renamed to *.file1
  • If *.file1 matches exactly one file, then file1 is renamed to the name of the matched file, and the matched file is lost.
  • If *.file1 matches exactly one file, and that file is a directory, then file1 is moved to the matched directory.
  • If *.file1 matches more than one files, and the last file matched is a directory, then file1, and all matched files (except this directory) will be moved to the directory.
  • If *.file1 matches more than one files, and the last file matched is NOT a directory, then mv will fail with an error.

See experiment below:

$ cat /etc/*-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"
NAME="Ubuntu"
VERSION="12.04.2 LTS, Precise Pangolin"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.04.2 LTS)"
VERSION_ID="12.04"
$ ls
$ touch file1
$ ls
file1
$ mv file1 *.file1
$ ls
*.file1
$ touch file1
$ ls
file1  *.file1
$ mv file1 *.file1
$ ls
*.file1
$ touch 1.file1
$ touch file1
$ mv file1 *.file1
mv: target `*.file1' is not a directory
$ ls
1.file1  file1  *.file1
$ mkdir z.file1
$ mv file1 *.file1
$ ls z.file1/
1.file1  file1  *.file1
like image 67
user000001 Avatar answered Feb 20 '23 00:02

user000001