Use find to find a directory and move it to a different path












0















I have hundreds of thousands of files in hundreds of directories.



An example directory structure is



./main/foo1/bar/*
./main/foo2/bar/*
./main/foo3/bar/*
./main/foo1/ran/*
./main/foo2/ran/*


For folders that have 'bar' directories, I want to move contents to following structure.



./secondary/bar/foo1/*
./secondary/bar/foo2/*
./secondary/bar/foo3/*


Can this be accomplished using find and mv?










share|improve this question









New contributor




Umer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    are the foo and bar directorys always in the same depth?

    – RoVo
    Jan 8 at 14:12











  • Yes, the depth is consistent.

    – Umer
    Jan 8 at 14:18


















0















I have hundreds of thousands of files in hundreds of directories.



An example directory structure is



./main/foo1/bar/*
./main/foo2/bar/*
./main/foo3/bar/*
./main/foo1/ran/*
./main/foo2/ran/*


For folders that have 'bar' directories, I want to move contents to following structure.



./secondary/bar/foo1/*
./secondary/bar/foo2/*
./secondary/bar/foo3/*


Can this be accomplished using find and mv?










share|improve this question









New contributor




Umer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2





    are the foo and bar directorys always in the same depth?

    – RoVo
    Jan 8 at 14:12











  • Yes, the depth is consistent.

    – Umer
    Jan 8 at 14:18
















0












0








0








I have hundreds of thousands of files in hundreds of directories.



An example directory structure is



./main/foo1/bar/*
./main/foo2/bar/*
./main/foo3/bar/*
./main/foo1/ran/*
./main/foo2/ran/*


For folders that have 'bar' directories, I want to move contents to following structure.



./secondary/bar/foo1/*
./secondary/bar/foo2/*
./secondary/bar/foo3/*


Can this be accomplished using find and mv?










share|improve this question









New contributor




Umer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I have hundreds of thousands of files in hundreds of directories.



An example directory structure is



./main/foo1/bar/*
./main/foo2/bar/*
./main/foo3/bar/*
./main/foo1/ran/*
./main/foo2/ran/*


For folders that have 'bar' directories, I want to move contents to following structure.



./secondary/bar/foo1/*
./secondary/bar/foo2/*
./secondary/bar/foo3/*


Can this be accomplished using find and mv?







command-line bash directory find mv






share|improve this question









New contributor




Umer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Umer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago







Umer













New contributor




Umer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Jan 8 at 14:07









UmerUmer

33




33




New contributor




Umer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Umer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Umer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2





    are the foo and bar directorys always in the same depth?

    – RoVo
    Jan 8 at 14:12











  • Yes, the depth is consistent.

    – Umer
    Jan 8 at 14:18
















  • 2





    are the foo and bar directorys always in the same depth?

    – RoVo
    Jan 8 at 14:12











  • Yes, the depth is consistent.

    – Umer
    Jan 8 at 14:18










2




2





are the foo and bar directorys always in the same depth?

– RoVo
Jan 8 at 14:12





are the foo and bar directorys always in the same depth?

– RoVo
Jan 8 at 14:12













Yes, the depth is consistent.

– Umer
Jan 8 at 14:18







Yes, the depth is consistent.

– Umer
Jan 8 at 14:18












1 Answer
1






active

oldest

votes


















2














Some little script to achieve this:



# Set the variables
main="./main"
secondary="./secondary"
foo_depth=3
bar_depth=4
bar_name="bar"

# let * match hidden files
shopt -s dotglob

# Loop through find (which is declared after done)
while IFS= read -r dir; do

# Read names of foo dir
foo=$(printf '%s' "$dir" | cut -d'/' -f $foo_depth)

# mkdir target and mv dir there
target="${secondary}/${bar_name}/${foo}"
mkdir -p "$target"
mv "$dir"/* "$target"

# as you mv the content of bar dir only,
# you may want to remove the full path to that folder
# rmdir -p will do that without deleting anything that is not empty,
# we can ignore the "failed to remove" messages.
[ $? = 0 ] && rmdir -p "${dir}"

done < <(find "$main" -type d -name "$bar_name")

# Turn off dotglob
shopt -u dotglob





share|improve this answer


























  • Thanks but my apologies since I messed up a bit. The 'sub' directory is not alone there and is a mix of files and folders that I intend to move. I've edited the question. Can you please edit your script? Really appreciated.

    – Umer
    2 days ago











  • I made an update.

    – RoVo
    2 days ago











  • Thanks a lot. I'll try it out soon.

    – Umer
    2 days ago











  • Ah this works beautifully. Thanks a ton. :)

    – Umer
    2 days ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});


}
});






Umer is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1108000%2fuse-find-to-find-a-directory-and-move-it-to-a-different-path%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Some little script to achieve this:



# Set the variables
main="./main"
secondary="./secondary"
foo_depth=3
bar_depth=4
bar_name="bar"

# let * match hidden files
shopt -s dotglob

# Loop through find (which is declared after done)
while IFS= read -r dir; do

# Read names of foo dir
foo=$(printf '%s' "$dir" | cut -d'/' -f $foo_depth)

# mkdir target and mv dir there
target="${secondary}/${bar_name}/${foo}"
mkdir -p "$target"
mv "$dir"/* "$target"

# as you mv the content of bar dir only,
# you may want to remove the full path to that folder
# rmdir -p will do that without deleting anything that is not empty,
# we can ignore the "failed to remove" messages.
[ $? = 0 ] && rmdir -p "${dir}"

done < <(find "$main" -type d -name "$bar_name")

# Turn off dotglob
shopt -u dotglob





share|improve this answer


























  • Thanks but my apologies since I messed up a bit. The 'sub' directory is not alone there and is a mix of files and folders that I intend to move. I've edited the question. Can you please edit your script? Really appreciated.

    – Umer
    2 days ago











  • I made an update.

    – RoVo
    2 days ago











  • Thanks a lot. I'll try it out soon.

    – Umer
    2 days ago











  • Ah this works beautifully. Thanks a ton. :)

    – Umer
    2 days ago
















2














Some little script to achieve this:



# Set the variables
main="./main"
secondary="./secondary"
foo_depth=3
bar_depth=4
bar_name="bar"

# let * match hidden files
shopt -s dotglob

# Loop through find (which is declared after done)
while IFS= read -r dir; do

# Read names of foo dir
foo=$(printf '%s' "$dir" | cut -d'/' -f $foo_depth)

# mkdir target and mv dir there
target="${secondary}/${bar_name}/${foo}"
mkdir -p "$target"
mv "$dir"/* "$target"

# as you mv the content of bar dir only,
# you may want to remove the full path to that folder
# rmdir -p will do that without deleting anything that is not empty,
# we can ignore the "failed to remove" messages.
[ $? = 0 ] && rmdir -p "${dir}"

done < <(find "$main" -type d -name "$bar_name")

# Turn off dotglob
shopt -u dotglob





share|improve this answer


























  • Thanks but my apologies since I messed up a bit. The 'sub' directory is not alone there and is a mix of files and folders that I intend to move. I've edited the question. Can you please edit your script? Really appreciated.

    – Umer
    2 days ago











  • I made an update.

    – RoVo
    2 days ago











  • Thanks a lot. I'll try it out soon.

    – Umer
    2 days ago











  • Ah this works beautifully. Thanks a ton. :)

    – Umer
    2 days ago














2












2








2







Some little script to achieve this:



# Set the variables
main="./main"
secondary="./secondary"
foo_depth=3
bar_depth=4
bar_name="bar"

# let * match hidden files
shopt -s dotglob

# Loop through find (which is declared after done)
while IFS= read -r dir; do

# Read names of foo dir
foo=$(printf '%s' "$dir" | cut -d'/' -f $foo_depth)

# mkdir target and mv dir there
target="${secondary}/${bar_name}/${foo}"
mkdir -p "$target"
mv "$dir"/* "$target"

# as you mv the content of bar dir only,
# you may want to remove the full path to that folder
# rmdir -p will do that without deleting anything that is not empty,
# we can ignore the "failed to remove" messages.
[ $? = 0 ] && rmdir -p "${dir}"

done < <(find "$main" -type d -name "$bar_name")

# Turn off dotglob
shopt -u dotglob





share|improve this answer















Some little script to achieve this:



# Set the variables
main="./main"
secondary="./secondary"
foo_depth=3
bar_depth=4
bar_name="bar"

# let * match hidden files
shopt -s dotglob

# Loop through find (which is declared after done)
while IFS= read -r dir; do

# Read names of foo dir
foo=$(printf '%s' "$dir" | cut -d'/' -f $foo_depth)

# mkdir target and mv dir there
target="${secondary}/${bar_name}/${foo}"
mkdir -p "$target"
mv "$dir"/* "$target"

# as you mv the content of bar dir only,
# you may want to remove the full path to that folder
# rmdir -p will do that without deleting anything that is not empty,
# we can ignore the "failed to remove" messages.
[ $? = 0 ] && rmdir -p "${dir}"

done < <(find "$main" -type d -name "$bar_name")

# Turn off dotglob
shopt -u dotglob






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered Jan 8 at 14:35









RoVoRoVo

6,9781741




6,9781741













  • Thanks but my apologies since I messed up a bit. The 'sub' directory is not alone there and is a mix of files and folders that I intend to move. I've edited the question. Can you please edit your script? Really appreciated.

    – Umer
    2 days ago











  • I made an update.

    – RoVo
    2 days ago











  • Thanks a lot. I'll try it out soon.

    – Umer
    2 days ago











  • Ah this works beautifully. Thanks a ton. :)

    – Umer
    2 days ago



















  • Thanks but my apologies since I messed up a bit. The 'sub' directory is not alone there and is a mix of files and folders that I intend to move. I've edited the question. Can you please edit your script? Really appreciated.

    – Umer
    2 days ago











  • I made an update.

    – RoVo
    2 days ago











  • Thanks a lot. I'll try it out soon.

    – Umer
    2 days ago











  • Ah this works beautifully. Thanks a ton. :)

    – Umer
    2 days ago

















Thanks but my apologies since I messed up a bit. The 'sub' directory is not alone there and is a mix of files and folders that I intend to move. I've edited the question. Can you please edit your script? Really appreciated.

– Umer
2 days ago





Thanks but my apologies since I messed up a bit. The 'sub' directory is not alone there and is a mix of files and folders that I intend to move. I've edited the question. Can you please edit your script? Really appreciated.

– Umer
2 days ago













I made an update.

– RoVo
2 days ago





I made an update.

– RoVo
2 days ago













Thanks a lot. I'll try it out soon.

– Umer
2 days ago





Thanks a lot. I'll try it out soon.

– Umer
2 days ago













Ah this works beautifully. Thanks a ton. :)

– Umer
2 days ago





Ah this works beautifully. Thanks a ton. :)

– Umer
2 days ago










Umer is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Umer is a new contributor. Be nice, and check out our Code of Conduct.













Umer is a new contributor. Be nice, and check out our Code of Conduct.












Umer is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Ask Ubuntu!


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1108000%2fuse-find-to-find-a-directory-and-move-it-to-a-different-path%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

數位音樂下載

When can things happen in Etherscan, such as the picture below?

格利澤436b