How to move files under multi-directories into same directory?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
1
down vote

favorite
2












i have so many files under 300 different directories, file tree like:



--s1
- ----abc
- ----bcd
--s2
- ----123
- ----234


... etc.



I want to put them together under the same directory, like:



--whole
- ----abc
- ----bcd
- ----123
- ----234 ...


Is there any useful bash script in practical?



I coded this piece of bash script:



mkdir wavs

for ((i=1;i<=9;i++)); do
cd ~/wav/train/S000$i
mv * ~/wav/train/wavs
cd .. done

for ((i=10;i<=99;i++)); do
cd ~/wav/train/S00$i
mv * ~/wav/train/wavs
cd .. done

for ((i=100;i<=917;i++)); do
cd ~/wav/train/S0$i
mv * ~/wav/train/wavs
cd .. done

echo "ok"


but i got the error which i dont understand:



./untar.sh: line 24: cd: /wav/train/S0917: No such file or directory 
cp: target '/wav/train/wavs/' is not a directory ok









share|improve this question























  • Hello, David, I've wrote an answer which uses find, that IMO is the easiest way to do this job. If you need to create a bash script here is provided two variants that solve a similar task: askubuntu.com/a/1020671/566421
    – pa4080
    Apr 2 at 9:57














up vote
1
down vote

favorite
2












i have so many files under 300 different directories, file tree like:



--s1
- ----abc
- ----bcd
--s2
- ----123
- ----234


... etc.



I want to put them together under the same directory, like:



--whole
- ----abc
- ----bcd
- ----123
- ----234 ...


Is there any useful bash script in practical?



I coded this piece of bash script:



mkdir wavs

for ((i=1;i<=9;i++)); do
cd ~/wav/train/S000$i
mv * ~/wav/train/wavs
cd .. done

for ((i=10;i<=99;i++)); do
cd ~/wav/train/S00$i
mv * ~/wav/train/wavs
cd .. done

for ((i=100;i<=917;i++)); do
cd ~/wav/train/S0$i
mv * ~/wav/train/wavs
cd .. done

echo "ok"


but i got the error which i dont understand:



./untar.sh: line 24: cd: /wav/train/S0917: No such file or directory 
cp: target '/wav/train/wavs/' is not a directory ok









share|improve this question























  • Hello, David, I've wrote an answer which uses find, that IMO is the easiest way to do this job. If you need to create a bash script here is provided two variants that solve a similar task: askubuntu.com/a/1020671/566421
    – pa4080
    Apr 2 at 9:57












up vote
1
down vote

favorite
2









up vote
1
down vote

favorite
2






2





i have so many files under 300 different directories, file tree like:



--s1
- ----abc
- ----bcd
--s2
- ----123
- ----234


... etc.



I want to put them together under the same directory, like:



--whole
- ----abc
- ----bcd
- ----123
- ----234 ...


Is there any useful bash script in practical?



I coded this piece of bash script:



mkdir wavs

for ((i=1;i<=9;i++)); do
cd ~/wav/train/S000$i
mv * ~/wav/train/wavs
cd .. done

for ((i=10;i<=99;i++)); do
cd ~/wav/train/S00$i
mv * ~/wav/train/wavs
cd .. done

for ((i=100;i<=917;i++)); do
cd ~/wav/train/S0$i
mv * ~/wav/train/wavs
cd .. done

echo "ok"


but i got the error which i dont understand:



./untar.sh: line 24: cd: /wav/train/S0917: No such file or directory 
cp: target '/wav/train/wavs/' is not a directory ok









share|improve this question















i have so many files under 300 different directories, file tree like:



--s1
- ----abc
- ----bcd
--s2
- ----123
- ----234


... etc.



I want to put them together under the same directory, like:



--whole
- ----abc
- ----bcd
- ----123
- ----234 ...


Is there any useful bash script in practical?



I coded this piece of bash script:



mkdir wavs

for ((i=1;i<=9;i++)); do
cd ~/wav/train/S000$i
mv * ~/wav/train/wavs
cd .. done

for ((i=10;i<=99;i++)); do
cd ~/wav/train/S00$i
mv * ~/wav/train/wavs
cd .. done

for ((i=100;i<=917;i++)); do
cd ~/wav/train/S0$i
mv * ~/wav/train/wavs
cd .. done

echo "ok"


but i got the error which i dont understand:



./untar.sh: line 24: cd: /wav/train/S0917: No such file or directory 
cp: target '/wav/train/wavs/' is not a directory ok






command-line bash scripts directory mv






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 2 at 10:13









muru

130k19273463




130k19273463










asked Apr 2 at 8:56









David Aksnes

82




82











  • Hello, David, I've wrote an answer which uses find, that IMO is the easiest way to do this job. If you need to create a bash script here is provided two variants that solve a similar task: askubuntu.com/a/1020671/566421
    – pa4080
    Apr 2 at 9:57
















  • Hello, David, I've wrote an answer which uses find, that IMO is the easiest way to do this job. If you need to create a bash script here is provided two variants that solve a similar task: askubuntu.com/a/1020671/566421
    – pa4080
    Apr 2 at 9:57















