Rename a file or folder in Linux (mv command and rename Command)
You can use the mv or rename command to rename a file or directory in Linux. Here we will share the usage of both.
The mv command can be renamed, and files or folders can be moved.
Example: Rename directory A to directory B
Mv A B
Example: Move the/a directory to/B and rename it to c
Mv/a/B/c
In text mode, you only need to use the mv command to rename a file or directory. For example, you need to rename a file named abc to 1234:
Mv abc 1234
Note: if there is a 1234 file in the current directory, this file will overwrite it.
The following describes how to use the rename command in a Linux system.
Create multiple file names with similar specifications. [Step 1 of the test]
[Root @ bkjia net] # for I in 'seq-w 10'
> Do touch stu \ _ $ I \ _linux.jpg
> Done
[Root @ bkjia net] # sl
Total 0
-Rw-r -- 1 root 0 Oct 9 21:22 stu_01_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_02_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_03_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_04_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_05_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_06_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_07_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_08_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_09_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_10_linux.jpg
Then, use rename to modify
[Root @ bkjia net] # rename \ _ linux ''' *. jpg
[Root @ bkjia net] # sl
Total 0
-Rw-r -- 1 root 0 Oct 9 21:22 stu_01.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_02.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_03.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_04.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_05.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_06.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_07.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_08.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_09.jpg
-Rw-r -- 1 root 0 Oct 9 21:22 stu_10.jpg
Note: If you want to replace the underline, you cannot add any quotation marks.
Create some test files:
[Root @ bkjia net] # for I in 'seq-w 10'; do touch stu \ _ $ I \ _linux.jpg; done
[Root @ bkjia net] # sl
Total 0
-Rw-r -- 1 root 0 Oct 9 21:58 stu_01_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:58 stu_02_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:58 stu_03_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:58 stu_04_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:58 stu_05_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:58 stu_06_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:58 stu_07_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:58 stu_08_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:58 stu_09_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:58 stu_10_linux.jpg
[Root @ bkjia net] # rename' \ _ linux ''' *. jpg
[Root @ bkjia net] # sl
Total 0
-Rw-r -- 1 root 0 Oct 9 21:27 stu_01_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:27 stu_02_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:27 stu_03_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:27 stu_04_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:27 stu_05_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:27 stu_06_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:27 stu_07_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:27 stu_08_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:27 stu_09_linux.jpg
-Rw-r -- 1 root 0 Oct 9 21:27 stu_10_linux.jpg
If the rename version is in perl, the rename Command supports regular expressions. Therefore, if the rename version is in c, the rename command can be successfully executed, the preceding command fails.
This is a version support issue, so you don't have to go into it. For comparison between the two versions, see the detailed description in batch rename Command rename in linux.
The previous problem may be caused by the unsupported rename version. So, here we will focus on rename, although this command is not very important. But it is actually very useful, isn't it?
Of course, some people will say that you are all under a directory. What if I am under a different directory?
Use two commands. Find the file to be modified and connect it to another pipeline. Isn't a rename finished?
Yes, the above method can be said to be a way of thinking, but there are actually many ways of thinking.
Attachment to view rename version information
[Root @ bkjia net] # man rename | head-1
RENAME (1) Linux Programmer's Manual RENAME (1)
This article permanently updates the link address: