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




















  • 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




















  • 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
















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















share|improve this question













share|improve this question




share|improve this question








edited Jan 9 at 7:36







Umer

















asked Jan 8 at 14:07









UmerUmer

33




33








  • 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
    Jan 9 at 7:39











  • I made an update.

    – RoVo
    Jan 9 at 8:43











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

    – Umer
    Jan 9 at 8:56











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

    – Umer
    Jan 9 at 9:08











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
});


}
});














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
    Jan 9 at 7:39











  • I made an update.

    – RoVo
    Jan 9 at 8:43











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

    – Umer
    Jan 9 at 8:56











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

    – Umer
    Jan 9 at 9:08
















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
    Jan 9 at 7:39











  • I made an update.

    – RoVo
    Jan 9 at 8:43











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

    – Umer
    Jan 9 at 8:56











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

    – Umer
    Jan 9 at 9:08














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 Jan 9 at 8:56

























answered Jan 8 at 14:35









RoVoRoVo

7,2211841




7,2211841













  • 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
    Jan 9 at 7:39











  • I made an update.

    – RoVo
    Jan 9 at 8:43











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

    – Umer
    Jan 9 at 8:56











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

    – Umer
    Jan 9 at 9:08



















  • 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
    Jan 9 at 7:39











  • I made an update.

    – RoVo
    Jan 9 at 8:43











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

    – Umer
    Jan 9 at 8:56











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

    – Umer
    Jan 9 at 9:08

















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
Jan 9 at 7:39





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
Jan 9 at 7:39













I made an update.

– RoVo
Jan 9 at 8:43





I made an update.

– RoVo
Jan 9 at 8:43













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

– Umer
Jan 9 at 8:56





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

– Umer
Jan 9 at 8:56













Ah this works beautifully. Thanks a ton. :)

– Umer
Jan 9 at 9:08





Ah this works beautifully. Thanks a ton. :)

– Umer
Jan 9 at 9:08


















draft saved

draft discarded




















































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

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?