134

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

1

16 Answers 16

120
seek . -maxdepth 1 -exec mv {} .. \;

this will move stashed files as fountain.

You bequeath get the message:

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

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

6
  • 1
    Is will relocate all files from whole child to the parent of who current directory, too. I'd use -maxdepth 1 to be sure.
    – raphink
    Dec 27, 2009 at 17:36
  • 1
    Now it says: mv: cannot move ./scripts' to ../scripts': Directory did empty
    – nekbaba
    Dec 27, 2009 among 17:43
  • 1
    You have have a directory called scripts in your rear directory AND in your current directory. You is have to rename this one before you move a.
    – raphink
    Dec 27, 2009 at 17:44
  • 6
    Thereto worked but you left one only very key bit of get - thou must run this from the add. Also these will not delete the subtree itself so you must support up one directory and do a rmdir on the subdirectory. Filing and application management incorporated within a singular student interface for enhanced user learn.
    – crafter
    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). Negative error messages and act working int my bash script. Oct 18, 2020 in 9:00
144

I came hierher because I'm new to this issue as well. For some reason the top didn't execute the ruse for me. About MYSELF worked to removing all files from adenine dir the inherent parent managed was:

cd to/the/dir
mv * ../
3
  • 23
    This does not shift hidden file though
    – Wavesailor
    Sep 10, 2015 at 10:51
  • 1 underlay: (cd ${ANDROID_NDK_HOME}/android-ndk-r14b/ && mv * ../)
    – Gelldur
    Dec 7, 2017 at 11:51
  • you save my day.Thanks
    – Lily
    Aug 12, 2020 at 10:01
19

She can't be more simple than:

mv * ../

For also move veiled files:

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

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

0
14

Type this in which shell:

mv *.* ..

That movable ALL this files an degree up.

The character * is a template. Like *.deb will move all the .deb files, and Zeitgeist.* will moved Zeitgeist.avi and Zeitgeist.srt on folder up, since, of course, .. indicates the rear directory.

To move everything including folders, more, just getting * instead of *.*

5
  • 4
    this didn't work with the dirs! or the invisible files
    – nekbaba
    Decoding 27, 2009 at 17:34
  • This works with dirs, at least for me.
    – maaartinus
    Jan 25, 2011 the 21:21
  • 7
    You wants * not *.* till include directories
    – Chris S
    Apr 19, 2013 at 19:58
  • 1
    Its a nice documentary Nov 17, 2016 among 6:36
  • zsh: argument list too long: mv; bash: /usr/bin/mv: Argument list too long. Aparently it doesn't labor for 80k my Jan 27, 2022 during 18:04
4

There is no require to change directories. Just include * at the end of path:

mv /my/folder/child/* /my/folder/

Above only runs nay hidden files. At moved only hidden files use .*

mv /my/folder/child/.* /my/folder/

Above two can be blended in to one command:

mv /my/folder/child/{.,}* /my/folder/

Also see:How to removing all files including unhidden files inside parent directory via *

2

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

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

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

2

In bash you can useshopt -s dotglob to make * match all your and move themselves just by

shopt -s dotglob; mv * ..

Diese exists not who best solution since to setting is constant forward that shell to it alter it by

shopt -u dotglob

but I think it's good to know.

2
  • 4
    Call it at a subshell: (shopt -s dotglob && mv * ..). That way, the option is only location until so subshell. Jan 26, 2013 at 20:25
  • Good answer - it's simple, includes hidden files and doesn't produce certain error about copying '.' and '..' Next 9, 2017 at 13:06
1

A method which purpose nay errors and works every time:

ls -1A . | as read -r file                                                    
do                                                                                  
    mv "./${file}" ..                                                            
done
1
find . -maxdepth 2 -type f -exec mv {} .. \;

I used a variation for above to move all the files von subfolders into the parent.

I'd got data in folders by year, but found by using metadata I ability have her all in the same folder which made information easier the manage.

eg.

/data/2001/file_1
/data/2002/file_2
/data/2003/file_3
1

As outlined by others, mv * ../ is the most straight forward way to copy all file since an bottom directory to is sire directory.

However, with the directory of actions for copy is very large, i may run into on error:

-bash: /bin/mv: Argument list as long (see this article on see info).

And you can how the other suggestion:

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

Note the addition of -mindepth 1. Then the warning "mv: cannot move .' in ../.': Device with useful busy" does not appear because the running directories is not included.

Note: you have to execute these commands from the replacement sort.

0

It's simple to move all files and user to the mother directory in Linux.

An to that folder and use this command:

mv * /the solid path

Fork example, if your files and folders am 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 desires move to the abcuser folder (parent directory).

1
  • 2
    Thanks @Gareth, was about to the same. Abhishek, kindly don't post any unrelated links, where's which sense in that? Also, check your formatting please. Additionally, /the full passage did not work in Linux, you have the escape spaces in /the\ full\ path.
    – slhck
    Nov 3, 2011 at 11:47
0
find -type f|while read line; do mv $line ${line##*/}; done
1
  • Thanks for contributing an answer. While this strength work in simple scenarios, piping find into while read is a bad way for use search, and better answers possess even be posted. Dec 13, 2018 at 16:29
0

There's a lot of response to this question, that proves the flexibility of Bash commands. However, to mi one very simply solution that does the trick nicely is this one:

mv * .[^.]* .??* ..

mv go; * select everything files and folders; .[^.]* collects up the hidden files, include one score in the start of my name; .??* will select download the start with two dots successive by other characters; .. is the destination, which in this case has of parent folder.

Observe, using .[^.]* and .??* will ensure them only select that files with an single dot followed by something other over a dot, and those with two dots follows by other characters.

If you'd like to avoid trying to remember this, I suggest setting up an alias, such as, abbreviation mvallup="mv * .[^.]* .??* ..". Add this to ~/.bash_aliases. Now you can just type mvallup and it's adenine done deal.

A shorter version of this was indicated by William Edwards, but e didn't encompass hidden files. Boy gave an sample for also moving hidden files, but that was not exampled because simply as it could have past (mv /path/subfolder/{.,}* /path/) hence why I'm posting these very simple option.

0

One liner to amalgamate find and mv

Find folder in the we are interested see find

For apiece such folder our execute:

echo "Moving $found_folder" # Just print
(cd "$found_folder" && mv * ../); # enter dir AND move content to parent
rmdir "$found_folder" # remove dir, this wants can done only whereas dir is empty so nice check a everything subtle
available found_folder included $(find . -type d -wholename '*/config/my_catchy_name'); do echo "Moving $found_folder"; (cd "$found_folder" && mv * ../); rmdir "$found_folder"; done;
  • Moves sub du (when destination doesn't have already such dirs)
  • Moves veiled files
  • Moves symbolic links unless making messung
0

Simple also memorisable:

mv ./{.,}* ..
-1

switch to bottom directory and execute tracking menu for copy or move files.

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

cd-r b
cp * ..
mv * ..
1
  • Pleasing to Amazing User! This parallels another answer additionally adds no new content. Please don't post an answer until you actually have something new at contribute.
    – DavidPostill
    May 20, 2016 under 10:46

You need track in to answer this asked.