Move all files and directories in linux

How to Move Files and Directories in Linux?

Moving files and directories can help you keep your file system organized by placing related files and directories in the same location. This will also free up space in the original file system, which can be useful if you run low on storage space.

This is one of the most important aspects that every Linux user should be aware of, and that’s why we will teach you how to move files and directories in Linux.

  • How to Use the mv Command in Linux?
  • Move a Single File and Directory to another Directory (CLI)
  • Move Multiple Files and Directories to a Different Directory via Terminal (CLI)
  • Move Files and Directories via Graphical User Interface (GUI)

Let’s start with the mv command.

How to Use the mv Command in Linux

The first thing that a user should do while learning about any command is to understand its basic syntax you can do this by typing:

This is a generic command, so its syntax will remain the same for all different Linux distributions such as Ubuntu, Fedora, or CentOS.

In this section, we will guide you on moving a single file or directory to a different location.

How to Move a Single File and Directory to Another Directory (CLI)?

The first thing you need to understand is that you should know the complete path where you want to move any file that you can do by typing:

You can see that after running the command, it shows the path where we are currently present. So, the next thing is learning how to move files in any directory. For that, we will be creating a few files for better understanding:

To show the list of all the directories available on the home directory along with these two newly created text files; you need to run the following command:

Suppose you want to move the ‘file1.txt’ in the ‘Documents’ directory then as stated earlier you need to know the complete path of any directory where you want to move the file. So, in this case, the complete path of the document’s directory would ‘/home/itslinux/Documents’ would be:

$ mv file1.txt /home/itslinux/Documents

You will get the same results in a simpler way as well by typing the below command:

Where ‘~/’ before the documents shows that you are currently in a home directory.

Now when you run the ‘ls’ command, you will notice that the ‘file1.txt’ is no longer available in the home directory and has moved to the document’s directory:

$ ls /home/itslinux/Documents $ ls ~/Dcouments

Pro Tip: Suppose you want to move a file in any directory, and you are not aware of its path so another way of doing this is to open that directory, right-click on it and then select ‘open in terminal’

When you do this then it will directly open the terminal by accessing that directory path and all you need to do is to run the pwd command again as shown:

Читайте также:  Logitech lightsync g102 linux

So, the path mentioned above can be used to move any file in the documents directory

Moving a Directory: The same thing could be applied to directories as well with the similar syntax as shown:

$ mv testdirectory ~/Documents

How to Move Multiple Files and Directories to a Different Directory (CLI)?

If you want to move multiple files in the directory, you need to type the name of all those files you want to move, followed by the destination directory.

$ mv file1.txt file2.txt /home/itslinux/Documents $ mv file1.txt file2.txt ~/Documents

The same thing can be applied while moving multiple directories to another directory as well just by typing:

$ mv Dir1 Dir2 /home/itslinux/Documents $ mv Dir1 Dir2 ~/Documents

How to Move All Files with the Same Format in the Directory?

Suppose if you want to move all the files into the document’s directory with ‘.txt’ format so you can do that by typing:

Similarly, if you want to move multiple files having different formats, then you can use the same above command while adding those formats as well:

How to Move Files Without Overwriting?

One of the drawbacks of the move command is that it will overwrite the file if it has the same name. So, to avoid that, you can use the ‘-i’ flag, which will prompt you whether you want to overwrite the file or not by typing:

The similar thing could be implemented for directories as well by following the same syntax as shown:

How to Move Files and Directories via Graphical User Interface (GUI)?

You can also move files and directories via GUI, and for that, you need to select the specific files or directories or their combination, right-click on them, and select the ‘move to’ option as shown.

This will open a new dialogue window where you must select your desired folder and click on the ‘select’ icon on the top right, as shown.

This will automatically move your selected files and folder to the desired folder. That’s how you can move files and directories in Linux.

Conclusion

Moving files and directories can help you organize your data, free up space, avoid duplication and make a backup. You can move files and directories in Linux either by using the ‘mv’ command using a terminal or by following the GUI method mentioned in this article.

Источник

How to move all files and folders via mv command [duplicate]

Good point. If you are using bash, then you can run shopt -s dotglob and then «*» will match hidden files, too.

What happens if there are folders and files with the same name in the destination folder? Are they overwritten?

. it seems folders with the same name are not overwritten. mv: cannot move ‘/a/js’ to ‘/b/js’: Directory not empty

I know I accepted the first answer many years ago, and it’s stupid for me now to change it. But actually, I’ve been always using this method.

This works for me in Bash (I think this depends on your shell quite a bit. )