Hello, David, I've wrote an answer which uses find, that IMO is the easiest way to do this job. If you need to create a bash script here is provided two variants that solve a similar task: askubuntu.com/a/1020671/566421
– pa4080
Apr 2 at 9:57




Hello, David, I've wrote an answer which uses find, that IMO is the easiest way to do this job. If you need to create a bash script here is provided two variants that solve a similar task: askubuntu.com/a/1020671/566421
– pa4080
Apr 2 at 9:57










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Maybe the simplest way to do this job is by the command find (by default it works recursively):



find ~/wav/train/S* -type f -name "*.wav" -exec echo mv ~/wav/train/wavs/ ;


  • ~/wav/train/S* is the search path and it will match to each sub dir that starts with S.


  • -type f will limit the search only to the files.


  • -name "*.wav" will limit the search only to the files that ends with .wav. Not mandatory.


  • -exec ... ; will execute the mentioned command once for each search coincidence.


  • is a variable that contains the coincidence item.


  • remove echo from the command echo mv ~/wav/train/wavs/ to do the action.


Further if you want to delete the directories you can use a command as one of these:



find ~/wav/train/S* -type d -name "S*" -exec echo rm -r ;
find ~/wav/train/S* -type d -name "S*" -delete





share|improve this answer




















    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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    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%2f1021266%2fhow-to-move-files-under-multi-directories-into-same-directory%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Maybe the simplest way to do this job is by the command find (by default it works recursively):



    find ~/wav/train/S* -type f -name "*.wav" -exec echo mv ~/wav/train/wavs/ ;


    • ~/wav/train/S* is the search path and it will match to each sub dir that starts with S.


    • -type f will limit the search only to the files.


    • -name "*.wav" will limit the search only to the files that ends with .wav. Not mandatory.


    • -exec ... ; will execute the mentioned command once for each search coincidence.


    • is a variable that contains the coincidence item.


    • remove echo from the command echo mv ~/wav/train/wavs/ to do the action.


    Further if you want to delete the directories you can use a command as one of these:



    find ~/wav/train/S* -type d -name "S*" -exec echo rm -r ;
    find ~/wav/train/S* -type d -name "S*" -delete





    share|improve this answer
























      up vote
      1
      down vote



      accepted










      Maybe the simplest way to do this job is by the command find (by default it works recursively):



      find ~/wav/train/S* -type f -name "*.wav" -exec echo mv ~/wav/train/wavs/ ;


      • ~/wav/train/S* is the search path and it will match to each sub dir that starts with S.


      • -type f will limit the search only to the files.


      • -name "*.wav" will limit the search only to the files that ends with .wav. Not mandatory.


      • -exec ... ; will execute the mentioned command once for each search coincidence.


      • is a variable that contains the coincidence item.


      • remove echo from the command echo mv ~/wav/train/wavs/ to do the action.


      Further if you want to delete the directories you can use a command as one of these:



      find ~/wav/train/S* -type d -name "S*" -exec echo rm -r ;
      find ~/wav/train/S* -type d -name "S*" -delete





      share|improve this answer






















        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Maybe the simplest way to do this job is by the command find (by default it works recursively):



        find ~/wav/train/S* -type f -name "*.wav" -exec echo mv ~/wav/train/wavs/ ;


        • ~/wav/train/S* is the search path and it will match to each sub dir that starts with S.


        • -type f will limit the search only to the files.


        • -name "*.wav" will limit the search only to the files that ends with .wav. Not mandatory.


        • -exec ... ; will execute the mentioned command once for each search coincidence.


        • is a variable that contains the coincidence item.


        • remove echo from the command echo mv ~/wav/train/wavs/ to do the action.


        Further if you want to delete the directories you can use a command as one of these:



        find ~/wav/train/S* -type d -name "S*" -exec echo rm -r ;
        find ~/wav/train/S* -type d -name "S*" -delete





        share|improve this answer












        Maybe the simplest way to do this job is by the command find (by default it works recursively):



        find ~/wav/train/S* -type f -name "*.wav" -exec echo mv ~/wav/train/wavs/ ;


        • ~/wav/train/S* is the search path and it will match to each sub dir that starts with S.


        • -type f will limit the search only to the files.


        • -name "*.wav" will limit the search only to the files that ends with .wav. Not mandatory.


        • -exec ... ; will execute the mentioned command once for each search coincidence.


        • is a variable that contains the coincidence item.


        • remove echo from the command echo mv ~/wav/train/wavs/ to do the action.


        Further if you want to delete the directories you can use a command as one of these:



        find ~/wav/train/S* -type d -name "S*" -exec echo rm -r ;
        find ~/wav/train/S* -type d -name "S*" -delete






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 2 at 9:50









        pa4080

        12.2k52256




        12.2k52256



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1021266%2fhow-to-move-files-under-multi-directories-into-same-directory%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Which professions warranted travel in Medieval times?

            How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

            Trouble downloading packages list due to a “Hash sum mismatch” error