- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to move a file, group of files, and directories in Linux?
The mv (move) command is used to move one or more files or directories from one directory to another directory using terminal in the Linux/Unix operating system. After using the mv command file is copied from source to destination and source file is removed. The mv command is also used to rename the file.
The general syntax of the mv command is as follows
$ mv [OPTION]...[-T] SOURCE DESTINATION... $ mv [OPTION]...SOURCE... DIRECTORY... $ mv [OPTION]...-t DIRECTORY SOURCE...
Brief description of options available in the mv command.
|Sr.No.||Option & Description|
Create backup of destination file
Create backup file without argument
Do not prompt before overwriting
Prompt before overwriting files
Not overwrite an existing file
Move all source arguments into directory
Treat destination as a normal file
Move when source file is newer than the destination file either destination file is missing
Prompt what is happening
Displays a help message and then exits.
It gives info about the version and then exits.
To move a file from a directory and paste it into another directory use the mv command as shown in below.
$ mv file.txt ../snow
Here snow is a directory in which a file will be moved from the current directory after executing the above command.
To move an entire directory to another directory using the mv command without any option the syntax is as follows.
$ mv <SOURCE DIRECTORY> <DESTINATION DIRECTORY>
Suppose snow is a directory which has to move to a shadow directory.
$ mv snow shadow
And the directory will be moved.
Here, we will move a file as well as create a backup file of an existing file in the destination directory that will be used to recover the previous file.
First of all, using the cat command we create a file file.txt and write some contents
$ cat >snow/file.txt
And move into another directory but another file with the same name available in the snow directory then a backup file is created in the snow directory.
$ mv --backup file.txt snow
We can see it using ls -a because just using the ls command hidden files will not prompt.
$ ls -a
- How to copy a file, group of files, or directory in Linux?
- How to Protect Files and Directories from Deleting in Linux
- How to Copy a File to Multiple Directories in Linux?
- How to Find a Specific String or Word in Files and Directories in Linux
- Find the Largest Top 10 Files and Directories On a Linux
- How to remove files and directories in the Linux operating system using the terminal?
- How to add, delete, change ownership (of files) of a group in Linux?
- How to change the file owner and group in Linux?
- How to compare the files available in two directories using diff command in Linux?
- How to Search and Remove Directories Recursively on Linux?
- Listing out directories and files in Python?
- Listing out directories and files using C#
- Generate temporary files and directories using Python
- C Program to list all files and sub-directories in a directory
- How to list non-hidden files and directories in windows using Python?