Linux mv command: rename files

table of Contents

description

Usage example

Rename file

Move file location

Move file and change name

View the command manual

Precautions

Move non-existent files or directories

Target path does not exist

A file/directory with the same name exists in the target path


 

description

In Linux, renaming files is called moving. The mv command can move files and directories to another location or rename them.

 

Usage example

Rename file

mv <old_file_name> <new_file_name>

For example, rename flavor.pb.go in the current directory to flavor.go:

$ ll | grep flavor
-rw-r--r--    1 root  staff  35644  6 11 12:57 flavor.pb.go
$ mv flavor.pb.go flavor.go
$ ll | grep flavor
-rw-r--r--    1 root  staff  35644  6 11 12:57 flavor.go

❗ Note: Moving the file will change the file name from flavor.pb.go to flavor.go, but the inode number and timestamp remain unchanged. This is because mv only affects file names.

 

Move file location

You can also use mv to move the file location. mv <file_name> <target_directory>

We move /home/cloudadmin/test.log to the /tmp directory:

$ ll
total 4
drwxr-xr-x. 3 cloudadmin root        4096 Aug 28 14:48 cache
-rw-r-----. 1 cloudadmin cloudadmin     0 Sep 21 15:30 test.log
$ mv test.log /tmp/
$ ll
total 3
drwxr-xr-x. 3 cloudadmin root        4096 Aug 28 14:48 cache
$ ll /tmp
total 3
-rw-r-----. 1 cloudadmin cloudadmin   0 Sep 21 15:30 test.log

As in the previous section, this operation does not change the inode number or timestamp of the file.

 

Move file and change name

Use mv <file> <new_file_name> to move the file and modify the file name.

$ ll
total 4
drwxr-x---. 2 root root 4096 Oct 28 17:10 desktop
-rw-r-----. 1 root root    0 Oct 28 17:10 test.c
$
$
$ mv test.c desktop/run.c
$ cd desktop/
$ ll
total 0
-rw-r-----. 1 root root 0 Oct 28 17:10 run.c

In the same way, mv only affects the file name at this time, and the inode number and timestamp remain unchanged.

 

View the command manual

Use man mv to query the mv command manual.

$ man mv

 

Precautions

Move non-existent files or directories

When trying to move a file or directory that does not exist, mv reports an error No such file or directory.

$ ll
total 0
-rw-r-----. 1 admin admin 0 Oct 28 17:10 run.c
$ mv main.go ../
mv: cannot stat ‘main.go’: No such file or directory

Target path does not exist

When the target path to be moved does not exist, mv reports an error Not a directory.

$ mv run.c $HOME/func/
mv: cannot move ‘run.c’ to ‘/home/admin/func/’: Not a directory

A file/directory with the same name exists in the target path

For example, suppose you want to move a file named "run.c" in the current directory to the desktop directory. But a file named "run.c" already exists in the desktop directory. If you use mv to move at this time, mv will move run.c in the current directory to the desktop directory and overwrite the existing ../desktop/run.c file.

$ ll
total 4
drwxr-x---. 2 admin admin 4096 Oct 28 17:11 desktop
-rw-r-----. 1 admin admin    0 Oct 28 17:28 run.c
$ mv run.c desktop/run.c
$ cd desktop/
$ ll
total 0
-rw-r-----. 1 admin admin 0 Oct 28 17:28 run.c

Observing the timestamp before and after the file in the above example, you can know that the file has been replaced. So how to prevent files from being accidentally replaced?

We can use the -i parameter of the mv command. So when the command tries to overwrite an existing file, you will get a prompt:

$ ll
total 4
drwxr-x---. 2 admin admin 4096 Oct 28 17:29 desktop
-rw-r-----. 1 admin admin    0 Oct 28 17:31 run.c
$ mv -i run.c desktop/
mv: overwrite ‘desktop/run.c’?

 

Guess you like

Origin blog.csdn.net/TCatTime/article/details/107431436