1. Introduction
In Linux systems, file name modification is a common and important operation. File name modification can better manage files and folders, making them more readable and orderly. By changing the file name, the content and purpose of the file can be clearly expressed, making it easier to quickly identify and locate files. In addition, adjusting file names is conducive to following specific naming rules and conventions, and helping project collaboration and document organization.
However, not all users are familiar with the various methods of file name modification. This article will introduce three commonly used file name modification methods in Linux systems, namely using mv command, rename command, and combining find command and shell script. By learning these three methods, you can easily handle various file name modification requirements and better manage your own file system.
2. Use the mv command to modify the file name
mv
Commands are commands used to move files or rename files under Linux systems. The basic usage is as follows:
Move the file to the specified directory:
mv <source_file> <destination_directory>
For example, move the file to the directory /home/fly/:
mv /home/fly/
Rename the file:
mv <old_file_name> <new_file_name>
For example, rename the file to:
mv
Move multiple files to the specified directory at once:
mv <source_file1> <source_file2> ... <destination_directory>
For example, move files together into directory /home/fly/:
mv /home/fly/
Note: Usemv
When the command moves a file to a different file system,It is actually performing the operation of copying and deleting the source file, not simply moving it。
usemv
The command directly moves the file to a new directory and gives it a new name at the target location, which is equivalent to renaming the file.
Knowledge extension,mv
Some advanced usages of commands:
- Batch modify the prefix or suffix of the file name. Suppose there is a batch of file names starting with "file" and prefix their file names with "new_":
for file in file*; do mv "$file" "new_$file"; done # First, all file names starting with "file" will be obtained and each file name is prefixed with "new_".
- Batch deletes specific parts of the file name. Suppose there is a batch of file names that start with "old", and remove this prefix:
for file in old*; do newname=$(echo $file | sed 's/^old//') mv "$file" "$newname" done # For each file starting with "old", use the sed command to remove the "old" prefix and rename them to a new file name.
- Batch replaces specific strings in file names. Suppose there is a batch of files with "txt", and replace "txt" in their file names with "md":
for file in *txt*; do newname=$(echo $file | sed 's/txt/md/') mv "$file" "$newname" done # Find all file names containing "txt" and replace "txt" with "md".
3. Use the rename command to modify the file name
rename
Commands use Perl regular expressions to match and rename files, usingrename
You need to understand the basic syntax of Perl regular expressions when commanding.
Basic usage:
rename 's/oldstring/newstring/' files
This replaces "oldstring" in the file name with "newstring". For example, if there is a file called "", use the above command to rename it to "".
You can also combine wildcard characters to perform batch renaming:
rename 's/oldstring/newstring/' *
This replaces "oldstring" in all file names in the current directory with "newstring".
Example:
Suppose there is a batch of files starting with "document", and prefix their file names with "new_":
rename 's/^document/new_document/' document*
There is a batch of files starting with "old", and you need to remove this prefix:
rename 's/^old//' old*
Suppose there is a batch of files with "txt", and replace "txt" in their file names with "md":
rename 's/txt/md/' *txt*
4. The difference between mv command and rename command
mv command:
- The mv command is used to move files or rename files. Its basic syntax is
mv source_file target_file
. For example,mv
。 - The mv command can only be used to rename a single file. To rename a file in batches, it is necessary to combine other commands or scripts.
- The mv command does not support regular expressions, and it is not possible to use pattern matching directly in the command to rename files in batches.
Rename command:
- The rename command is used to rename files in batches, and it supports the use of Perl regular expressions to match and rename files. The basic syntax is
rename 's/oldstring/newstring/' files
。 - The rename command can easily rename files in batches, and more complex file name modification operations can be achieved through regular expressions.
Recommended usage:
- If you only need to rename a small number of files or a single file, you can use the mv command to rename it directly.
- If you need to rename files in batches, especially if you need to use regular expressions to perform complex file name modification operations, it is recommended to use the rename command.
- When using the rename command, it is recommended to test the effect of the command first to ensure that the matching result of the regular expression is as expected. Prevents accidental modification of file names resulting in data loss.
5. Use the find command and shell script to modify the file name
Use the find command to find files that meet certain criteria, and then rename the found files in combination with loops in the shell script.
Example:
#!/bin/bash # Use the find command to find a file with the suffix .txt and save it to the files variablefiles=$(find /path/to/search -type f -name "*.txt") # traverse the found filesfor file in $files do # To modify the file name, you can use the mv command or rename command # Here, replace the .txt suffix with .md through the mv command newname=$(echo $file | sed 's/\.txt$/\.md/') mv "$file" "$newname" done
In the example, first use the find command to find all files with suffixes in the specified directory with .txt, and save the found files to the variable files. Then use a loop to traverse each file in the files, modify the file name through the mv command and the sed command, and rename it with the new file name.
flexibility:
- Flexible search conditions: Use the find command to search files according to various conditions, such as file name, file type, file size, etc.
- Flexible modification rules: When using shell scripts to modify file names, different renaming rules can be written according to specific needs, including replacement, adding prefixes, adding suffixes, removing specific characters, etc.
Practicality:
- Batch processing: When you need to modify a large number of file names, it is too cumbersome to modify them manually one by one. Use the find command combined with shell scripts to easily batch process file names that meet certain criteria.
- Automated operations: Combining find commands and shell scripts can automate operations. You can reuse them by writing a script once to avoid repeated labor.
When writing scripts, you also need to consider some boundary situations, such as the file name may contain special characters, spaces, etc., to ensure that the script you write handles these situations.
6. Summary
mv
Advantages of commands: simple and easy to use; it can realize basic file movement and renaming functions.
mv
The disadvantages of the command: The mv command needs to specify the file name and target file name one by one, and is not suitable for large-scale batch processing of files; it can only perform basic renaming operations, and complex renaming rules are difficult to implement.
rename
Advantages of the command: supports simple batch renaming. The rename command can implement simple batch modification of file names, such as replacing strings, adding prefixes and suffixes.
rename
The command disadvantage: The rename command does not support complex regular expression operations when handling complex file name modification rules; it is inflexible.
find
Combination of commandsshell
Advantages of scripts: strong flexibility; can be processed automatically.
find
Combination of commandsshell
Disadvantages of scripts: It requires a certain amount of programming knowledge; important files need to be backed up before performing renaming operations to avoid data loss due to accidental operations.
To sum up, the mv command is suitable for simple movement and renaming operations of a small number of files; the rename command is suitable for simple batch renaming needs; the find command combined with shell scripts is suitable for more complex and flexible batch renaming operations.
The above is the detailed content of the Linux file name modification method. For more information about Linux file name modification, please pay attention to my other related articles!