Raspberry Pi Shell Commands used in Terminal and ssh.

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:

Open Raspberry Pi Terminal from Desktop.

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.

Shell Commands:

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:

Raspberry Pi Terminal – Shell Commands – Pi With Vic

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:

touch readme.txt

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:

Using the mv Shell Command to Rename a File in the Raspberry Pi Terminal.

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:

mkdir mydir

Using the touch shell command create an empty text file like below:

touch myfile

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:

ls myfile

Results should be as follows:

ls: cannot access 'myfile': No such file or directory

Now enter the following ls shell command as follows:

ls mydir/myfile

As a result the file named myfile should be confirmed to exist in the directory mydir as shown here:

mydir/myfile

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:

Using the mv Shell Command to Move File in the Raspberry Pi Terminal.

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:

More Information:

For more information enter the following manual page or help command for mv in the Raspberry Pi Terminal:

man mv

Or

mv --help

Additional information on GNU coreutils online help can be found at: gnu.org.