If we want to rename a file in Linux system, we can use mv
and rename
command.
Of course you can also change the name through graphic interface, but using command line is always much faster for me. In addition, the rename command can handle a large number of rename requirements at once.
In the past, I only used mv
to rename the files... If there are multiple files that have to be renamed, I will simply write a shell script to deal with it. But today I learned the rename
command and found that this is the function I want, so I recorded it here along with the mv
command.
mv
command
The mv
command is abbreviation of the so-called move. The main function of this command is move files, but we can also rename files while moving the file path, so it has naturally become our command to rename files.
The use way is:
mv old_file new_file
Let's take an example. Suppose there are four files in my folder:
I can use mv
command to rename the file.
mv 1.test 1.txt
Output:
We can find that we have renamed the original 1.test file to 1.txt.
rename command
The rename
command is divided into two type.
- rename.ul: C Programming Language
- rename: Perl
The method of use is more complicated than mv
, and there are basically three variables.
rename.ul replaced_string new_string match_file
Take the file I just made as an example, I can use the following command to rename all of them:
rename.ul .test .txt *.test
Output:
If you want to use perl version, you need to use the following command to install it:
sudo apt install rename
the rename
command of perl version just only have two parameters:
- match_pattern
- match_files
The rename method is also very similar to the replacement in vim:
rename 's/.txt/.test/' *.txt
Output:
I personally find it very easy to use. How much time did I spend writing mv
renamed shell script...
References
- https://www.howtogeek.com/423214/how-to-use-the-rename-command-on-linux/
- https://unix.stackexchange.com/questions/510553/whats-the-difference-between-rename-and-mv
- https://www.webservertalk.com/rename-files-in-linux
Read More
- [Linux] 各種壓縮、加密壓縮、分割壓縮、解壓縮的指令
- [Linux] 讓使用者加入至某個特定群組 (group)
- [Linux] 使用命令列掛載 USB 裝置
- [Linux] 如何使用指令掛載硬碟
- Linux 複製資料時顯示進度條
- Linux Terminal (終端機) 中資料夾顏色更改
- 在 Linux 使用 tree 指令視覺化資料夾層級
- Linux 底下使用 du 查看檔案大小
- 在 Linux 作業系統底下使用 alias 自定義指令
- 在 Linux 底下使用 Wine 執行 Windows 軟體
- [Linux] Compression, Encrypted compression, Split compression, Decompression
- [Linux] How to add users to a specific group
- [Linux] How to mount a USB device
- [Linux] How to mount a hard disk drive
- [Linux] Using "tree" command to visualize the folder hierarchy
- [Linux] Use "du" command to check the file size