How to move content of a folder to current folder?
Solution 1
mv
will overwrite files, but it will refuse to overwrite directories. There's no single command that will merge directories and remove the source directories (which is probably what you want with mv
). Even rsync --remove-source-files
will leave empty directories.
You can use a combination of commands:
cp -a dev/. .
rm -r dev
which copies everything in dev
to the current directory and then removes the dev
directory.
Or:
rsync -a --remove-source-files dev/ .
find dev -depth -type d -exec rmdir {} \;
which uses rsync
to move all the files, and then deletes the empty directories left behind.
Solution 2
Issue at Hand
You wish to move the contents of foo/bar/
up a level to foo/
.
I will be referencing this post on superuser as well as this post from serverfault in the solution.
Solution
According to user Stephan202, you are looking for the following commands to execute this task:
cd /path/to/foo/bar
mv * .[^.]* ..
It should also be possible, from within foo/bar/
, to run the following command as well:
(shopt -s dotglob; mv -- * ..)
Verify you have the correct permissions as well. If needed run the command with root(sudo) privileges.
Conclusion
Please comment if you have any questions or issues with this answer. I highly suggest you read through each link I have provided thoroughly before attempting the commands. I appreciate feedback to correct any misconceptions and to improve my posts. I can update my answer as needed.
Best of Luck!
Solution 3
Edit: I revised this answer to indicate the pros and cons.
Some manual pages are rather terse because the information is mentioned in the corresponding Info manual.
Man
-f, --force
do not prompt before overwriting
Info
Note:
mv
will only replace empty directories in the destination. Conflicting populated directories are skipped with a diagnostic.[...]
-f, --force
If a destination file exists but is normally unwritable, standard input is a terminal, and the
-f
or--force
option is not given,mv
prompts the user for whether to replace the file. (You might own the file, or have write permission on its directory.) If the response is not affirmative, the file is skipped.
mv
cannot replace non-empty directories but can overwrite existing files owned by the user which are normally unwritable (the owner do not have write permissions, c.f. the quotation).
foo
|-- ajax
`-- bar
`-- ajax
You can merge the directories foo/ajax
and foo/bar/ajax
either keeping all files or overwritting identical files as proposed by @muru.
However, this method is unsuitable if the contents of these directories are unrelated. It may be not obvious at first glance so let's see a new tree.
prompt% tree home/network
home/network/
├── data
│ ├── img
│ │ └── demo.png
│ └── src
│ ├── contact.pdf
│ └── report.pdf
├── hardware
│ ├── api
│ ├── docs
│ └── src
│ └── driver.c
├── misc
│ └── src
│ └── init.py
├── README
└── src
├── main.c
└── manager.c
We can move the contents of network/data
into network
and choose to merge directories having the same name (c.f. below).
prompt% cp -ab home/network/data/* home/network
prompt% rm -r home/network/data/*
prompt% tree home/network
home/network/
├── data
├── hardware
│ ├── api
│ ├── docs
│ └── src
│ └── driver.c
├── img
│ └── demo.png
├── misc
│ └── src
│ └── init.py
├── README
└── src
├── contact.pdf
├── main.c
├── manager.c
└── report.pdf
As we can see, the final directory network/src
may contain unrelated files after moving. If it is undesirable, we can rename files and directories having the same names after moving.
prompt% mv -b -S "_data" home/network/data/* home/network
prompt% tree home/network
home/network/
|-- README
|-- data
|-- hardware
| |-- api
| |-- docs
| `-- src
| `-- driver.c
|-- lib
| `-- demo.png
|-- misc
| `-- src
| `-- init.py
|-- src
| |-- contact.pdf
| `-- report.pdf
`-- src_data
|-- main.c
`-- manager.c
Related videos on Youtube
![Black](https://i.stack.imgur.com/QASBU.jpg?s=256&g=1)
Comments
-
Black almost 2 years
I have this folder structure:
foo `----> bar
How can I extract the content of
bar
intofoo
?I tried
mv -f bar/* .
from withinfoo
.-f, --force | dont't ask before overwrite
but I get "could not move
bar/ajax
tofoo/ajax
because the directory is not empty"How can I solve this?
-
frostschutz almost 6 years
mv
complains for reasons, in the end it depends what exactly you want to happen when there are folders and files to be overwritten... There are also some corner cases to take note of, e.g. what happens when there is abar/bar/
, or other conflicts. -
Black almost 6 years@ctrl-alt-delor, there are many other files and folders in foo
-
ctrl-alt-delor almost 6 yearsIt is best to edit the question, so that people see it (not just leave amendments in the comments).
-
-
Jeff Schaller about 6 yearsIn bash, consider setting dotglob so that this picks up “hidden” files.
-
Kusalananda about 6 yearsNote that this may overwrite files that have the same names in both folders, and that hidden files are skipped.
-
Black almost 6 yearsDoes not work, I get `Not possible because the directory is not empty"