Linux move all files and folders to another folder

How to move all files in current folder to subfolder?

Now I want to move all files and folders/sub-folders in the downloads folder to the sub-folder. how can I do this? I tried:

You do not need -R. Just mv * new Though, it will whine «cannot move new to new» or something similar. Perhaps mv -i * new to you prevent overwrites.

6 Answers 6

should do the trick. If it doesn’t work, run shopt -s extglob first.

To also move hidden files/directories (that beginning with a dot), run also shopt -s dotglob first.
So, to sum up:

shopt -s extglob dotglob mv !(new) new shopt -u dotglob 

(it is always better to unset dotglob to avoid bad surprises).

@adeer that is just saying not(something). In other words, copy everything but not something. Clear as mud?

I don’t get it, e.g. what If I try to move the content of the folder dev to my current location, do I have to execute mv !(dev) dev ?? I get -bash: !: event not found

@adeer very similar. setopt extendedglob to set the extended glob mode in zsh. ^ is the exclusion symbol, so mv ^new new would do the trick. then to see all the options set in zsh you can use setopt and to disable extended glob mode you can do unsetopt extendedglob

I found something like this but is a bit simpler to understand, and it might work well for you too:

ls | grep -v new | xargs mv -t new 

Adding an explanation to the above solution:

-t, --target-directory=DIRECTORY move all SOURCE arguments into DIRECTORY 
-v, --invert-match Invert the sense of matching, to select non-matching lines. 
  • ls will list the files in current directory
  • grep -v new will return piped to that is not match new
  • xargs mv -t new will move the files piped to it from grep -v to the target directory

@ConstantFun It could be zsh but I would guess it’s your implementation of mv , (meaning your version of mv is different them what I was using) what os are you using? I believe when I wrote this answer I was on linux distro centos using bash .

Just testing this on osx and I get the same error mv: illegal option — t which is because osx’s mv does not have a -t option.

(I have not tried this, and don’t plan on it.) This link apple.stackexchange.com/questions/69223/… tells you how to install the gnucore which should help you get gnu core cli utils, osx ships with bsd cli tools which are different the the ones that come with linux.

@TheFool that is because Alpine linux uses busybox and the busybox implementation of mv does not have -t as an option. github.com/brgl/busybox/blob/master/coreutils/mv.c

Just use mv * subdir1 and ignore the warning.

You can just use mv * subdir1 . You will see a warning message relating to trying to move subdir1 into itself, like this:

mv: cannot move 'subdir1' to a subdirectory of itself, 'subdir1/subdir1' 

But it will move all of the other files and directories to subdir1 correctly.

$ ls $ mkdir dir1 dir2 dir3 $ touch file1.txt file2.txt file3.txt $ mkdir subdir1 $ ls #=> dir1 dir2 dir3 file1.txt file2.txt file3.txt subdir1 $ mv * subdir1 #=> mv: cannot move 'subdir1' to a subdirectory of itself, 'subdir1/subdir1' $ ls #=> subdir1 $ ls subdir1 #=> dir1 dir2 dir3 file1.txt file2.txt file3.txt 

If you really don’t like the warning (maybe this is in a bigger script where the warning would be confusing), mv * subdir1 2>/dev/null . This is not always desirable, though, since there might be other errors reported by mv (e.g. if you don’t have permissions).

Читайте также:  Cloud storage server linux

@NotSoShabby Interesting! Thanks for posting. I didn’t even think of that. Any idea how those can be included as well?

Simple idea. Assuming you are in /myuser, rename downloads to new, create a new downloads directory then move new into it.

mv downloads new # downloads is now called new mkdir downloads # create new directory downloads mv new downloads # move new into it. 

If you want to move all the files from a folder to one of its subfolders you can use the following command:

find /myuser/downloads/ -type d -name 'new' -prune -type f | xargs mv -t /myuser/downloads/new 

It will find all the files and then move them to your subfolder.

