To rename a file in Linux, you can use the mv
command.
The mv
command stands for "move," and it is used to move files and directories from one location to another. However, it can also be used to rename a file by moving it to the same directory with a different name.
For example, to rename the file oldfile.txt
to newfile.txt
, you can use the following command:
mv oldfile.txt newfile.txt
This will rename the file oldfile.txt
to newfile.txt
. If the file is in a different directory, you can specify the path to the file as well.
For example:
mv /path/to/oldfile.txt /path/to/newfile.txt
This will rename the file oldfile.txt
in the /path/to
directory to newfile.txt
.
You can also use the mv
command to rename multiple files at once by specifying multiple source and destination files. For example:
mv file1.txt file2.txt file3.txt /path/to/
This will move the files file1.txt
, file2.txt
, and file3.txt
to the /path/to
directory.
It's important to note that the mv
command will overwrite any existing files with the same name. If you want to avoid overwriting an existing file, you can use the -n
option to prevent the mv
command from overwriting existing files.
For example:
mv -n file1.txt file2.txt file3.txt /path/to/
This will move the files file1.txt
, file2.txt
, and file3.txt
to the /path/to
directory, but will not overwrite any existing files with the same name.
Finally, you can use the -v
option to display a verbose output of the mv
command, which will show the names of the files being moved.
For example:
mv -v file1.txt file2.txt file3.txt /path/to/