How to move all files from current directory to upper directory?
How to move all files from current directory to upper directory in linux? I tried something like mv *.* , but it doesn’t work.
I don’t have enough rep to retag questions, but may I suggest [linux] [mv] [cwd] [files] , or something similar?
11 Answers 11
The command you are looking for is
or (see below for more info):
Explanation: the mv command moves files and directories. The last argument to mv is the target (in this case the directory one step «up» in the tree, .. ). The arguments before that are the source files and directories. The asterisk ( * ) is a wildcard which matches all files which do not start with a dot. Files that start with a dot (dotfiles) are «hidden». They are matched using the pattern .[^.]* (see edit below).
See the manpage which I linked for more information on mv .
Why .[^.]* instead of .* ?
As Chris Johnsen correctly points out: the pattern .* also matches . and .. . Since you don’t want to (and cannot) move those, it’s better to use a pattern which matches any filename starting with a dot except those two. The pattern .[^.]* does just that: it matches any filename (1) starting with a dot (2) followed by a character which is not a dot (3) followed by zero or more arbitrary characters.
As Paggas points out, we’d also have to add the pattern . * in order to match files starting with two dots. See his answer for an alternative solution using find .
Arjan’s answer mentions shopt in order to avoid all those issues with dotfiles. But then there is still the problem with files starting with a dash. And it requires three commands. Still, I like the idea. I propose to use it like this:
This executes shopt in a subshell (thus no second call to shopt required) and uses — so that files starting with a dash will not be interpreted as arguments to mv .
How do I move files and directories to the parent folder in Linux?
when it tries to move . (current directory) but that won’t cause any harm.
It will move all files from all subdirectories to the parent of the current directory, too. I’d use -maxdepth 1 to be sure.
You must have a directory called scripts in your parent directory AND in your current directory. You will have to rename this one before you move it.
It worked but you left one one very important bit of information — you must run this from the subdirectory. Also this will not delete the subdirectory itself so you must back up one directory and do a rmdir on the subdirectory.
I found this superior: find . -mindepth 1 -maxdepth 1 -exec mv -t .. — <> + (taken from unix.stackexchange.com/q/6393/93768). No error messages and actually working in my bash script.
I came here because I’m new to this subject as well. For some reason the above didn’t do the trick for me. What I did to move all files from a dir to its parent dir was:
It can’t be more simple than:
To also move hidden files:
mv is a command to move files, * means all files and folders and ../ is the path to the parent directory.
That moves ALL the files one level up.
The character * is a wildcard. So *.deb will move all the .deb files, and Zeitgeist.* will move Zeitgeist.avi and Zeitgeist.srt one folder up, since, of course, .. indicates the parent directory.
To move everything including folders, etc, just use * instead of *.*
zsh: argument list too long: mv ; bash: /usr/bin/mv: Argument list too long . Aparently it doesn’t work for 80k files
There is no need to change directories. Just include * at the end of path:
mv /my/folder/child/* /my/folder/
Above only moves non hidden files. To move only hidden files use .*
mv /my/folder/child/.* /my/folder/
Above two can be combined in to one command:
mv /my/folder/child/* /my/folder/
Assuming all your hidden files begin with dot followed by a letter or a number (which they should), you could use
The .[A-Za-z0-9]* part is to make sure you don’t try to move . or .. along, which would fail.
In bash you can use shopt -s dotglob to make * match all files and move them simply by
This is not the best solution since the setting is permanent for the shell until you change it by
but I think it’s good to know.
Call it in a subshell: (shopt -s dotglob && mv * ..) . That way, the option is only local to that subshell.
Good answer — it’s simple, includes hidden files and doesn’t cause an error about copying ‘.’ and ‘..’
A method which causes no errors and works every time:
ls -1A . | while read -r file do mv "./$" .. done
find . -maxdepth 2 -type f -exec mv <> .. \;
I used a variation of above to move all the files from subfolders into the parent.
I’d got data in folders by year, but found by using metadata I could have them all in the same folder which made it easier to manage.
/data/2001/file_1 /data/2002/file_2 /data/2003/file_3
It’s simple to move all files and folders to the parent directory in Linux.
Go to that folder and use this command:
For example, if your files and folders are as follows:
/home/abcuser/test/1.txt 2.txt 3.jpg 4.php 1folder 2folder
cd /home/abcuser/test mv * /home/abcuser
All your files and folders will move to the abcuser folder (parent directory).
Thanks @Gareth, was about to the same. Abhishek, please don’t post any unrelated links, where’s the sense in that? Also, check your formatting please. Additionally, /the full path does not work in Linux, you have to escape spaces with /the\ full\ path .
find -type f|while read line; do mv $line $; done
Thanks for contributing an answer. While this might work in simple scenarios, piping find into while read is a bad way to use find , and better answers have already been posted.
There’s a lot of answers to this question, which proves the flexibility of Bash commands. However, to me a very simply solution that does the trick nicely is this one:
mv move; * select all files and folders; .[^.]* collects up the hidden files, with one dot at the start of their name; . * will select files that start with two dots followed by other characters; .. is the destination, which in this case is the parent folder.
Note, using .[^.]* and . * will ensure you only select those files with a single dot followed by something other than a dot, and those with two dots followed by other characters.
If you’d like to avoid trying to remember this, I suggest setting up an alias, such as, alias mvallup=»mv * .[^.]* . * ..» . Add this to ~/.bash_aliases . Now you can just type mvallup and it’s a done deal.
A shorter version of this was suggested by William Edwards, but it didn’t include hidden files. He gave an example for also moving hidden files, but that was not exampled as simply as it could have been ( mv /path/subfolder/* /path/ ) hence why I’m posting this very simple option.
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).
@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.