Users Online

· Guests Online: 41

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

5+ Practical mv Command Usage Examples in Linux

5+ Practical “mv” Command Usage Examples in Linux

This tutorial explains Linux “mv” command, options and its usage with examples.

“mv” command is used To move (rename) files. This post describes “mv” command used in Linux along with usage examples and/or output.

 

Usage:
mv [OPTION]… SOURCE… DIRECTORY

mv (short for move) is a Unix command that moves one or more files or directories from one place to another. Since it can “move” files from one filename to another, it is also used to rename files.

Using mv requires the user to have write permission for the directories the file will move between. This is because mv changes the file’s location by editing the file list of each directory.

Here’s the listing of example usage of “mv” command:

1. To rename a file(mv original_name new_name):

sanfoundry-> ls
mv_command  sample.txt  sam.txt
sanfoundry-> ls -i sam.txt 
591562 sam.txt
sanfoundry-> mv sam.txt sam1.txt 
sanfoundry-> ls -i sam1.txt 
591562 sam1.txt

Here you can see that even after moving the files, it’s inode number remains the same which indicates no change in the content.

2. To rename a Directory(mv original_name new_name):
Just like renaming a file, you can rename a directory using mv command as shown below. This also keeps the inode number of the directory same after renaming.

sanfoundry-> mkdir sam
sanfoundry-> ls -ldi sam
612573 drwxrwxr-x 2 himanshu himanshu 4096 Jul 16 18:59 sam
sanfoundry-> mv sam sam1 
sanfoundry-> ls -ldi sam1
612573 drwxrwxr-x 2 himanshu himanshu 4096 Jul 16 18:59 sam1

Here as we can see tha the innode number, 612573 is remained the same. We here used “-d” option alongwith ls since we want info about
the directories.

3. To prompt for a confirmation before overwriting(mv -i original new):
To avoid ovewritting the existing file before mv command, you might want to get a confirmation from move command before overwriting the destination file using -i option as shown below. You may type either ‘y’ or ‘n’ to accept or reject the move operation.

sanfoundry-> mv -i sam1.txt sample.txt 
mv: overwrite `sample.txt'? y

4. To take a backup of destination before overwriting(mv –suffix=extension oldname newname):
If you want to avoid the overwriting of the file, you mat rename the file before using mv command.

sanfoundry-> ls
sample1.txt  sample2.txt
sanfoundry-> mv --suffix=.backup sample1.txt sample2.txt 
sanfoundry-> ls
sample2.txt  sample2.txt.backup

Here as you can see that sample2.txt files was alreadt there. But using –suffix=.backup had added the extension and renamed it as sample2.txt

5. Move all the files from a folder:

sanfoundry-> ls
1.txt  2.txt
sanfoundry-> mv * ../folder/
sanfoundry-> ls
sanfoundry-> cd ..
sanfoundry-> cd folder/
sanfoundry-> ls
1.txt  2.txt

6. To move only the files that don’t exist in the destination directory(mv -u source destination):

sanfoundry-> cd ../sam/
sanfoundry-> ls
1.txt 
sanfoundry-> ls
1.txt  2.txt  3.txt
sanfoundry-> mv * ../sam/
sanfoundry-> cd ../sam/
sanfoundry-> ls
1.txt  2.txt  3.txt

7. To show the errors:
mv will move the file(s) without prompting even if it is writing over an existing target. Note that this is the default if the standard input is not a terminal.

sanfoundry-> mv -f 1.txt ../sam/
mv: `1.txt' and `../sam/1.txt' are the same file
sanfoundry-> mv -f 11.txt ../sam/
mv: cannot stat `11.txt': No such file or directory

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 0.74 seconds
10,820,502 unique visits