$ mv source/* /destination/folder/here 

When I try I get mv: overwrite ‘destination/.’? mv: overwrite ‘destination/..’? , but adding -n to mv stops it from trying to overwrite

@Putnik — that’s a good gotcha! what os/distro ? ( I was working on OSX when I was messing around with this. )

This works for me in Bash 4.2.46, it moves all files and folders including hidden files and folders to another directory

Читайте также:  Kyocera printer drivers linux

Notice that .[^.]* means all hidden files except . and ..

I’d say it’s a bit boring, but really bullet-proof (GNU) way is:

cd /SourceDir && find ./ -maxdepth 1 -mindepth 1 -exec mv -t /Target/Dir <> +

P. S. Now you can possibly see why lots of people do prefer Midnight Commander, though.

If you only want to do a cut and paste-like action there is a simple way that worked for me:

$mv /media/dir_source $HOME/Documents/ 

It will move the folder named dir_source located in /media to the directory $HOME/Documents/

yet another way just for the heck of it (because I love convoluted ways to do things I guess)

cd /source for f in $(\ls -QA); do eval mv $f /destination/$f; done 

the -Q and the -A are not POSIX, however the -A is fairly prevalent, and to not use the -Q you need to change the IFS (which then means you don’t need the eval but need to quote the variable)

IFS=" " && for f in $(ls -A); do mv "$f" /destination/"$f"; done 

Источник

How to move all files including hidden files into parent directory via *

Its must be a popular question but I could not find an answer. How to move all files via * including hidden files as well to parent directory like this:

This will move all files to parent directory like expected but will not move hidden files. How to do that?

this question has a duplicate at SU, with an even more correct answer (not the accepted one though): cp -r /path/to/source/. /destination

9 Answers 9

You can find a comprehensive set of solutions on this in UNIX & Linux’s answer to How do you move all files (including hidden) from one directory to another?. It shows solutions in Bash, zsh, ksh93, standard (POSIX) sh, etc.

You can use these two commands together:

mv /path/subfolder/* /path/ # your current approach mv /path/subfolder/.* /path/ # this one for hidden files 
mv /path/subfolder/* /path/subfolder/.* /path/ 

(example: echo ab expands to a.b ab )

Note this will show a couple of warnings:

mv: cannot move ‘/path/subfolder/.’ to /path/.’: Device or resource busy mv: cannot remove /path/subfolder/..’: Is a directory 

Just ignore them: this happens because /path/subfolder/* also expands to /path/subfolder/. and /path/subfolder/.. , which are the directory and the parent directory (See What do “.” and “..” mean when in a folder?).

If you want to just copy, you can use a mere:

cp -r /path/subfolder/. /path/ # ^ # note the dot! 

This will copy all files, both normal and hidden ones, since /path/subfolder/. expands to «everything from this directory» (Source: How to copy with cp to include hidden files and hidden directories and their contents?)

The braces are just a short cut for mv /path/subfolder/* /path/subfolder/.* /path/ , not strictly necessary to combine the two commands into one.

I get the following error: mv: overwrite `/path/.’? y mv: cannot move `/path/subfolder/.’ to `/path/.’: Device or resource busy mv: overwrite `/path/..’? y mv: cannot move `/path/subfolder/..’ to `/path/..’: Device or resource busy

@Dejan Just ignore it. . denotes current directory and .. denotes up directory. You must have noticed that all other files are moved.

«Just ignore the warning» may not always be a good idea. Right now I’m having a problem with a script in which I need to stop execution if any step fails — since this solution always causes an error, it kills my script. I need a way to determine if the mv command failed or not.

Читайте также:  How to install all packages in linux

* works fine as a pattern to match hidden and unhidden files excluding . and .. but always returns itself as well even if there are some matching files. This seems to be a bug and can be worked around by setting nullglob ( shopt -s nullglob ). But if one does that one could set dotglob instead which seems favorable to me. Either should probably only be enabled temporarily.

I think this is the most elegant, as it also does not try to move .. :

mv /source/path/* /destination/path 

this would miss files like ..anything or . anything etc. — stackoverflow.com/a/31438355/2351568 contains the correct regex for this problem. || but anyway using shopt -s dotglob is still the better solution!

@DylanB don’t memorize it. remember that it matches whatever is in the curlybrackets, separated by commas. * would find all files starting with a or b such as «anatomy» and «bulldozer». The second match is just an empty match, equivalent to * , and the first match is equivalent to .[!.] , where the group [!.] means a group NOT starting with a . . This means .* but not ..* .

This will move all files to parent directory like expected but will not move hidden files. How to do that?

You could turn on dotglob :

shopt -s dotglob # This would cause mv below to match hidden files mv /path/subfolder/* /path/ 

In order to turn off dotglob , you’d need to say:

Very helpful. Wanted to find out more but shopt is a builtin so man shopt doesn’t work and help shopt is very brief. But you can do bashman () < man bash | less -p "^ $1 "; >and then bashman shopt to read all about it straightforwardly. (Might have to hit n to jump down to the command if there are lines starting with shopt, as I found.)

By using the find command in conjunction with the mv command, you can prevent the mv command from trying to move directories (e.g. .. and . ) and subdirectories. Here’s one option:

find /path/subfolder -maxdepth 1 -type f -name '*' -exec mv -n <> /path \; 

There are problems with some of the other answers provided. For example, each of the following will try to move subdirectories from the source path:

1) mv /path/subfolder/* /path/ ; mv /path/subfolder/.* /path/ 2) mv /path/subfolder/* /path/ 3) mv /source/path/* /destination/path 

Also, 2) includes the . and .. files and 3) misses files like ..foobar, . barfoo, etc.

You could use, mv /source/path/* /destination/path , which would include the files missed by 3), but it would still try to move subdirectories. Using the find command with the mv command as I describe above eliminates all these problems.

Alternative simpler solution is to use rsync utility:

sudo rsync -vuar --delete-after --dry-run path/subfolder/ path/ 

Note: Above command will show what is going to be changed. To execute the actual changes, remove —dry-run .

The advantage is that the original folder ( subfolder ) would be removed as well as part of the command, and when using mv examples here you still need to clean up your folders, not to mention additional headache to cover hidden and non-hidden files in one single pattern.

In addition rsync provides support of copying/moving files between remotes and it would make sure that files are copied exactly as they originally were ( -a ).

The used -u parameter would skip existing newer files, -r recurse into directories and -v would increase verbosity.

Источник

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