How do I move files and directories to the parent folder in Linux?

84

29

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

nekbaba

Posted 2009-12-27T17:25:21.277

Reputation:

the question with by far the most complete answer i found: https://unix.stackexchange.com/q/6393/93768

– DJCrashdummy – 2018-09-06T18:39:32.427

Answers

72

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.

John T

Posted 2009-12-27T17:25:21.277

Reputation: 149 037

1It 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. – crafter – 2016-05-10T16:50:54.817

1It will move all files from all subdirectories to the parent of the current directory, too. I'd use -maxdepth 1 to be sure. – ℝaphink – 2009-12-27T17:36:40.300

1Now it says: mv: cannot move ./scripts' to../scripts': Directory not empty – None – 2009-12-27T17:43:29.023

1You 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. – ℝaphink – 2009-12-27T17:44:49.940

96

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

Ben Fransen

Posted 2009-12-27T17:25:21.277

Reputation: 1 121

14This does not move hidden file though – Wavesailor – 2015-09-10T10:51:14.720

1 liner: (cd ${ANDROID_NDK_HOME}/android-ndk-r14b/ && mv * ../) – Dawid Drozd – 2017-12-07T11:51:02.813

10

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

Gil

Posted 2009-12-27T17:25:21.277

Reputation: 121

Its a nice documentary – BlackBurn027 – 2016-11-17T06:36:27.173

It works with dirs, at least for me. – maaartinus – 2011-01-25T21:21:46.157

5You want * not *.* to include directories – Chris S – 2013-04-19T19:58:41.073

3this didn't work with the dirs! or the hidden files – None – 2009-12-27T17:34:44.860

6

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.

William Edwards

Posted 2009-12-27T17:25:21.277

Reputation: 303

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.

maaartinus

Posted 2009-12-27T17:25:21.277

Reputation: 3 054

Good answer - it's simple, includes hidden files and doesn't cause an error about copying '.' and '..' – Daniel Howard – 2017-11-09T13:06:36.113

4Call it in a subshell: (shopt -s dotglob && mv * ..). That way, the option is only local to that subshell. – Martin Ueding – 2013-01-26T20:25:57.573

1

A method which causes no errors and works every time:

ls -1A . | while read -r file                                                    
do                                                                                  
    mv "./${file}" ..                                                            
done

djhaskin987

Posted 2009-12-27T17:25:21.277

Reputation: 349

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

Bill Bixby

Posted 2009-12-27T17:25:21.277

Reputation: 11

0

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 *

Shital Shah

Posted 2009-12-27T17:25:21.277

Reputation: 159

0

find -type f|while read line; do mv $line ${line##*/}; done

Adler

Posted 2009-12-27T17:25:21.277

Reputation: 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. – Scott – 2018-12-13T16:29:29.250

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

Abhishek

Posted 2009-12-27T17:25:21.277

Reputation: 1

2Thanks @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. – slhck – 2011-11-03T11:47:00.910

0

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.

ℝaphink

Posted 2009-12-27T17:25:21.277

Reputation: 3 531

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

M Ikram

Posted 2009-12-27T17:25:21.277

Reputation: 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. – DavidPostill – 2016-05-20T10:46:32.360