mv Command in Linux
The mv command is one of Linux’s built-in commands, and its primary function is to move files and directories. However, the mv command’s function is not limited to moving files; it also performs a variety of other tasks. In Linux, mv stands for move.
In this tutorial, we will learn how to move files and directories in Linux using mv Command.
How to use mv command
The mv command is used to move files and directories from one location to another. When moving the mv command delete the original file or directory. But it will wait for the files/directories to be completely moved to end the action. When moving a file to a destination, if the destination already has a file with the same name then it will get overwritten.
You may also use the mv command to rename the name of the file and directory. If both filenames or directories are on the same filesystem, results in a rename.
The mv won’t rename a directory to another directory if the target directory contains the same directory with files. «Directory not empty» messages come due to this reason.
The basic syntax of the mv command is as follows.
[Options] refers to the various mv command options. The Source might be a single file or directory or multiple files or directories. At the Destination , a single file or directory can be specified.
You must have write permission on both the Source and the Destination to move a file or directory, or you will receive a permission denied error.
- When the Source consists of numerous files or directories, the Destination must be a directory. The files or directories on the Source are moved to the Destination directory.
For example, if we need to move the text1.txt file to the /dir1 directory, the syntax would be as follows.
- When the Source consists of numerous files or directories, the Destination must be a directory. The files or directories on the Source are moved to the Destination directory.
- When the Source is a single file and the Destination is a directory, the file is moved to the Destination directory.
- When the Source is a single file and the Destination is a filename, the Source file is renamed to the Destination filename.
- The Source is a directory, as is the Destination, but the Destination directory does not exist. In this situation, the Source directory will be renamed as the Destination directory. If the Destination directory already exists, the Source directory is moved to it.
How to move multiple files or directories
To move multiple files or directories, you must first specify the Source file names and the Destination directory.
For example, to move text1, text2, and text3 files to the dir1 directory, use the following syntax.
The mv command also supports pattern matching For example, you could use the following syntax to move all text files to the
How to rename a file or directory
You can use the mv command to rename a file or a directory.
For example, to rename a file from text1.txt to text2.txt, use the following syntax.
For example, if we need to move the dir1 directory to the dir2 directory, the syntax would be as follows. However, if the dir2 does not exist then the dir1 directory is renamed as dir2.
mv command Options
The mv command provides various options for specific purposes. Some of the useful mv command options are:
Prompt Before Overwriting
If the Destination file or directory already exists, it is overwritten by default. The -i option can be used to display a confirmation prompt. The following syntax is used to display prompts before overwriting.
While attempting to move file1 to the dir1 directory when file1 already exists in dir1, a prompt will appear as:
If you want to overwrite type y or Y .
Not Overwrite Existing file
To never overwrite an existing file, use the -n option along with the mv command, as shown in the following syntax.
When attempting to move file1 to the dir1 directory, if file1 already exists, the command will do nothing; otherwise, the file will be moved to dir1.
Force Overwriting
The following syntax can be used to force a file to overwrite without displaying a prompt message.
Backup Files
To back up a file in an existing destination file, use the -b command-line option. The following syntax can be used to back up a file.
) will show in the backed-up file with the same name as the original.
Conclusion
In this tutorial, we learned how to use the mv command in Linux. For more information refer to man mv.
Источник
How to Move Files Using Linux Commands or File Managers
Learn how to move files with Linux commands in this tutorial from our archives.
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 all together. 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:
mv testfile testfile2
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.
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
/ – 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
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.
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 (on the Ubuntu 13.10 distribution, with Unity as the interface).
Nautilus has probably the most efficient means of moving files about. Here’s how it’s done:
Open up the Nautilus file manager.
Locate the file you want to move and right-click said file.
From the pop-up menu (Figure 1) select the “Move To” option.
When the Select Destination window opens, navigate to the new location for the file.
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 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 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 the choose the Move To option, or just drag and drop them into a new location.
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.
Источник
How to move (and overwrite) all files from one directory to another?
I know of the mv command to move a file from one place to another, but how do I move all files from one directory into another (that has a bunch of other files), overwriting if the file already exists?
6 Answers 6
From the man page:
It’s just mv srcdir/* targetdir/ .
If there are too many files in srcdir you might want to try something like the following approach:
In contrast to \; the final + collects arguments in an xargs like manner instead of executing mv once for every file.
It’s also possible by using rsync , for example:
- -v , —verbose : increase verbosity
- -a , —archive : archive mode; equals -rlptgoD (no -H,-A,-X )
- —delete-after : delete files on the receiving side be done after the transfer has completed
If you’ve root privileges, prefix with sudo to override potential permission issues.
For moving and overwriting files, it doesn’t look like there is the -R option (when in doubt check your options by typing [your_cmd] —help . Also, this answer depends on how you want to move your file. Move all files, files & directories, replace files at destination, etc.
When you type in mv —help it returns the description of all options.
For mv, the syntax is mv [option] [file_source] [file_destination]
To move simple files: mv image.jpg folder/image.jpg
To move as folder into destination mv folder home/folder
To move all files in source to destination mv folder/* home/folder/
Use -v if you want to see what is being done: mv -v
Use -i to prompt before overwriting: mv -i
Use -u to update files in destination. It will only move source files newer than the file in the destination, and when it doesn’t exist yet: mv -u
Tie options together like mv -viu , etc.
If you simply need to answer «y» to all the overwrite prompts, try this:
In linux shell, many commands accept multiple parameters and therefore could be used with wild cards. So, for example if you want to move all files from folder A to folder B, you write:
If you want to move all files with a certain «look» to it, you could do like this:
Which copies all files that are blablabla.txt to folder B
Star (*) can substitute any number of characters or letters while ? can substitute one. For example if you have many files in the shape file_number.ext and you want to move only the ones that have two digit numbers, you could use a command like this:
Or more complicated examples:
For files that look like fi _ .e
Unlike many commands in shell that require -R to (for example) copy or remove subfolders, mv does that itself.
Remember that mv overwrites without asking (unless the files being overwritten are read only or you don’t have permission) so make sure you don’t lose anything in the process.
For your future information, if you have subfolders that you want to copy, you could use the -R option, saying you want to do the command recursively. So it would look something like this:
By the way, all I said works with rm (remove, delete) and cp (copy) too and beware, because once you delete, there is no turning back! Avoid commands like rm * -R unless you are sure what you are doing.
Источник