- How to Move Files Using Linux Commands or File Managers
- Command line moving
- Linux command to move a directory
- 5 Answers 5
- Not the answer you’re looking for? Browse other questions tagged linux command-line mv or ask your own question.
- Related
- Hot Network Questions
- Subscribe to RSS
- Move a folder in Linux using mv command
- mv command syntax
- Linux mv command examples
- Move files and folders recursively on Linux
- 8 Answers 8
- How do I move all files from one folder to another using the command line?
- 12 Answers 12
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.
Источник
Linux command to move a directory
My old and new directory have same folders and files inside.
mv: cannot move `./xxxxxx’ to a subdirectory of itself
How can I move it?
5 Answers 5
You should use mv -if old/* new/ without the trailing * .
This is because it unrolled to
i.e. move everything into new/baz
This is not what you wanted.
It works. What are You trying to achieve? Could You please write a short example of what the input data should look like and what the output data should look like? The truth is I have no idea what You are trying to do 🙂 Help me help You.
note that mv a/* b/ don’t move files .* (file name start with ‘.’) in a/ to b/
If you are copying from an ext2/3/4 file system to a FAT32 file system, and a filename has an invalid character for FAT32 naming conventions, you get this terribly annoying and incorrect as hell error message. How do I know? I wrestled with this bug — yes, it’s a KERNEL BUG — for 6 hours before it dawned on me. I thought it was a shell interpreter error, I thought it was an «mv» error — I tried multiple different shells, everything. Try this experiment: on an ext file system, «touch ‘a:b'» them «mv» it to a FAT32 file system. Try it, you’ll enjoy (hate) the results. The same is true for ‘ ‘ (\074 and \076).
Thanks for «man mv» — that’s a real big help, don’t quit your day job.
Might be you got the answer but above answer is not working for me. and finally lots of researching I got the answer. (Issue is due to files-ownership)
and just put sudo before the command and its working. 🙂 Same thing for cp and mv command.
Not the answer you’re looking for? Browse other questions tagged linux command-line mv or ask your own question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
Move a folder in Linux using mv command
Q: How can I move a folder in Unix, Linux and MacOS operating systems using the command line?
A: Folders / directories and files can be moved and renamed using the mv command in the Linux terminal.
mv command syntax
The Linux mv commnand sintax is pretty easy:
Linux mv command examples
Move folder and folder2 to your /tmp directory
Move folder and file1 to /home/ directory
The previous examples would move files and folders to the new destinations without changing their names.
mv command can be used to move a very big number of files and folders in a single command line.
We will move all the files, folders and all the sub-folders that are located inside /home/johndoe/ directory, to the new destination /home/jane/new_folder/.
Here we used a wildcard (the asterisks), which is used in Unix and Linux like systems, to specifiy that all content must be affected (moved in this case).
In this next example, we will move multiple direcctories from different paths to a new folder:
or you can also do it from the origin directory:
mv command can be configured to show the output of every action it is doing by adding the verbose option -v, for example:
This is the output:
In the previous examples, when you move a folder or file as we’ve done in the previous examples, you will not receive any confirmation before moving the content from one place to another, unless there is a file or folder with the same name on the destination path.
You can force promt before overwriting the files usign the -i option, in this way mv becomes interactive and will ask you for confirmation on the Linux terminal.
Other mv command options that may be useful in your Linux system administration day to day tasks. The man page from gnu/mv have lot of options you can explore, let’s see some of the most useful mv options:
If you need to see the full mv command options, check out the man page as you see below:
Источник
Move files and folders recursively on Linux
How do I move the contents of /public-back/templates recursively with permissions into /public/templates ?
8 Answers 8
Unless I am misunderstanding the question, this would work:
Also, unless you have a huge list of files, adding -i will ask before it overwrites anything, which add some safety when using wildcards like * .
The man page for cp states:
When moving items from my thumb drive to my OSMC system, I’ve found the following very useful:
Explanation on how it works below.
BTW, Don’t forget to add a backslash before any spaces in the source or destination directory names (see above).
Effectively, you are finding all files and all folders and moving them one by one (or if a directory gets found first, you are moving that directory and the contents in it). This starts a new process for each move and is very inefficient. Only use this when the regular commands fail.
So create hard links in the destination directory and remove the source dir. ‘mv’ simply will not work in your case, and in general works only when source dir and dest have no common subtrees.
Note that I’m assuming that the word ‘move’ in the question means that the source dir should be gone after the operation.
mv doesn’t seem to do this. But you can use this little trick, works like a charm:
and preserves permissions and all.
Note: none of the above worked for me, that’s why this workaround.
It is possible to move instead of copy with rsync by using the —remove-source-files argument. This will preserve properties such as permissions and dates modified. It has the added benefit of checking whether files don’t need to be moved to the target directory (i.e., if a newer file with the same name already exists there).
Of course you can also copy the files and remove the original directory.
This is my recommended parameters for rsync but there are other arguments for preserving various properties or handling links and compression/encryption of large files. This command also supports copying to remote file systems via ssh tunnels.
As noted above, on the same filesystem mv is faster than cp . By example, the following preserves timestamps and ownerships, and recursively moves directories and files including hidden files and directories.
Initial conditions:
- note ownership of dir02/* is root:victoria
Move those files, directories:
This appears to preserve:
- timestamps
- recursively moves files, directories (including hidden files, directories)
- preserves ownerships
After the move:
None of the answers in this thread fit my usecase, so I came up with one on my own as a shell script.
At the heart of it is this function:
Which you could invoke like so (for the OP usecase):
If source is a file or a directory for which the destination does not exist yet, it simply moves it with mv -u . If source is a directory for which the destination already exists, it iterates through its contents and recursively performs the same check followed by move|recurse with each of its members.
I used -u because I only needed old files freshened and newer files untouched, but you could replace it with -f for an unconditional move or -i for an interactive one. Or not change anything and simply rm -rf your source after the script is done moving things.
[A slightly modified repost from Server Fault]
Источник
How do I move all files from one folder to another using the command line?
I would like to know how could I move all files from a folder to another folder with a command line.
Let’s say I’m in my Downloads folder and there are a 100 files that I would like to move to my Videos folder, without having to write all the files name.
12 Answers 12
Open a terminal and execute this command:
It will move all the files and folders from Downloads folder to Videos folder.
To move all files, but not folders:
If you are interested in moving all files (but not folders) from Downloads folder to Videos folder, use this command
To move only files from the Download folders, but not from sub-folders:
If you want to move all files from the Downloads folder, but not any files within folders in the Download folder, use this command:
here, -maxdepth option specifies how deep find should try, 1 means, only the directory specified in the find command. You can try using 2 , 3 also to test.
See the Ubuntu find manpage for a detailed explanation
/Videos/») won’t move dot files.
/Videos can be more efficiently done with -exec mv -t
/Videos/ does not work for hidden files
It will move all the files including subfolders in the directory you want to mv . If you want to cp (copy) or rm (remove) you will need the -r (recursive) option to include subfolders.
/ in the prefix of the folder names doesn’t always work (doesn’t work on bash and git atleast)
For the simple case:
If you want to move dot (hidden) files too, then set the dotglob shell option.
This leaves the shell option set.
For one time dotglob use, run the commands in a subshell:
/Videos) only moves (cuts) the contents (including the hidden files). In this case, both the origin and destination folders must exist already. At the end, the origin directory becomes empty.
It’s possible by using rsync , for example:
-a , —archive : Archive mode; equals -rlptgoD (no -H , -A , -X ).
-u , —update : Skip files that are newer on the receiver.
—remove-source-files This tells rsync to remove from the sending side the files (meaning non-directories) that are a part of the transfer and have been successfully duplicated on the receiving side.
If you’ve root privileges, prefix with sudo to override potential permission issues.
To move a directory with or without content to its new name just like how you would use the mv command to rename a file:
- -T treats the destination as a normal file
- dir1 is the original name of the directory
- dir2 is the new name of the directory
NB: dir2 doesn’t have to exist.
I hope this saves someone a lot of time, as a noob, before this, I would create a directory with the new name and then move the contents of the directory to the directory created earlier.
Источник