Move all file linux command

How to move multiple files at once to a specific destination directory?

I got a bunch of files in some directory (along with many other files) that I want to move. Luckily, all the files I want to move contain a certain identifier in their names, so I can ls | grep IDENTIFIER to get the exact list of files to move. But, how can I execute mv file /path/to/dest/folder/ at once, and not one by one (there’s a lot of files to move)?

15 Answers 15

mv -t DESTINATION file1 file2 file3 

The following also works, but I’m not sure if mv is invoked multiple times or not, as grep will output a new line for each match:

mv -t DESTINATION `ls|grep IDENTIFIER` 

Does not work on Mac (10.11.16 El Capitan). But you can simply put the target folder at the back, i.e. mv file1 file2 . destination

If you want to move ABC-IDENTIFIER-XYZ.ext or IDENTIFIER-XYZ.xml , you can use:

* is a wildcard for zero or more characters, this means zero or more characters, followed by IDENTIFIER , followed by zero or more characters.

This will move all the files that contain the IDENTIFIER you specified.

Fore example, to move all files having a .doc extension:

This will move all doc file under the current directory to the specific destination.

but the list of files to move is not determined by extension. some of the files are named: ABC-IDENTIFIER-XYZ.ext and some just IDENTIFIER-XYZ.ext all having different extensions, mostly xml or properties .

mv *.ext *.xml *.txt /path/to/dest/folder/ 

but the list of files to move is not determined by extension. some of the files are named: ABC-IDENTIFIER-XYZ.ext and some just IDENTIFIER-XYZ.ext all having different extensions, mostly xml or properties .

If you want to move a set of arbitrary files (no common pattern in the names and types) you can do as Mr. Rajanand said: first go to the directory that contains the files you want to move

mv file1.ext1 file2.ext2 file3.ext3 /destination/ 

In case the files are scattered in different directories, you only need to specify the path for each file in the mv command.

If the files are in the same dir you can use

mv /path/to/source/dir/ /path/to/destination/ 

I use tuomaz’s technique, but slightly modified:

mv file1 file2 file3 -t DESTINATION 

I find this easier to remember and harder to screw up since it uses the same ordering as the vanilla mv operation:

mv `ls|grep IDENTIFIER` /path/to/dest/folder 

However, ls is not recommended for this kind of use. Use find command instead.

ls is not recommended for this kind of use. If you want to list files, especially with a grep behind, use find . -name \*IDENTIFIER\* .

This answer was just to demonstrate how you can use the output of previous command in mv. As ls|grep was mentioned in the question, I just copied it.

find -type f -name "[range]" -exec mv <> target-directory ';' 

this command will move file names with any pattern/range to target-directory.

find -type f -name "file10376" -exec mv <> target-directory ';' 

it will move files with names like file1 , file2 . file50000 to target-directory .

given the example in the question, I just want to put a note here on character classes — [range] (literally) will match r or a or n or g or e, so [IDENTIFIER] (whatever OP’s identifier is) would probably not do the thing expected. Better to run find without -exec first to see what files will be operated on.

Читайте также:  Virtualbox linux mint образ

If you have so many files to move you can actually have too many for the mv command (or other commands like rm ). I suggest using xargs to move each file individually in a loop like fashion. One way to get around that is to do:

ls -1 | grep IDENTIFIER | xargs -i mv <> /path/to/dest/folder/ 

The ls -1 (minus one) ensures that there is only one filename on each line. If you have hidden aliases for the ls command you can have multiple filenames on a single line and inadvertently move a file you did not intend to move.

Источник

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.

* 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.

Источник

Читайте также:  Linux ubuntu program files

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?

I know this is old question, but what about «rsync -az src/ dest/» it will copy all files which do not exist in dest/ directory from src/, and then just remove destination directory with «rm -rf dest/»

6 Answers 6

-f, --force do not prompt before overwriting 

mv -f /a/b /c/d moves b inside d (so I have /c/d/a ) instead of overwriting d with a (I want /c/a ). How can I do such overwrite?

@Xenos you would need to delete the file d before you can create a directory d . That would require a couple of commands, there’s no force overwrite of a directory with a file that I know of. You could script that with just a few lines, but it would be better taken up as a separate question since it deviates from question the OPasked.

It’s just mv srcdir/* targetdir/ .

If there are too many files in srcdir you might want to try something like the following approach:

cd srcdir find -exec mv <> targetdir/ + 

In contrast to \; the final + collects arguments in an xargs like manner instead of executing mv once for every file.

just a note: on directories with a couple of thousands files, you wont be able to use the * since the argument list will be too long for the shell to handle, so you would need to use «find» instead or similar solution

I’ve tried the following command «find . -exec mv <> ../clean-copy/Ressources/img/ +» but got the following error : «find: -exec: no terminating «;» or «+»» — Any idea why ?

@musiKk Thanks for the tip. Although my comment is a question, it’s here because I feel the answer is 99% helping but missing the 1% that would make it perfect.

It’s also possible by using rsync , for example:

rsync -va --delete-after src/ dst/ 
  • -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.

yup, just lost all my files. Shame on me for thinking people online know what they are talking about.

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.

Источник

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