Invert last two subfolders of the directory structure
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Basically, I request a unix way (bash, perl, etc) to invert the last two subfolders, like in this windows question:
https://superuser.com/questions/221/how-can-i-invert-a-directory-structure
This is an example, the objective is to modify the path of lots of files.
i.e.
/dir1/dir2/dir3/dir4/myfile
to:
/dir1/dir2/dir4/dir3/myfile
shell-script regular-expression directory perl
add a comment |Â
up vote
0
down vote
favorite
Basically, I request a unix way (bash, perl, etc) to invert the last two subfolders, like in this windows question:
https://superuser.com/questions/221/how-can-i-invert-a-directory-structure
This is an example, the objective is to modify the path of lots of files.
i.e.
/dir1/dir2/dir3/dir4/myfile
to:
/dir1/dir2/dir4/dir3/myfile
shell-script regular-expression directory perl
Can we assume that there is not already a dir1/dir2/dir4 directory? What is the input to this process â the path to myfile?
â Jeff Schaller
Jan 23 at 21:24
showtree /dir1/dir2
â RomanPerekhrest
Jan 23 at 21:33
1
You are unhappy withmv /dir1/dir2/dir3/dir4,dir3 ; mv /dir1/dir2/dir3,dir4
?
â Ralph Rönnquist
Jan 23 at 21:35
Jeff Shaller, I am not sure what I can answer, but it would be better to just check it in the process. and create it only when necessary. I think of several files in the example subfolder dir4. I am not sure if the input should be every file or if it would be the subfolder.
â Ferroao
Jan 23 at 21:43
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Basically, I request a unix way (bash, perl, etc) to invert the last two subfolders, like in this windows question:
https://superuser.com/questions/221/how-can-i-invert-a-directory-structure
This is an example, the objective is to modify the path of lots of files.
i.e.
/dir1/dir2/dir3/dir4/myfile
to:
/dir1/dir2/dir4/dir3/myfile
shell-script regular-expression directory perl
Basically, I request a unix way (bash, perl, etc) to invert the last two subfolders, like in this windows question:
https://superuser.com/questions/221/how-can-i-invert-a-directory-structure
This is an example, the objective is to modify the path of lots of files.
i.e.
/dir1/dir2/dir3/dir4/myfile
to:
/dir1/dir2/dir4/dir3/myfile
shell-script regular-expression directory perl
edited Jan 23 at 21:41
asked Jan 23 at 21:18
Ferroao
1256
1256
Can we assume that there is not already a dir1/dir2/dir4 directory? What is the input to this process â the path to myfile?
â Jeff Schaller
Jan 23 at 21:24
showtree /dir1/dir2
â RomanPerekhrest
Jan 23 at 21:33
1
You are unhappy withmv /dir1/dir2/dir3/dir4,dir3 ; mv /dir1/dir2/dir3,dir4
?
â Ralph Rönnquist
Jan 23 at 21:35
Jeff Shaller, I am not sure what I can answer, but it would be better to just check it in the process. and create it only when necessary. I think of several files in the example subfolder dir4. I am not sure if the input should be every file or if it would be the subfolder.
â Ferroao
Jan 23 at 21:43
add a comment |Â
Can we assume that there is not already a dir1/dir2/dir4 directory? What is the input to this process â the path to myfile?
â Jeff Schaller
Jan 23 at 21:24
showtree /dir1/dir2
â RomanPerekhrest
Jan 23 at 21:33
1
You are unhappy withmv /dir1/dir2/dir3/dir4,dir3 ; mv /dir1/dir2/dir3,dir4
?
â Ralph Rönnquist
Jan 23 at 21:35
Jeff Shaller, I am not sure what I can answer, but it would be better to just check it in the process. and create it only when necessary. I think of several files in the example subfolder dir4. I am not sure if the input should be every file or if it would be the subfolder.
â Ferroao
Jan 23 at 21:43
Can we assume that there is not already a dir1/dir2/dir4 directory? What is the input to this process â the path to myfile?
â Jeff Schaller
Jan 23 at 21:24
Can we assume that there is not already a dir1/dir2/dir4 directory? What is the input to this process â the path to myfile?
â Jeff Schaller
Jan 23 at 21:24
show
tree /dir1/dir2
â RomanPerekhrest
Jan 23 at 21:33
show
tree /dir1/dir2
â RomanPerekhrest
Jan 23 at 21:33
1
1
You are unhappy with
mv /dir1/dir2/dir3/dir4,dir3 ; mv /dir1/dir2/dir3,dir4
?â Ralph Rönnquist
Jan 23 at 21:35
You are unhappy with
mv /dir1/dir2/dir3/dir4,dir3 ; mv /dir1/dir2/dir3,dir4
?â Ralph Rönnquist
Jan 23 at 21:35
Jeff Shaller, I am not sure what I can answer, but it would be better to just check it in the process. and create it only when necessary. I think of several files in the example subfolder dir4. I am not sure if the input should be every file or if it would be the subfolder.
â Ferroao
Jan 23 at 21:43
Jeff Shaller, I am not sure what I can answer, but it would be better to just check it in the process. and create it only when necessary. I think of several files in the example subfolder dir4. I am not sure if the input should be every file or if it would be the subfolder.
â Ferroao
Jan 23 at 21:43
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
For a solution with four subdirectories you could use something like this:
for p4 in */*/*/*
do
p3="$p4%/*" p2="$p3%/*" d4="$p4/*/" d3="$p3/*/"
mv "$p4" "$p3/$d3" && mv "$p3" "$p2/$d4"
done
It does no error checking for the presence of a target directory before it attempts the transposition. In this situation you will end up with the third level directory being duplicated. For example, a/b/d
exists and we are going to transpose a/b/c/d
, we will end up with a/b/d/c/c
.
If you have too many directories matching */*/*/*
you could split it further with an additional loop per level. With care this will obviate the need to derive paths $p2
and $p3
.
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
For a solution with four subdirectories you could use something like this:
for p4 in */*/*/*
do
p3="$p4%/*" p2="$p3%/*" d4="$p4/*/" d3="$p3/*/"
mv "$p4" "$p3/$d3" && mv "$p3" "$p2/$d4"
done
It does no error checking for the presence of a target directory before it attempts the transposition. In this situation you will end up with the third level directory being duplicated. For example, a/b/d
exists and we are going to transpose a/b/c/d
, we will end up with a/b/d/c/c
.
If you have too many directories matching */*/*/*
you could split it further with an additional loop per level. With care this will obviate the need to derive paths $p2
and $p3
.
add a comment |Â
up vote
0
down vote
For a solution with four subdirectories you could use something like this:
for p4 in */*/*/*
do
p3="$p4%/*" p2="$p3%/*" d4="$p4/*/" d3="$p3/*/"
mv "$p4" "$p3/$d3" && mv "$p3" "$p2/$d4"
done
It does no error checking for the presence of a target directory before it attempts the transposition. In this situation you will end up with the third level directory being duplicated. For example, a/b/d
exists and we are going to transpose a/b/c/d
, we will end up with a/b/d/c/c
.
If you have too many directories matching */*/*/*
you could split it further with an additional loop per level. With care this will obviate the need to derive paths $p2
and $p3
.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
For a solution with four subdirectories you could use something like this:
for p4 in */*/*/*
do
p3="$p4%/*" p2="$p3%/*" d4="$p4/*/" d3="$p3/*/"
mv "$p4" "$p3/$d3" && mv "$p3" "$p2/$d4"
done
It does no error checking for the presence of a target directory before it attempts the transposition. In this situation you will end up with the third level directory being duplicated. For example, a/b/d
exists and we are going to transpose a/b/c/d
, we will end up with a/b/d/c/c
.
If you have too many directories matching */*/*/*
you could split it further with an additional loop per level. With care this will obviate the need to derive paths $p2
and $p3
.
For a solution with four subdirectories you could use something like this:
for p4 in */*/*/*
do
p3="$p4%/*" p2="$p3%/*" d4="$p4/*/" d3="$p3/*/"
mv "$p4" "$p3/$d3" && mv "$p3" "$p2/$d4"
done
It does no error checking for the presence of a target directory before it attempts the transposition. In this situation you will end up with the third level directory being duplicated. For example, a/b/d
exists and we are going to transpose a/b/c/d
, we will end up with a/b/d/c/c
.
If you have too many directories matching */*/*/*
you could split it further with an additional loop per level. With care this will obviate the need to derive paths $p2
and $p3
.
answered Jan 24 at 0:19
roaima
39.7k545108
39.7k545108
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f419187%2finvert-last-two-subfolders-of-the-directory-structure%23new-answer', 'question_page');
);
Post as a guest
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
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
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
Can we assume that there is not already a dir1/dir2/dir4 directory? What is the input to this process â the path to myfile?
â Jeff Schaller
Jan 23 at 21:24
show
tree /dir1/dir2
â RomanPerekhrest
Jan 23 at 21:33
1
You are unhappy with
mv /dir1/dir2/dir3/dir4,dir3 ; mv /dir1/dir2/dir3,dir4
?â Ralph Rönnquist
Jan 23 at 21:35
Jeff Shaller, I am not sure what I can answer, but it would be better to just check it in the process. and create it only when necessary. I think of several files in the example subfolder dir4. I am not sure if the input should be every file or if it would be the subfolder.
â Ferroao
Jan 23 at 21:43