@waltinator: added -type d -name ‘new’ -prune to prevent traversal of /myuser/downloads/new .

I created a subdirectory named new and tried this command, I get mv: missing file operand and Try ‘mv —help’ for more information

You can try this alternative process –– remain in the path

but, instead of first creating the /myuser/downloads/new/ directory, instead create a folder in the /myuser/ directory, with the command mkdir ../new , then move all the files in downloads to new , and finally move new into downloads . You can do this in one line, while in the /myuser/downloads/ path, with the command:

mkdir ../new && mv * ../new && mv ../new ../downloads 

In this case, you don’t have to worry about any sort of «filtering» of files/folders, since new is on the same level of the path as downloads , so you can just move everything in downloads to new , and then move new into downloads`.

However, if you already have the subfolder new created and don’t want to create another one, not to worry –– just change the mkdir command on the left-hand side of the first && in the command shown above to an mv command, pushing new up in the path; in other words, while you’re still in /myuser/downloads/ , you can change mkdir ../new to mv new .. . Then the subfolder new [in the path /myuser/downloads/new/ ] gets pushed up to /myuser/new/ , at the same level as /myuser/downloads/ , and then you can run the rest of the command as it is shown above. All together, we have, starting from the path /myuser/downloads/ :

mv new .. && mv * ../new && mv ../new ../downloads 

and, since you wanted to «move all files and folders/sub-folders in the downloads folder to the sub-folder [ new ]», you’re done! If you had wanted to move only files (or only folders or [insert more granular object movement]), then you’d have to use other commands that can «filter» objects, such as grep . The commands written above are sufficient for your purposes, though.

Источник

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.

Читайте также:  Linux works on which platform

You’re asking about moving files but people showing you how to move not only files but folders as well. Is that OK?

@Hontvári Levente gave an answer a year ago that seems to be by far the best, clean, simple, and it works. So how did it get only 3 votes (as compared to 262 for the answer currently at the top)?

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

find ~/Downloads/ -type f -print0 | xargs -0 mv -t ~/Videos 

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:

find ~/Downloads/ -maxdepth 1 -type f -print0 | xargs -0 mv -t ~/Videos 

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

Nb. your -print0 | xargs -0 mv -t ~/Videos can be more efficiently done with -exec mv -t ~/Videos \ \+ 🙂

for hidden files trick folder/.* (note the dot) works — it says some busy errors, but the move still happens => source: askubuntu.com/a/399632

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.

@MarkDoliner, yes, mv doesn’t need recursive option to include subfolders. One can use it also for renaming.

If you want to move dot (hidden) files too, then set the dotglob shell option.

shopt -s dotglob mv ~/Downloads/* ~/Videos 

This leaves the shell option set.

For one time dotglob use, run the commands in a subshell:

(shopt -s dotglob; mv ~/Downloads/* ~/Videos) 

A note for myself: The last option (shopt -s dotglob; mv ~/Downloads/* ~/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 works only under certain conditions (namely, if length of files is less than getconf ARG_MAX bytes)

Yes. On Ubuntu 20.04 getconf ARG_MAX is 2MiB. Therefore this method works if there are less than about 250,000 files in the directory.

It’s possible by using rsync , for example:

rsync -vau --remove-source-files src/ dst/ 

-v , —verbose : Increase verbosity.

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

Читайте также:  Wsl kali linux интерфейс

WARNING! the —delete-after option as noted doesn’t work the way you might expect. It doesn’t delete the source files after successful copy. IT DELETES ALL THE REMAINING/OTHER FILES IN THE DESTINATION. (as @kenorb noted. but I didn’t read carefully enough! DOH)

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.

Use for subdirectories

This command is useful when many files have been saved in a subfolder of the target directory i.e. Downloads/mp4 . In this example, running mv -T Downloads/mp4 Videos will result in mp4 subfolder being removed and all files contained inside are moved to Videos folder.

Источник

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

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 

Источник

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