Remove subdirectory from path and promote files to parent
up vote
0
down vote
favorite
I have a bunch of files which contain within their path, a duplicated folder (in the following examples bar). I would like to remove this directory and move any contents in contains into the parent. To use search and replace as a metaphor, I would like to replace /foo/ with / in the path.
Initial State:
foo/bar/some_file.txt
foo/another_file.txt
quux/bar/yet_another_file.txt
Goal:
foo/some_file.txt
foo/another_file.txt
quux/yet_another_file.txt
I have a preference for solving this with bash, but would be open to any solution that doesn't have any dependencies and will work on Linux.
In case this is helpful for anyone who stumbles upon this thread, there is an existing answer for Windows using robocopy.
bash
add a comment |
up vote
0
down vote
favorite
I have a bunch of files which contain within their path, a duplicated folder (in the following examples bar). I would like to remove this directory and move any contents in contains into the parent. To use search and replace as a metaphor, I would like to replace /foo/ with / in the path.
Initial State:
foo/bar/some_file.txt
foo/another_file.txt
quux/bar/yet_another_file.txt
Goal:
foo/some_file.txt
foo/another_file.txt
quux/yet_another_file.txt
I have a preference for solving this with bash, but would be open to any solution that doesn't have any dependencies and will work on Linux.
In case this is helpful for anyone who stumbles upon this thread, there is an existing answer for Windows using robocopy.
bash
This is pretty straightforward withbashParameter Expansion: what have you tried? We are not a script-writing service, but we will help when you get stuck.
– AFH
Dec 3 at 14:52
@AFH I tried renaming the files with krename, but was only able to get substitutions to work in the file name (in contrast to the path). I feel like xargs + sed could somehow be used to get where I want to go, but don't really know where to start. I'm happy to read if you'd point me in the right direction.
– Henry Marshall
Dec 3 at 15:08
Iffncontains the file name, then${fn%%/*}is the first element of the path (removing the first '/' and all beyond), and${fn##*/}is the file name (removing all up to the last '/'). So you simply need to create anmvtarget from these elements; you don't need external programs for the name processing.
– AFH
Dec 3 at 16:37
Will this work?mv foo/bar/*.* foo/*.*
– Biswapriyo
Dec 4 at 4:05
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a bunch of files which contain within their path, a duplicated folder (in the following examples bar). I would like to remove this directory and move any contents in contains into the parent. To use search and replace as a metaphor, I would like to replace /foo/ with / in the path.
Initial State:
foo/bar/some_file.txt
foo/another_file.txt
quux/bar/yet_another_file.txt
Goal:
foo/some_file.txt
foo/another_file.txt
quux/yet_another_file.txt
I have a preference for solving this with bash, but would be open to any solution that doesn't have any dependencies and will work on Linux.
In case this is helpful for anyone who stumbles upon this thread, there is an existing answer for Windows using robocopy.
bash
I have a bunch of files which contain within their path, a duplicated folder (in the following examples bar). I would like to remove this directory and move any contents in contains into the parent. To use search and replace as a metaphor, I would like to replace /foo/ with / in the path.
Initial State:
foo/bar/some_file.txt
foo/another_file.txt
quux/bar/yet_another_file.txt
Goal:
foo/some_file.txt
foo/another_file.txt
quux/yet_another_file.txt
I have a preference for solving this with bash, but would be open to any solution that doesn't have any dependencies and will work on Linux.
In case this is helpful for anyone who stumbles upon this thread, there is an existing answer for Windows using robocopy.
bash
bash
asked Dec 3 at 14:44
Henry Marshall
1414
1414
This is pretty straightforward withbashParameter Expansion: what have you tried? We are not a script-writing service, but we will help when you get stuck.
– AFH
Dec 3 at 14:52
@AFH I tried renaming the files with krename, but was only able to get substitutions to work in the file name (in contrast to the path). I feel like xargs + sed could somehow be used to get where I want to go, but don't really know where to start. I'm happy to read if you'd point me in the right direction.
– Henry Marshall
Dec 3 at 15:08
Iffncontains the file name, then${fn%%/*}is the first element of the path (removing the first '/' and all beyond), and${fn##*/}is the file name (removing all up to the last '/'). So you simply need to create anmvtarget from these elements; you don't need external programs for the name processing.
– AFH
Dec 3 at 16:37
Will this work?mv foo/bar/*.* foo/*.*
– Biswapriyo
Dec 4 at 4:05
add a comment |
This is pretty straightforward withbashParameter Expansion: what have you tried? We are not a script-writing service, but we will help when you get stuck.
– AFH
Dec 3 at 14:52
@AFH I tried renaming the files with krename, but was only able to get substitutions to work in the file name (in contrast to the path). I feel like xargs + sed could somehow be used to get where I want to go, but don't really know where to start. I'm happy to read if you'd point me in the right direction.
– Henry Marshall
Dec 3 at 15:08
Iffncontains the file name, then${fn%%/*}is the first element of the path (removing the first '/' and all beyond), and${fn##*/}is the file name (removing all up to the last '/'). So you simply need to create anmvtarget from these elements; you don't need external programs for the name processing.
– AFH
Dec 3 at 16:37
Will this work?mv foo/bar/*.* foo/*.*
– Biswapriyo
Dec 4 at 4:05
This is pretty straightforward with
bash Parameter Expansion: what have you tried? We are not a script-writing service, but we will help when you get stuck.– AFH
Dec 3 at 14:52
This is pretty straightforward with
bash Parameter Expansion: what have you tried? We are not a script-writing service, but we will help when you get stuck.– AFH
Dec 3 at 14:52
@AFH I tried renaming the files with krename, but was only able to get substitutions to work in the file name (in contrast to the path). I feel like xargs + sed could somehow be used to get where I want to go, but don't really know where to start. I'm happy to read if you'd point me in the right direction.
– Henry Marshall
Dec 3 at 15:08
@AFH I tried renaming the files with krename, but was only able to get substitutions to work in the file name (in contrast to the path). I feel like xargs + sed could somehow be used to get where I want to go, but don't really know where to start. I'm happy to read if you'd point me in the right direction.
– Henry Marshall
Dec 3 at 15:08
If
fn contains the file name, then ${fn%%/*} is the first element of the path (removing the first '/' and all beyond), and ${fn##*/} is the file name (removing all up to the last '/'). So you simply need to create an mv target from these elements; you don't need external programs for the name processing.– AFH
Dec 3 at 16:37
If
fn contains the file name, then ${fn%%/*} is the first element of the path (removing the first '/' and all beyond), and ${fn##*/} is the file name (removing all up to the last '/'). So you simply need to create an mv target from these elements; you don't need external programs for the name processing.– AFH
Dec 3 at 16:37
Will this work?
mv foo/bar/*.* foo/*.*– Biswapriyo
Dec 4 at 4:05
Will this work?
mv foo/bar/*.* foo/*.*– Biswapriyo
Dec 4 at 4:05
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
I wound up solving it like this:
for iteration_path in ${1}/*; do
if [[ ! -z $iteration_path ]]; then
mv "${iteration_path}"/bar/* "${iteration_path}";
rmdir "${iteration_path}"/bar;
fi
done
If anyone can expand on AFH's suggestions about parameter expansion, I'd be interested if that is a more elegant solution.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
I wound up solving it like this:
for iteration_path in ${1}/*; do
if [[ ! -z $iteration_path ]]; then
mv "${iteration_path}"/bar/* "${iteration_path}";
rmdir "${iteration_path}"/bar;
fi
done
If anyone can expand on AFH's suggestions about parameter expansion, I'd be interested if that is a more elegant solution.
add a comment |
up vote
0
down vote
I wound up solving it like this:
for iteration_path in ${1}/*; do
if [[ ! -z $iteration_path ]]; then
mv "${iteration_path}"/bar/* "${iteration_path}";
rmdir "${iteration_path}"/bar;
fi
done
If anyone can expand on AFH's suggestions about parameter expansion, I'd be interested if that is a more elegant solution.
add a comment |
up vote
0
down vote
up vote
0
down vote
I wound up solving it like this:
for iteration_path in ${1}/*; do
if [[ ! -z $iteration_path ]]; then
mv "${iteration_path}"/bar/* "${iteration_path}";
rmdir "${iteration_path}"/bar;
fi
done
If anyone can expand on AFH's suggestions about parameter expansion, I'd be interested if that is a more elegant solution.
I wound up solving it like this:
for iteration_path in ${1}/*; do
if [[ ! -z $iteration_path ]]; then
mv "${iteration_path}"/bar/* "${iteration_path}";
rmdir "${iteration_path}"/bar;
fi
done
If anyone can expand on AFH's suggestions about parameter expansion, I'd be interested if that is a more elegant solution.
answered Dec 4 at 2:51
Henry Marshall
1414
1414
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2fsuperuser.com%2fquestions%2f1380414%2fremove-subdirectory-from-path-and-promote-files-to-parent%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
This is pretty straightforward with
bashParameter Expansion: what have you tried? We are not a script-writing service, but we will help when you get stuck.– AFH
Dec 3 at 14:52
@AFH I tried renaming the files with krename, but was only able to get substitutions to work in the file name (in contrast to the path). I feel like xargs + sed could somehow be used to get where I want to go, but don't really know where to start. I'm happy to read if you'd point me in the right direction.
– Henry Marshall
Dec 3 at 15:08
If
fncontains the file name, then${fn%%/*}is the first element of the path (removing the first '/' and all beyond), and${fn##*/}is the file name (removing all up to the last '/'). So you simply need to create anmvtarget from these elements; you don't need external programs for the name processing.– AFH
Dec 3 at 16:37
Will this work?
mv foo/bar/*.* foo/*.*– Biswapriyo
Dec 4 at 4:05