Linux move one file to another directory

Classic SysAdmin: How to Move Files Using Linux Commands or File Managers

There are certain tasks that are done so often, users take for granted just how simple they are. But then, you migrate to a new platform and those same simple tasks begin to require a small portion of your brain’s power to complete. One such task is moving files from one location to another. Sure, it’s most often considered one of the more rudimentary actions to be done on a computer. When you move to the Linux platform, however, you may find yourself asking “Now, how do I move files?”

If you’re familiar with Linux, you know there are always many routes to the same success. Moving files is no exception. You can opt for the power of the command line or the simplicity of the GUI – either way, you will get those files moved.

Let’s examine just how you can move those files about. First, we’ll examine the command line.

Command line moving

One of the issues so many users new to Linux face is the idea of having to use the command line. It can be somewhat daunting at first. Although modern Linux interfaces can help to ensure you rarely have to use this “old school” tool, there is a great deal of power you would be missing if you ignored it altogether. The command for moving files is a perfect illustration of this.

The command to move files is mv . It’s very simple and one of the first commands you will learn on the platform. Instead of just listing out the syntax and the usual switches for the command – and then allowing you to do the rest – let’s walk through how you can make use of this tool.

The mv command does one thing – it moves a file from one location to another. This can be somewhat misleading because mv is also used to rename files. How? Simple. Here’s an example. Say you have the file testfile in /home/jack/ and you want to rename it to testfile2 (while keeping it in the same location). To do this, you would use the mv command like so:

mv /home/jack/testfile /home/jack/testfile2

or, if you’re already within /home/jack:

The above commands would move /home/jack/testfile to /home/jack/testfile2 – effectively renaming the file. But what if you simply wanted to move the file? Say you want to keep your home directory (in this case /home/jack) free from stray files. You could move that testfile into /home/jack/Documents with the command:

mv /home/jack/testfile /home/jack/Documents/

With the above command, you have relocated the file into a new location, while retaining the original file name.

Читайте также:  Linux openvpn server log

What if you have a number of files you want to move? Luckily, you don’t have to issue the mv command for every file. You can use wildcards to help you out. Here’s an example:

You have a number of .mp3 files in your ~/Downloads directory (~/ – is an easy way to represent your home directory – in our earlier example, that would be /home/jack/) and you want them in ~/Music. You could quickly move them with a single command, like so:

That command would move every file that ended in .mp3 from the Downloads directory, and move them into the Music directory.

Should you want to move a file into the parent directory of the current working directory, there’s an easy way to do that. Say you have the file testfile located in ~/Downloads and you want it in your home directory. If you are currently in the ~/Downloads directory, you can move it up one folder (to ~/) like so:

The “../” means to move the folder up one level. If you’re buried deeper, say ~/Downloads/today/, you can still easily move that file with:

Just remember, each “../” represents one level up.

As you can see, moving files from the command line isn’t difficult at all.

GUI

There are a lot of GUIs available for the Linux platform. On top of that, there are a lot of file managers you can use. The most popular file managers are Nautilus (GNOME) and Dolphin (KDE). Both are very powerful and flexible. I want to illustrate how files are moved using the Nautilus file manager.

Nautilus has probably the most efficient means of moving files about. Here’s how it’s done:

Nautilus screenshot

  1. Open up the Nautilus file manager.
  2. Locate the file you want to move and right-click said file.
  3. From the pop-up menu (Figure 1) select the “Move To” option.
  4. When the Select Destination window opens, navigate to the new location for the file.
  5. Once you’ve located the destination folder, click Select.

This context menu also allows you to copy the file to a new location, move the file to the Trash, and more.

If you’re more of a drag and drop kind of person, fear not – Nautilus is ready to serve. Let’s say you have a file in your home directory and you want to drag it to Documents. By default, Nautilus will have a few bookmarks in the left pane of the window. You can drag the file into the Document bookmark without having to open a second Nautilus window. Simply click, hold, and drag the file from the main viewing pane to the Documents bookmark.

If, however, the destination for that file is not listed in your bookmarks (or doesn’t appear in the current main viewing pane), you’ll need to open up a second Nautilus window. Side by side, you can then drag the file from the source folder in the original window to the destination folder in the second window.

If you need to move multiple files, you’re still in luck. Similar to nearly every modern user interface, you can do a multi-select of files by holding down the Ctrl button as you click each file. After you have selected each file (Figure 2), you can either right-click one of the selected files and then choose the Move To option, or just drag and drop them into a new location.

