Lesson 11: Moving (renaming) files¶
Moving files¶
Now, let's assume that we want to move these files to a new directory
('planets'). We will do this using the mv
command demonstrating, that
either a single or multiple file(s) can be moved in one command:
[learner learning_linux]$ mkdir planets
[learner learning_linux]$ mv mars.txt planets/
[learner learning_linux]$ mv earth.txt planets/
[learner learning_linux]$ mv jupiter.txt heaven.txt planets/
[learner learning_linux]$ ls
planets
[learner learning_linux]$ ls planets/
earth.txt heaven.txt jupiter.txt mars.txt
Note
We will remove 'heaven.txt' in the next step as it is not a planet!
For the mv
command, we always have to specify a source file (or
directory) that we want to move, and then specify a target location,
similar to cp
. The following commands would have moved all files to
the 'planets' directory in a single command:
mv *.txt planets/
mv *t planets/
mv {mars,earth,heaven,jupiter}.txt planets/
The asterisk *
acts as a wild-card
character, essentially
meaning 'match anything'. The second example works because there are no
other files or directories in the directory that end with the letter 't'
(if there were, then they would be moved too). Using wild-card
characters can save you a lot of typing.
Renaming files¶
Similarly to moving directories, moving files is also achieved using the
mv
command. If the target location is not a directory, the file will
be renamed.
A file can be moved and renamed in one command. Let's create a new file to and move it to the 'planets' directory:
[learner learning_linux]$ touch template_planet.txt
[learner learning_linux]$ mv template_planet.txt planets/neptune.txt
[learner learning_linux]$ ls
planets
[learner learning_linux]$ ls planets/
earth.txt heaven.txt jupiter.txt mars.txt neptune.txt