mv command has very featureset, It can be used to rename the file(s) and Directory names, also also used to relocate the contents and help better in organizing the files and directories on a linux OS.
Syntax of mv command:
$ mv [OPTIONS] </path/to/Source> </path/to/Destination>
How to rename a single file
The rename operation is linux is done using the
[vamshi@linuxcent mv]$ ls demo.txt
$ mv demo-today.txt demo-old.txt
[vamshi@linuxcent mv]$ ls demo-old.txt
Here the file
demo-today.txt has been renamed to
How to move or relocate multiple files and directories at once into a Destination Directory
Out DemoProject Directory contains the following content
[vamshi@node02 DemoProject]$ ls api LICENSE mvnw mvnw.cmd README.md
We are only interested to move out only selected directories
admin/ and the file
pom.xml to the target destination
/tmp/Demo-test/, We can achieve this using the option
[vamshi@node02 DemoProject]$ mv -vi core/ site/ admin/ pom.xml -t /tmp/Demo-test/ ‘core/’ -> ‘/tmp/Demo-test/core’ ‘site/’ -> ‘/tmp/Demo-test/site’ ‘admin/’ -> ‘/tmp/Demo-test/admin’ ‘pom.xml’ -> ‘/tmp/Demo-test/pom.xml’
As a result we have successfully moved the selected content:
[vamshi@linuxcent DemoProject]$ ls /tmp/Demo-test/ admin core pom.xml site
Renaming multiple files with extensions
Here’s what we will be demonstrating in this tutorial, We will use a combination of tools like
cut combining them with a
for loop to accomplish our task in an iterative loop.
For simplicity sake let’s consider we have 10 files ending with .txt extension, as seen below
[vamshi@linuxcent ~]$ ls file10.txt file1.txt file2.txt file3.txt file4.txt file5.txt file6.txt file7.txt file8.txt file9.txt
We will now rename them and append an extension of .txt to all the files as demonstrated below:
[vamshi@node02 source]$ for i in *.txt; do sh -c "mv $i `echo $i| cut -d'.' -f1 `.html" ; done
[vamshi@linuxceent ~]$ ls file10.html file1.html file2.html file3.html file4.html file5.html file6.html file7.html file8.html file9.html
rename command to rename the file extensions.
The linux rename command takes the arguments
We have here 10 files with .html extension
[vamshi@linuxcent ~]$ rename .html .doc * [vamshi@linuxcent ~]$ ls file10.doc file1.doc file2.doc file3.doc file4.doc file5.doc file6.doc file7.doc file8.doc file9.doc
While we also might have many other files in another extension format and we can change thrir extension format in the following method.
Suppose have 3 files with .txt extension as file11.txt file12.txt file13.txt and remaining files with .doc extension, they all can be renamed to .html as per the following format.
[vamshi@linuxcent source]$ ls file10.doc file11.txt file12.txt file13.txt file1.doc file2.doc file3.doc file4.doc file5.doc file6.doc file7.doc file8.doc file9.doc
[vamshi@linuxcent ~]$ ls file10.doc file11.txt file12.txt file13.txt file1.doc file2.doc file3.doc file4.doc file5.doc file6.doc file7.doc file8.doc file9.doc
[vamshi@linuxcent ~]$ rename .doc .txt .html * [vamshi@node02 source]$ ls file10.html file11.html file12.html file13.html file1.html file2.html file3.html file4.html file5.html file6.html file7.html file8.html file9.html