| by Arround The Web | No comments

How to Move a File From One Directory to Another in Linux

When working with files and directories in Linux, you will often need to move files from one location to another. It could be that you want to move a file to a directory in the same or another location. Linux offers two ways of moving files from one directory to another. You can use the command-line option with the “mv” command or the graphical interface to move the files. This guide covers the two options.

Moving Files in Linux

There are multiple reasons for moving files in Linux. Let’s cover two options for moving files.

1. Via Graphical Interface

Using the GUI favors those not comfortable with using the command line. Linux has different GUIs but the process of moving files is the same.

Follow the steps below.

1. Open your file manager, such as GNOME or KDE, and navigate to the directory containing the file you want to move.

2. Right-click on the file.

3. On the context menu that appears, click the Move To option


4. Select the destination directory and click the select button at the top to move your file.


That is all you need to transfer files from one directory to another using the Linux GUI.

2. Using the “mv” Command

Linux offers the “mv” command for moving files and directories from one location to another. Still, you can use the “mv” command to rename a file or folder.

The move command has different options that you can utilize when moving files and directories. Let us have different examples to understand how to use it to move files.

i. Moving a File to a Directory in the Same Location

When you have a file in a directory containing the destination directory, moving the files is straightforward as no destination or source path is required.

Here is an example of moving names.txt to docs in the same location.

$ mv -v source destination

 

We are adding -v for verbose. If we try moving a file with the same name to another in the destination file, it will overwrite the existing one.

To avoid that, add the -i for interactive. You will be prompted whether to overwrite the existing file or not.


Pressing y will overwrite the existing file. To forcefully overwrite the existing file without prompting, use the -f flag.


Suppose you do not want to overwrite the file. You can add the -n option. In this case, both files will remain intact and your file will not be moved to the destination directory as it contains another with the same name.


If you want to move your file and create a backup in the destination directory, use the -b option.


The backup file created will have a trailing tilde (~) sign.

Using the syntax below, you can also specify more than one file in the same command regardless of the file type.

$ mv -v file file2 file3 destination

 

Another great option that you can use is the asterisks (*). It allows you to move files that match a given pattern, such as all .txt or .pdf files. Let us use it to move all text files.

ii. Moving a File to a Directory in a Different Location

So far, we have covered examples that involve moving a file to a directory in the same location. If the destination is in another location, you must specify its full path.

The example below moves the specified script to a directory in another location. We have specified the path to the destination directory starting from Home.

$ mv -v script.sh ~/Pictures/test/

 

With Linux, you can also move a file from a given directory to another without being in the current directory. For instance, you can be in the /Downloads and move a file from /Pictures to /Music by specifying the path to the source and its destination. Here is an example.

Conclusion

You can move files in Linux using the GUI or the move command. The move command offers more flexibility and is recommended for moving files in directories. We have covered different examples of using the move command to move a file from one directory to another in the same or different location.

Share Button

Source: linuxhint.com

Leave a Reply