mv – Move or Rename Files and Directories
When logged into the Raspberry Pi Terminal or ssh, the mv shell command allows users to move or rename files. In addition, Directories can be renamed or moved.
Example of mv Shell Command:
For example, on the Raspberry Pi Desktop, open the Terminal application as seen below:
Raspberry Pi Terminal:
As a result, the terminal window will appear with a command prompt. Consequently, the command prompt interface provides the ability to type and enter names of programs for execution. Accordingly, the results of program execution are displayed as output in the Raspberry Pi terminal window. In fact, both the Terminal and ssh are command line interfaces. In contrast, the Raspberry Pi Desktop is a graphical user interface (GUI) for managing files and execution of programs.
Generally speaking, several programs included in the Operating System are Shell Commands. Shell commands are used in shell scripting and batch jobs. Additionally, shell commands are entered at the command prompt in the Raspberry Pi Terminal to perform various tasks. In fact, for more information on Shell Commands, follow the link below:
mv Shell Command Example:
For example, let us assume you have a text file named readme.txt in your home directory. Further, let us assume you wish to rename the file to notes.txt. In the Raspberry Pi Terminal within your home directory enter the following command:
After the file is created enter the following ls shell command:
ls -al readme.txt
As a result you should see details on the empty file you created with the touch shell command. Now enter the following mv shell command to rename your file as shown below:
mv readme.txt notes.txt
The File should now be renamed. See picture below showing the example demonstrated above:
Moving Files with the mv Shell Command:
The mv shell command allows for moving a File or Directory containing one or more Files. For this example, let us first crate a new Directory for this demonstration. Enter the following shell commands in the Raspberry Pi Terminal as shown below:
Using the touch shell command create an empty text file like below:
Using the mv shell command move and relocate your new file named myfile to the mydir directory created above:
mv myfile mydir
Enter the following ls shell command as shown below to verify results:
Results should be as follows:
ls: cannot access 'myfile': No such file or directory
Now enter the following ls shell command as follows:
As a result the file named myfile should be confirmed to exist in the directory mydir as shown here:
Enter the following mv shell command to demonstrate how to rename a Directory followed by ls shell command to verify results:
mv mydir newdir ls newdir
See picture below for the example used above:
Popular often used mv Command Options:
The following is a partial list of options used often that can precede file list when using mv shell command:
- -backup[Control] : Make a backup of each existing Destination File.
- -b : Like -backup however does not accept an argument.
- -f or -force : Do not prompt before overwriting.
- -i or -interactive : Prompt before overwriting.
- -n or -no-clobber : Do not overwrite an existing File.
- -u or -update : Move only when the Source File is newer than the Destination File or when the Destination File is missing..
- -v or -verbose : Explain in detail what Operations are being done.
- -help : Display Help Usage.
- -version : Output all version information and exit.
File Commands Related to mv Shell Command:
The mv shell command is part of a group of file commands. For more File Commands see list below:
- cat – Concatenate files.
- cp – Copy Files and Directories.
- head – Display the first part of a file.
- less – An Advanced File Viewer.
- ln – Link Symbolic Links to Files.
- ls – List Directory Contents.
- mkdir – Make Directory.
- more – An Basic File Viewer.
- mv – Move or Rename File or Directory.
- rm – Remove File.
- pwd – Print Working Directory.
- tail – Display the last part of file.
- touch – Update Key File Timestamps.
- wc – Word Count for counting words in a file.
For more information enter the following manual page or help command for mv in the Raspberry Pi Terminal:
Additional information on GNU coreutils online help can be found at: gnu.org.