- How to Move Files Using Linux Commands or File Managers
- Command line moving
- How to rename and move files and directories on Linux
- Overview
- Rename files on Linux
- Moving a file on Linux
- Moving and Renaming files on Linux
- Moving Multiple files on Linux
- Moving Directories on Linux
- Verbose Output Flag
- Do Not Overwrite Existing Files
- Do Not Prompt to Confirm Overwrites
- mv Command in Linux: 7 Essential Examples
- 7 practical examples of the mv command
- 1. How to move a file to different directory
- 2. How to move multiple files
- 3. How to rename a file
- 4. How to move a directory in Linux with mv command
- 5. How to rename a directory
- 6. How to deal with overwriting a file while moving
- 7. How to forcefully move the files
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 rename and move files and directories on Linux
Overview
In this tutorial, you will learn how to use the mv command to move and renames files and directories on Linux.
Files and directories on Linux as very similar, from a filesystem point of view. This means the operations done on one can be also done on the other, with very few exceptions.
As such, you will notice that commands used to perform actions on files are identical with directories.
Rename files on Linux
To rename a file in Linux you use the mv command. The command accepts two or more arguments. For renaming files, only two arguments are needed, which are the source file and the target file.
The mv command will take the source file specified and rename it to the target file.
To rename a file named student1 to student10, for example, you would run the following command.
Provided the file target is the same directory, all file attributes will remain, including permissions.
Moving a file on Linux
To move a file to another location we use the same method as renaming a file, except the file path should be different.
For example, to move a file from /home/student1/lab-work.log to /var/labs/student1/lab-work.log , you would run the following command.
Moving and Renaming files on Linux
A file can be renamed during a move process using the mv command. You simply give the target path a different name. When mv moves the file, it will be given a new name.
For example, to move a file named student1.txt to /var/students and rename it to class1-student1.txt , you would run the following command.
Moving Multiple files on Linux
The mv command accepts multiple source files, which means we can move two or more files at the same time. When executing the mv command, each file listed will be considered a source with the last path being the exception. The last path will be treated as the target.
For example, to move student1.txt and student2.txt to /var/students , you would run the following command.
Moving Directories on Linux
Moving directories work the same as moving files. We specify the source directory and give a target directory.
For example, to move a directory path /tmp/logs to
/data/logs you would run the following command.
Moving Multiple Directories on Linux
As with files, multiple directories can be moved to a new location. We simply specially all of the directories to be moved, and then give a target directory for them to be moved to.
Verbose Output Flag
The mv command will perform its operations silently. No output will be printed to the screen while files or directories are being moved or renamed.
To instruct the mv command to print out a log of actions being taken, you can use the -v flag. This flag enabled verbosity, which is helpful for auditing.
Do Not Overwrite Existing Files
To force the mv command to not overwrite existing files when moving or renaming a file, use the -n flag.
In the example below, if the student2.txt file already exists, then the mv command will not rename the file and it will exit with an error.
Do Not Prompt to Confirm Overwrites
If you want to forcefully move files or directories and overwrite paths that already exist, you can use the -f flag. This is effective for overwriting old, stale files or directories with new ones with the same name.
Источник
mv Command in Linux: 7 Essential Examples
mv is one of the must known commands in Linux. mv stands for move and is essentially used for moving files or directories from one location to another.
The syntax is similar to the cp command in Linux however there is one fundamental difference between these two commands.
You can think of the cp command as a copy-paste operation. Whereas the mv command can be equated with the cut-paste operation.
Which means that when you use the mv command on a file or directory, the file or directory is moved to a new place and the source file/directory doesn’t exist anymore. That’s what a cut-paste operation, isn’t it?
cp command = copy and paste
mv command = cut and paste
mv command can also be used for renaming a file. Using mv command is fairly simple and if you learn a few options, it will become even better.
7 practical examples of the mv command
Let’s see some of the useful examples of the mv command.
1. How to move a file to different directory
The first and the simplest example is to move a file. To do that, you just have to specify the source file and the destination directory or file.
This command will move the source_file and put it in the target_directory.
2. How to move multiple files
If you want to move multiple files at once, just provide all the files to the move command followed by the destination directory.
You can also use glob to move multiple files matching a pattern.
For example in the above example, instead of providing all the files individually, you can also use the glob that matches all the files with the extension .txt and moves them to the target directory.
3. How to rename a file
One essential use of mv command is in renaming of files. If you use mv command and specify a file name in the destination, the source file will be renamed to the target_file.
In the above example, if the target_fille doesn’t exist in the target_directory, it will create the target_file.
However, if the target_file already exists, it will overwrite it without asking. Which means the content of the existing target file will be changed with the content of the source file.
I’ll show you how to deal with overwriting of files with mv command later in this tutorial.
You are not obliged to provide a target directory. If you don’t specify the target directory, the file will be renamed and kept in the same directory.
Keep in mind: By default, mv command overwrites if the target file already exists. This behavior can be changed with -n or -i option, explained later.
4. How to move a directory in Linux with mv command
You can use mv command to move directories as well. The command is the same as what we saw in moving files.
In the above example, if the target_directory exists, the entire source_directory will be moved inside the target_directory. Which means that the source_directory will become a sub-directory of the target_directory.
5. How to rename a directory
Renaming a directory is the same as moving a directory. The only difference is that the target directory must not already exist. Otherwise, the entire directory will be moved inside it as we saw in the previous directory.
6. How to deal with overwriting a file while moving
If you are moving a file and there is already a file with the same name, the contents of the existing file will be overwritten immediately.
This may not be ideal in all the situations. You have a few options to deal with the overwrite scenario.
To prevent overwriting existing files, you can use the -n option. This way, mv won’t overwrite existing file.
But maybe you want to overwrite some files. You can use the interactive option -i and it will ask you if you want to overwrite existing file(s).
You can enter y for overwriting the existing file or n for not overwriting it.
There is also an option for making automatic backups. If you use -b option with the mv command, it will overwrite the existing files but before that, it will create a backup of the overwritten files.
By default, the backup of the file ends with
. You can change it by using the -S option and specifying the suffix:
You can also use the update option -u when dealing with overwriting. With the -u option, source files will only be moved to the new location if the source file is newer than the existing file or if it doesn’t exist in the target directory.
- -i : Confirm before overwriting
- -n : No overwriting
- -b : Overwriting with backup
- -u : Overwrite if the target file is old or doesn’t exist
7. How to forcefully move the files
If the target file is write protected, you’ll be asked to confirm before overwriting the target file.
To avoid this prompt and overwrite the file straightaway, you can use the force option -f.
If you do not know what’s write protection, please read about file permissions in Linux.
You can further learn about mv command by browsing its man page. However, you are more likely to use only these mv commands examples I showed here. FYI, you may also use rename command for renaming multiple files at once.
I hope you like this article. If you have questions or suggestions, please feel free to ask in the comment section below.
Источник