Getting touch with Linux commands (part 4)

Here, we will cover how to move and rename files. Just like “cp” command for copying files as discussed in previous article “Linux commands part 3“, we can use “mv” linux inbuilt command for moving files in Linux operating system. In this article we will discuss most of the useful syntax’s with mv command.

mv : Move command in Linux operating system. 

syntax : mv <options>  <source> <destination>

* source could be one or more sources and destination can be another different filename when renaming. 

eg_1 : How to rename a file

Here, we have created some files with subject names.

But, we need to rename the file named “language” with “english”.  To rename we can use mv command as below.

How to use mv command to rename a file ?

eg_2 : How to move a single / multiple files 

Here, we have created a directory named “marks”.

And we need to move the files ( english, history, maths, science ) to the new directory named “marks”. We use mv command as below.

How to use mv command to move multiple files to a new directory.

Guess, if you need to move only file named  “english”. Then you can use command as below.

mv english marks/ 

eg_3 : How to move a directory to another location

Here, we have created a backup directory for subjects with name “backup_subjects”. Now we have two directories.

Now we need to move the subjects directory with it’s all content to be moved to backup directory. Below is the command.

When moving a directory, we don’t need to move with recursive option like when copying. Please refer the article for “cp command in Linux” for more details.

* Keep in remind, when using mv command, last argument should be the destination. Check the below.

Guess we created new directories named test1, test2, test3.

We run the below command as in the image.

What does the above means ? It moves english, history, maths files and the directory test1 as well as to the directory test2.  So, the last argument should be the destination. Don’t assume that the first 3 files will be moved to the mentioned 2 directories.

You have earned most of basics that must be aware in order to use mv command in Linux. There are more options you can use than the basics. Please go through man pages, info pages, man mv, info mv, mv –help. 

Other Linux commands resources


2 thoughts on “Getting touch with Linux commands (part 4)

Leave a Reply