138

In Linux (Ubuntu), how do you move all the files and directories to the parent directory?

slhck
236k73 gold badges639 silver badges611 bronze badges
asked Dec 27, 2009 at 17:25
1

16 Answers 16

121
find . -maxdepth 1 -exec mv {} .. \;

this will move hidden files as well.

You will get the message:

mv: cannot move `.' to `../.': Device or resource busy

when it tries to move . (current directory) but that won't cause any harm.

answered Dec 27, 2009 at 17:29
6
  • 1
    It will move all files from all subdirectories to the parent of the current directory, too. I'd use -maxdepth 1 to be sure. Commented Dec 27, 2009 at 17:36
  • 1
    Now it says: mv: cannot move ./scripts' to ../scripts': Directory not empty Commented Dec 27, 2009 at 17:43
  • 1
    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. Commented Dec 27, 2009 at 17:44
  • 6
    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. Commented May 10, 2016 at 16:50
  • 1
    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. Commented Oct 18, 2020 at 9:00
149

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:

cd to/the/dir
mv * ../
answered Jan 26, 2013 at 20:20
4
  • 24
    This does not move hidden file though Commented Sep 10, 2015 at 10:51
  • 1 liner: (cd ${ANDROID_NDK_HOME}/android-ndk-r14b/ && mv * ../) Commented Dec 7, 2017 at 11:51
  • you save my day.Thanks Commented Aug 12, 2020 at 10:01
  • This will break on large file sets due to command line limitations Commented Nov 2 at 20:17
22

It can't be more simple than:

mv * ../

To also move hidden files:

mv /path/subfolder/{.,}* /path/ 

mv is a command to move files, * means all files and folders and ../ is the path to the parent directory.

answered Jul 16, 2014 at 18:50
0
14

Type this in the shell:

mv *.* ..

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

robinCTS
4,4174 gold badges22 silver badges30 bronze badges
answered Dec 27, 2009 at 17:27
5
  • 4
    this didn't work with the dirs! or the hidden files Commented Dec 27, 2009 at 17:34
  • It works with dirs, at least for me. Commented Jan 25, 2011 at 21:21
  • 7
    You want * not *.* to include directories Commented Apr 19, 2013 at 19:58
  • 1
    Its a nice documentary Commented Nov 17, 2016 at 6:36
  • zsh: argument list too long: mv; bash: /usr/bin/mv: Argument list too long. Aparently it doesn't work for 80k files Commented Jan 27, 2022 at 18:04
4

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/

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

answered Apr 11, 2016 at 4:38
2

Assuming all your hidden files begin with dot followed by a letter or a number (which they should), you could use

mv * .[A-Za-z0-9]* ..

The .[A-Za-z0-9]* part is to make sure you don't try to move . or .. along, which would fail.

answered Dec 27, 2009 at 17:48
2

In bash you can use shopt -s dotglob to make * match all files and move them simply by

shopt -s dotglob; mv * ..

This is not the best solution since the setting is permanent for the shell until you change it by

shopt -u dotglob

but I think it's good to know.

answered Jan 25, 2011 at 21:33
2
  • 4
    Call it in a subshell: (shopt -s dotglob && mv * ..). That way, the option is only local to that subshell. Commented Jan 26, 2013 at 20:25
  • Good answer - it's simple, includes hidden files and doesn't cause an error about copying '.' and '..' Commented Nov 9, 2017 at 13:06
2

As outlined by others, mv * ../ is the most straight forward way to copy all files from a sub directory to its parent directory.

However, if the list of files to copy is very large, you may run into this error:

-bash: /bin/mv: Argument list too long (see this article for more info).

Then you can use the other suggestion:

find . -mindepth 1 -maxdepth 1 -exec mv {} .. \;

Note the addition of -mindepth 1. Then the warning "mv: cannot move .' to ../.': Device or resource busy" does not appear because the current directory is not included.

Note: you have to execute these commands from the sub directory.

answered Oct 25, 2022 at 9:40
1

A method which causes no errors and works every time:

ls -1A . | while read -r file 
do 
 mv "./${file}" .. 
done
answered Jul 25, 2012 at 20:15
1
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.

eg.

/data/2001/file_1
/data/2002/file_2
/data/2003/file_3
answered Jul 16, 2014 at 18:37
0

It's simple to move all files and folders to the parent directory in Linux.

Go to that folder and use this command:

mv * /the full path

For example, if your files and folders are as follows:

/home/abcuser/test/1.txt 
 2.txt
 3.jpg
 4.php
 1folder
 2folder

Go to that folder via cd:

cd /home/abcuser/test
mv * /home/abcuser

All your files and folders will move to the abcuser folder (parent directory).

Gareth
19.1k15 gold badges60 silver badges70 bronze badges
answered Nov 3, 2011 at 11:39
1
  • 2
    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. Commented Nov 3, 2011 at 11:47
0
find -type f|while read line; do mv $line ${line##*/}; done
answered Dec 13, 2018 at 14:22
1
  • 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. Commented Dec 13, 2018 at 16:29
0

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 * .[^.]* .??* ..

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.

answered Jul 3, 2020 at 6:28
0

One liner to combine find and mv

Find folder in which we are interested see find

For each such folder we execute:

echo "Moving $found_folder" # Just print
(cd "$found_folder" && mv * ../); # enter dir AND move content to parent
rmdir "$found_folder" # remove dir, this will be done only when dir is empty so nice check is everything fine
for found_folder in $(find . -type d -wholename '*/config/my_catchy_name'); do echo "Moving $found_folder"; (cd "$found_folder" && mv * ../); rmdir "$found_folder"; done;
  • Moves sub dirs (when destination doesn't have already such dirs)
  • Moves hidden files
  • Moves symbolic links without making mess
answered Oct 25, 2021 at 7:12
0

Simple and memorisable:

mv ./{.,}* ..
answered Apr 10, 2023 at 12:43
-1

switch to sub directory and execute following command for copy or move files.

ex: a is parent directory and b is sub directory, we want to move/copy all files from b to a (sub directory to parent directory).

cd b
cp * ..
mv * ..
techraf
4,97211 gold badges27 silver badges44 bronze badges
answered May 20, 2016 at 6:32
1
  • Welcome to Super User! This duplicates another answer and adds no new content. Please don't post an answer unless you actually have something new to contribute. Commented May 20, 2016 at 10:46

You must log in to answer this question.