How to move files in Linux/Unix

In this tutorial, we will explain how to use the “mv” command to move files in Linux and Unix, with examples.

Open the terminal

The first step is to open the terminal on your Linux or Unix machine. You can do this by clicking on the terminal icon in the taskbar or by using the keyboard shortcut “Ctrl + Alt + T.”

Use the “mv” command

Once the terminal is open, you can use the “mv” command to move a file from one location to another. The basic syntax of the “mv” command is as follows:

mv [options] [source_file] [destination]

For example, if you want to move a file named “file.txt” from your home directory to a directory named “documents,” you would use the command:

mv ~/file.txt ~/documents

Moving multiple files

You can also move multiple files at once by providing a list of files as the source.

mv file1.txt file2.txt file3.txt ~/documents

Renaming a file while moving

You can also move a file to a different directory and rename it in the process. To do this, you would provide the new name as the destination.

mv file.txt ~/documents/new_file.txt

Using Wildcards

The “mv” command also supports the use of wildcards, which allows you to move multiple files that match a certain pattern. For example, if you wanted to move all files with the “.txt” extension from your home directory to a directory called “text_files,” you would use the command:

Advertisements
mv ~/*.txt ~/text_files

Overwriting files

By default, the “mv” command will not overwrite existing files. If you want to force the “mv” command to overwrite existing files, you can use the “-f” option.

mv -f file.txt ~/documents

Verbose option

The “mv” command also has a “verbose” option, “-v,” which displays the names of files that are moved. This can be useful when you’re moving a large number of files and you want to ensure that the command is working as expected.

mv -v file.txt ~/documents

By following the steps above and examples provided, you will be able to move files effectively on your Linux or Unix machine.

Advertisements

LEAVE A REPLY

Please enter your comment!
Please enter your name here