How to move content of a folder to current folder? The 2019 Stack Overflow Developer Survey Results Are InHow do you move all files (including hidden) from one directory to another?Copy directory, but fail if file already exists at destinationRecursive move (`mv -rn`, like `cp -rn`), a move that will only move not present filesMove Files From Subfolders to Parent Folder Within Larger DirectoryHow can I move a file within a directory to the current working directory without renaming?How to extract a specifically named folder from a recursive directory, delete the others?Copying a folder using xargsMove Directory Contents Up A Level And OVERWRITE Target FilesMove folder structure across multiple directoriesMoving contents of current directory up one level and overwriting if exists recursively
Is an up-to-date browser secure on an out-of-date OS?
Building a conditional check constraint
Is bread bad for ducks?
Protecting Dualbooting Windows from dangerous code (like rm -rf)
Resizing object distorts it (Illustrator CC 2018)
Can you compress metal and what would be the consequences?
Who coined the term "madman theory"?
Landlord wants to switch my lease to a "Land contract" to "get back at the city"
Origin of "cooter" meaning "vagina"
Why isn't the circumferential light around the M87 black hole's event horizon symmetric?
Right tool to dig six foot holes?
If I score a critical hit on an 18 or higher, what are my chances of getting a critical hit if I roll 3d20?
Why is the maximum length of OpenWrt’s root password 8 characters?
Why did Acorn's A3000 have red function keys?
How to manage monthly salary
Button changing it's text & action. Good or terrible?
Should I use my personal e-mail address, or my workplace one, when registering to external websites for work purposes?
Return to UK after being refused entry years previously
STM32 programming and BOOT0 pin
Is there a symbol for a right arrow with a square in the middle?
How to save as into a customized destination on macOS?
Why can Shazam fly?
How to deal with fear of taking dependencies
Which Sci-Fi work first showed weapon of galactic-scale mass destruction?
How to move content of a folder to current folder?
The 2019 Stack Overflow Developer Survey Results Are InHow do you move all files (including hidden) from one directory to another?Copy directory, but fail if file already exists at destinationRecursive move (`mv -rn`, like `cp -rn`), a move that will only move not present filesMove Files From Subfolders to Parent Folder Within Larger DirectoryHow can I move a file within a directory to the current working directory without renaming?How to extract a specifically named folder from a recursive directory, delete the others?Copying a folder using xargsMove Directory Contents Up A Level And OVERWRITE Target FilesMove folder structure across multiple directoriesMoving contents of current directory up one level and overwriting if exists recursively
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have this folder structure:
foo
`----> bar
How can I extract the content of bar into foo?
I tried mv -f bar/* . from within foo.
-f, --force | dont't ask before overwrite
but I get "could not move bar/ajax to foo/ajax because the directory is not empty"
How can I solve this?
cp mv
add a comment |
I have this folder structure:
foo
`----> bar
How can I extract the content of bar into foo?
I tried mv -f bar/* . from within foo.
-f, --force | dont't ask before overwrite
but I get "could not move bar/ajax to foo/ajax because the directory is not empty"
How can I solve this?
cp mv
mvcomplains 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.
– frostschutz
Jul 5 '18 at 11:02
Is there anything else in foo? (is it just bar?)
– ctrl-alt-delor
Jul 5 '18 at 18:11
@ctrl-alt-delor, there are many other files and folders in foo
– Black
Jul 6 '18 at 8:04
It is best to edit the question, so that people see it (not just leave amendments in the comments).
– ctrl-alt-delor
Jul 6 '18 at 8:04
add a comment |
I have this folder structure:
foo
`----> bar
How can I extract the content of bar into foo?
I tried mv -f bar/* . from within foo.
-f, --force | dont't ask before overwrite
but I get "could not move bar/ajax to foo/ajax because the directory is not empty"
How can I solve this?
cp mv
I have this folder structure:
foo
`----> bar
How can I extract the content of bar into foo?
I tried mv -f bar/* . from within foo.
-f, --force | dont't ask before overwrite
but I get "could not move bar/ajax to foo/ajax because the directory is not empty"
How can I solve this?
cp mv
cp mv
edited Jul 5 '18 at 10:33
Black
asked Jun 21 '18 at 11:59
BlackBlack
57421030
57421030
mvcomplains 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.
– frostschutz
Jul 5 '18 at 11:02
Is there anything else in foo? (is it just bar?)
– ctrl-alt-delor
Jul 5 '18 at 18:11
@ctrl-alt-delor, there are many other files and folders in foo
– Black
Jul 6 '18 at 8:04
It is best to edit the question, so that people see it (not just leave amendments in the comments).
– ctrl-alt-delor
Jul 6 '18 at 8:04
add a comment |
mvcomplains 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.
– frostschutz
Jul 5 '18 at 11:02
Is there anything else in foo? (is it just bar?)
– ctrl-alt-delor
Jul 5 '18 at 18:11
@ctrl-alt-delor, there are many other files and folders in foo
– Black
Jul 6 '18 at 8:04
It is best to edit the question, so that people see it (not just leave amendments in the comments).
– ctrl-alt-delor
Jul 6 '18 at 8:04
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 a bar/bar/, or other conflicts.– frostschutz
Jul 5 '18 at 11:02
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 a bar/bar/, or other conflicts.– frostschutz
Jul 5 '18 at 11:02
Is there anything else in foo? (is it just bar?)
– ctrl-alt-delor
Jul 5 '18 at 18:11
Is there anything else in foo? (is it just bar?)
– ctrl-alt-delor
Jul 5 '18 at 18:11
@ctrl-alt-delor, there are many other files and folders in foo
– Black
Jul 6 '18 at 8:04
@ctrl-alt-delor, there are many other files and folders in foo
– Black
Jul 6 '18 at 8:04
It is best to edit the question, so that people see it (not just leave amendments in the comments).
– ctrl-alt-delor
Jul 6 '18 at 8:04
It is best to edit the question, so that people see it (not just leave amendments in the comments).
– ctrl-alt-delor
Jul 6 '18 at 8:04
add a comment |
5 Answers
5
active
oldest
votes
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.
add a comment |
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!
add a comment |
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:
mvwill 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
-for--forceoption is not given,mvprompts 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
add a comment |
If you have a directory foo that only contains a directory bar. (bar may contain other stuff).
And you want to rename foo/bar → foo, then do:
mv -T foo foo.original
mv -T foo.original/bar foo
rmdir foo.original
You can leave of the -T if your mv does not have this option, however it makes it safer.
add a comment |
Simply use the move command :
cd foo
mv bar/* .
As Jeff Schaller suggest, use
shopt -s dotglob
before the mv command to move hidden files and use the option -i of mv if you want an interactive check before overwriting
In bash, consider setting dotglob so that this picks up “hidden” files.
– Jeff Schaller♦
Jun 21 '18 at 12:05
Note that this may overwrite files that have the same names in both folders, and that hidden files are skipped.
– Kusalananda♦
Jun 21 '18 at 12:06
1
Does not work, I get `Not possible because the directory is not empty"
– Black
Jul 5 '18 at 10:30
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451081%2fhow-to-move-content-of-a-folder-to-current-folder%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
add a comment |
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.
add a comment |
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.
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.
answered Jul 5 '18 at 10:58
murumuru
37.4k589164
37.4k589164
add a comment |
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
edited Jul 21 '18 at 18:40
answered Jul 5 '18 at 11:08
kemotepkemotep
2,6983823
2,6983823
add a comment |
add a comment |
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:
mvwill 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
-for--forceoption is not given,mvprompts 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
add a comment |
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:
mvwill 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
-for--forceoption is not given,mvprompts 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
add a comment |
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:
mvwill 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
-for--forceoption is not given,mvprompts 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
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:
mvwill 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
-for--forceoption is not given,mvprompts 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
edited yesterday
answered Jul 5 '18 at 17:25
FólkvangrFólkvangr
34014
34014
add a comment |
add a comment |
If you have a directory foo that only contains a directory bar. (bar may contain other stuff).
And you want to rename foo/bar → foo, then do:
mv -T foo foo.original
mv -T foo.original/bar foo
rmdir foo.original
You can leave of the -T if your mv does not have this option, however it makes it safer.
add a comment |
If you have a directory foo that only contains a directory bar. (bar may contain other stuff).
And you want to rename foo/bar → foo, then do:
mv -T foo foo.original
mv -T foo.original/bar foo
rmdir foo.original
You can leave of the -T if your mv does not have this option, however it makes it safer.
add a comment |
If you have a directory foo that only contains a directory bar. (bar may contain other stuff).
And you want to rename foo/bar → foo, then do:
mv -T foo foo.original
mv -T foo.original/bar foo
rmdir foo.original
You can leave of the -T if your mv does not have this option, however it makes it safer.
If you have a directory foo that only contains a directory bar. (bar may contain other stuff).
And you want to rename foo/bar → foo, then do:
mv -T foo foo.original
mv -T foo.original/bar foo
rmdir foo.original
You can leave of the -T if your mv does not have this option, however it makes it safer.
edited Jul 6 '18 at 8:05
answered Jul 5 '18 at 18:15
ctrl-alt-delorctrl-alt-delor
12.4k52662
12.4k52662
add a comment |
add a comment |
Simply use the move command :
cd foo
mv bar/* .
As Jeff Schaller suggest, use
shopt -s dotglob
before the mv command to move hidden files and use the option -i of mv if you want an interactive check before overwriting
In bash, consider setting dotglob so that this picks up “hidden” files.
– Jeff Schaller♦
Jun 21 '18 at 12:05
Note that this may overwrite files that have the same names in both folders, and that hidden files are skipped.
– Kusalananda♦
Jun 21 '18 at 12:06
1
Does not work, I get `Not possible because the directory is not empty"
– Black
Jul 5 '18 at 10:30
add a comment |
Simply use the move command :
cd foo
mv bar/* .
As Jeff Schaller suggest, use
shopt -s dotglob
before the mv command to move hidden files and use the option -i of mv if you want an interactive check before overwriting
In bash, consider setting dotglob so that this picks up “hidden” files.
– Jeff Schaller♦
Jun 21 '18 at 12:05
Note that this may overwrite files that have the same names in both folders, and that hidden files are skipped.
– Kusalananda♦
Jun 21 '18 at 12:06
1
Does not work, I get `Not possible because the directory is not empty"
– Black
Jul 5 '18 at 10:30
add a comment |
Simply use the move command :
cd foo
mv bar/* .
As Jeff Schaller suggest, use
shopt -s dotglob
before the mv command to move hidden files and use the option -i of mv if you want an interactive check before overwriting
Simply use the move command :
cd foo
mv bar/* .
As Jeff Schaller suggest, use
shopt -s dotglob
before the mv command to move hidden files and use the option -i of mv if you want an interactive check before overwriting
edited Jun 21 '18 at 12:14
Kusalananda♦
141k17262438
141k17262438
answered Jun 21 '18 at 12:03
Jean-Paul SabatierJean-Paul Sabatier
324
324
In bash, consider setting dotglob so that this picks up “hidden” files.
– Jeff Schaller♦
Jun 21 '18 at 12:05
Note that this may overwrite files that have the same names in both folders, and that hidden files are skipped.
– Kusalananda♦
Jun 21 '18 at 12:06
1
Does not work, I get `Not possible because the directory is not empty"
– Black
Jul 5 '18 at 10:30
add a comment |
In bash, consider setting dotglob so that this picks up “hidden” files.
– Jeff Schaller♦
Jun 21 '18 at 12:05
Note that this may overwrite files that have the same names in both folders, and that hidden files are skipped.
– Kusalananda♦
Jun 21 '18 at 12:06
1
Does not work, I get `Not possible because the directory is not empty"
– Black
Jul 5 '18 at 10:30
In bash, consider setting dotglob so that this picks up “hidden” files.
– Jeff Schaller♦
Jun 21 '18 at 12:05
In bash, consider setting dotglob so that this picks up “hidden” files.
– Jeff Schaller♦
Jun 21 '18 at 12:05
Note that this may overwrite files that have the same names in both folders, and that hidden files are skipped.
– Kusalananda♦
Jun 21 '18 at 12:06
Note that this may overwrite files that have the same names in both folders, and that hidden files are skipped.
– Kusalananda♦
Jun 21 '18 at 12:06
1
1
Does not work, I get `Not possible because the directory is not empty"
– Black
Jul 5 '18 at 10:30
Does not work, I get `Not possible because the directory is not empty"
– Black
Jul 5 '18 at 10:30
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f451081%2fhow-to-move-content-of-a-folder-to-current-folder%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
mvcomplains 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.– frostschutz
Jul 5 '18 at 11:02
Is there anything else in foo? (is it just bar?)
– ctrl-alt-delor
Jul 5 '18 at 18:11
@ctrl-alt-delor, there are many other files and folders in foo
– Black
Jul 6 '18 at 8:04
It is best to edit the question, so that people see it (not just leave amendments in the comments).
– ctrl-alt-delor
Jul 6 '18 at 8:04