Moving and Renaming Files and Directories (mv) - Images: Japanese
Hey there, everyone!
This time, we're looking at the 'mv' command — your go-to tool for moving and renaming files and directories.
We've already covered rm and cp, and mv follows the same basic pattern: mv <source> <destination>. That said, the result can vary quite a bit depending on what you specify as the destination, so let's walk through each case one by one.
First, let's create a test file using the touch command.
[root@localhost ~]# touch test.txt [root@localhost ~]# ls test.txt
Now let's run the following command.
[root@localhost ~]# mv test.txt test1.txt
The syntax is mv <source> <destination>, so here we're "moving" test.txt to the same directory but under a different name. Let's check the result with ls.
[root@localhost ~]# mv test.txt test1.txt [root@localhost ~]# ls test1.txt
test.txt is gone and test1.txt has appeared — the file was renamed. To make this even clearer, you can also write it like this:
[root@localhost ~]# mv ./test.txt ./test1.txt
The ./ notation refers to the current directory, so this literally means "move test.txt to the same directory under the name test1.txt" — which is exactly what a rename is.
This might feel a bit strange if you're coming from Windows or macOS, where renaming and moving are separate operations. In UNIX-like systems, both are handled by the mv command. It just takes some getting used to.
By the way, if you try to "move" a file to itself, you'll get an error:
[root@localhost ~]# mv test.txt test.txt mv: `test.txt' and `test.txt' are the same file
Makes sense — it's telling you that source and destination are identical.
Next, let's try moving a file to a different directory. First, create a directory called test.
[root@localhost ~]# mkdir test [root@localhost ~]# ls test test1.txt
To move test1.txt into the test directory while keeping the same filename, do this:
[root@localhost ~]# mv test1.txt test/ [root@localhost ~]# ls test/ test1.txt
If you want to move the file and rename it at the same time, just specify the new name in the destination:
[root@localhost ~]# mv test1.txt test/test.txt [root@localhost ~]# ls test/ test.txt
Here, test1.txt was moved into the test directory and renamed to test.txt in a single step. Worth remembering.
And if you want to bring a file back to the current directory, you can use . (a single dot) as the second argument, which represents the current directory. It's a neat shorthand.
[root@localhost ~]# ls test/ test.txt [root@localhost ~]# mv test/test.txt . [root@localhost ~]# ls test test.txt
Just a single dot and the file lands right where you are. Very handy.
What about moving directories? That works just as easily with mv — no extra options needed.
[root@localhost ~]# ls test.txt test [root@localhost ~]# mv test test1 [root@localhost ~]# ls test.txt test1
Note that rm and cp required the -r option for directories, but mv does not. This is a common point of confusion, so keep it in mind.
And that covers the mv command. In the next article, we'll start looking at vi. See you there!
This article was written by Sakurama.
Author's beloved small mammal |
桜舞 春人 Sakurama HarutoA Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him. |
If you find any errors or copyright issues, please contact us.