[LINUX] Rename and move files (directories) with mv command

With the mv command ① Change file (directory name) ② Move file (directory)

You can perform two types of processing.

Rename file (directory)

$ mv <File before change(directory)Name> <File after change(directory)Name>

Example) When renaming the file test.txt to the file name hoge.txt

$ mv  test.txt  hoge.txt

In addition, when the command is executed in the above example If the hoge.txt file already exists, the original hoge.txt will be overwritten as soon as you rename test.txt. One way to prevent this is to add the -i option. This will allow you to be asked "Are you sure you want to overwrite?" When executing the mv command.

$ mv  -i  test.txt  hoge.txt

There are other file overwrite protection options, so I've listed them at the end of this article.

Also, since you can only change the file name one by one with the mv command, use the rename command when you want to change multiple files at once.

Move files (directories)

$ mv  <File(directory)Name>  < 移動先のdirectoryName>

Example) When moving the file sample.txt to the directory dir

$ mv  sample.txt  dir

By the way, it is safer to add /. to the destination directory to make it dir / .. This /. means "directly under the directory". The reason is that if the dir directory doesn't exist when you run the command "Move the sample.txt file to the dir directory", then the sample.txt file is simply named __ This is because it behaves as if it is changed to file __. The syntax for renaming files $ mv <file (directory) name before change> <file (directory) name after change> Has been applied, hasn't it? Therefore, it is safe to type the command as follows.

$ mv  sample.txt  dir/.

Move multiple files (directories) together to a directory

You can also move multiple files with the mv command.

Example) When moving sample01.txt, sample02.txt, sample03.txt together to a directory called dir

$ mv  sample01.txt  sample02.txt  sample03.txt  dir/.

Move files (directories) up one level in the current directory

$ mv  <File(directory)Name>  ../

List of main options for mv command

option Description
If there is a file with the same name in the destination, make a backup and then overwrite it.
If there is a file with the same name in the destination, forcibly overwrite it
If there is a file with the same name in the move destination, overwrite it and check if it is OK
File with the same name in the destination(directory)Do not overwrite if there is
Display detailed information when performing move processing

Recommended Posts

Rename and move files (directories) with mv command
Linux: files and directories
Linux (about files and directories)
mv command Move up one level
Upload & move GCS files with Go
Check and move directories in Python
Save and retrieve files with Pepper
Edit and save read-only files with vim
Read and write csv files with numpy
Reading and writing JSON files with Python
mv command
Drag and drop local files with Selenium (Python)
Compile and run Rust with a single command
Download and import files with Splunk external python
Reading and writing fits files with Python (memo)
How to use the grep command to recursively search directories and files to a specified depth
Maintain directory structure on Linux and move old files
Extract files from EC2 storage with the scp command
Read and write files with Slackbot ~ Bot development with Python ~
Recursively search for files and directories in Python and output
Give the history command a date and time and collect the history files of all users with a script