Читайте также:  Linux забыть wifi сеть

nautilus

The selected files (in this case, folders) will each be highlighted.

Moving files on the Linux desktop is incredibly easy. Either with the command line or your desktop of choice, you have numerous routes to success – all of which are user-friendly and quick to master.

Источник

Linux mv Command Examples

The mv command is one of the basic Linux commands that is used to move the files and directories from one location to another. It is also used to rename the files and directories. The mv command is available on all Linux distributions by default.

In this post, we will show you the practical examples of the mv command in Linux. We will also show the command line options that are used with it.

Let’s start with this guide.

How Does the MV Command Work in Linux?

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

To move a single file to another directory, use the following syntax:

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

Options Description
-v It overwrites the existing file or directory.
-i It prompts whether to overwrite the existing file or not.
-f It is used to forcefully overwrite the existing file without prompting.
-n The files remain intact and your file will not be moved to the destination directory.
-b It creates a backup file in the destination directory.

To explore more options of the “rm” command, users can utilize the “help” utility:

The examples of the Linux mv command are as follows:

Example 1: Move a Single File or Directory from One Directory to Another

To move a single file or directory from one location to another, you need to tell “mv” where the file is and where to move it. Note that when you move a file to another directory where another file with the same name already exists, it overwrites the existing file.

For instance, to move a file named sample1.txt from the current directory to the ~/Document directory, the command is as follows:

Similarly, to move a directory from one location to another, use the following syntax:

For instance, to move a directory named testdir from the current directory to the ~/Documents directory, the command is as follows:

Example 2: Move Multiple Files or Directories from One Directory to Another

To move multiple files from one directory to another, use the following syntax:

For instance, to move the files named sample1.txt, sample2.txt, and sample3.txt from the current directory to the ~/Document directory, the command would be:

Similarly, to move multiple directories from one location to another, use the following syntax:

For instance, to move the directories named testdir1, testdir2, and testdir3 from the current directory to the ~/Documents directory, the command would be:

Читайте также:  Установка доверенного сертификата linux

Example 3: Rename a File and Directory

With the mv command, you can also rename a file or directory. To rename a file, use the following syntax:

For instance, to rename a file named sample1.txt to sample2.txt, the command would be:

Note: If the file sample2.txt already exists, it is overwritten by the file sample1.txt.

To rename a directory, use the following syntax:

For instance, to rename a directory named testdir1/ to testdir2/, the command would be:

Example 4: Prompt Before Overwriting an Existing File

When you move a file to another directory where another file with the same name already exists, it overwrites the existing file at the destination directory by default. If you want, you can tell the mv command to ask before overwriting the existing file using the mv command’s -i option.

If you want to move the sample.txt file to ~/Documents directory which already contains a file named sample.txt, the -i option prompts you before overwriting the file.

If you want to overwrite the file, hit y. Otherwise, it is canceled.

Example 5: Do Not Overwrite an Existing File

If you want, you can tell the mv command to never overwrite an existing file at the destination using the -n option as follows:

For instance, you want to move the sample.txt file to ~/Documents directory which already contains a file named sample.txt. If you use the -n option, it prevents the file from being overwritten.

Example 6: Move Only If the Source File Is Newer Than the Destination

When moving a file to another directory that already contains the same file, you can tell the mv command to update the file at the destination only if the source file is newer than the file at the destination.

We have a sample2.txt file that exists in both the current directory and the ~/Documents directory. The sample.txt file that exists in the current directory is newer than the sample.txt file that exists in the ~/Documents directory as can be seen in the following screenshot.

Now, if we use the mv command -u option, the file at the destination is updated as the source file is more recent.

Example 7: Create a Backup of Existing Destination File

To avoid the already existing destination file being overwritten, you can also create its backup at the destination directory using the mv command’s -b option:

We have a test.txt file that exists in both the current directory and the ~/Documents directory. Before the test.txt file at the destination directory gets overwritten by the source file, you can create its backup using the -b option as follows:

It creates the backup file at the destination directory with the same name but with a tilde (~) appended to it.

That is all from the “mv” command.

Conclusion

The “mv” command offers more flexibility and is recommended to move the files in directories. We covered the different examples of using the “mv” command to move a file from one directory to another in the same or different location.

About the author

Syed Minhal Abbas

I hold a master’s degree in computer science and work as an academic researcher. I am eager to read about new technologies and share them with the rest of the world.

Источник

Оцените статью
Adblock
detector