How to rename (unhide) all files and subdirectories within a directory?

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











up vote
3
down vote

favorite












I want to make a script to "unhide" all files and directories inside a certain directory in one go, e.g. ./unhide test.



test/
├── sub1
│   └── .hiddenfile1
└── sub2
└── .hiddendir
├── .hiddendirsub
├── .hiddenfile2
└── not.hidden


Desired outcome:



test/
├── sub1
│   └── hiddenfile1
└── sub2
└── hiddendir
├── hiddendirsub
├── hiddenfile2
└── not.hidden


How can I do that?



I'm still new to this and I've been trying to find a solution using find, but stuck at -exec, and rename (or mv) because I'm still struggling to understand how this combination works. :(
So, I'll appreciate if anyone here can give a solution and detailed explanation as well. Thanks.










share|improve this question









New contributor




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



















  • It might help to show your desired outcome
    – wjandrea
    47 mins ago










  • @wjandrea Updated. :)
    – Lukman Hakim
    6 mins ago














up vote
3
down vote

favorite












I want to make a script to "unhide" all files and directories inside a certain directory in one go, e.g. ./unhide test.



test/
├── sub1
│   └── .hiddenfile1
└── sub2
└── .hiddendir
├── .hiddendirsub
├── .hiddenfile2
└── not.hidden


Desired outcome:



test/
├── sub1
│   └── hiddenfile1
└── sub2
└── hiddendir
├── hiddendirsub
├── hiddenfile2
└── not.hidden


How can I do that?



I'm still new to this and I've been trying to find a solution using find, but stuck at -exec, and rename (or mv) because I'm still struggling to understand how this combination works. :(
So, I'll appreciate if anyone here can give a solution and detailed explanation as well. Thanks.










share|improve this question









New contributor




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



















  • It might help to show your desired outcome
    – wjandrea
    47 mins ago










  • @wjandrea Updated. :)
    – Lukman Hakim
    6 mins ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I want to make a script to "unhide" all files and directories inside a certain directory in one go, e.g. ./unhide test.



test/
├── sub1
│   └── .hiddenfile1
└── sub2
└── .hiddendir
├── .hiddendirsub
├── .hiddenfile2
└── not.hidden


Desired outcome:



test/
├── sub1
│   └── hiddenfile1
└── sub2
└── hiddendir
├── hiddendirsub
├── hiddenfile2
└── not.hidden


How can I do that?



I'm still new to this and I've been trying to find a solution using find, but stuck at -exec, and rename (or mv) because I'm still struggling to understand how this combination works. :(
So, I'll appreciate if anyone here can give a solution and detailed explanation as well. Thanks.










share|improve this question









New contributor




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











I want to make a script to "unhide" all files and directories inside a certain directory in one go, e.g. ./unhide test.



test/
├── sub1
│   └── .hiddenfile1
└── sub2
└── .hiddendir
├── .hiddendirsub
├── .hiddenfile2
└── not.hidden


Desired outcome:



test/
├── sub1
│   └── hiddenfile1
└── sub2
└── hiddendir
├── hiddendirsub
├── hiddenfile2
└── not.hidden


How can I do that?



I'm still new to this and I've been trying to find a solution using find, but stuck at -exec, and rename (or mv) because I'm still struggling to understand how this combination works. :(
So, I'll appreciate if anyone here can give a solution and detailed explanation as well. Thanks.







command-line bash scripts batch-rename






share|improve this question









New contributor




Lukman Hakim 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




Lukman Hakim 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 7 mins ago





















New contributor




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









asked 1 hour ago









Lukman Hakim

385




385




New contributor




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





New contributor





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






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











  • It might help to show your desired outcome
    – wjandrea
    47 mins ago










  • @wjandrea Updated. :)
    – Lukman Hakim
    6 mins ago
















  • It might help to show your desired outcome
    – wjandrea
    47 mins ago










  • @wjandrea Updated. :)
    – Lukman Hakim
    6 mins ago















It might help to show your desired outcome
– wjandrea
47 mins ago




It might help to show your desired outcome
– wjandrea
47 mins ago












@wjandrea Updated. :)
– Lukman Hakim
6 mins ago




@wjandrea Updated. :)
– Lukman Hakim
6 mins ago










2 Answers
2






active

oldest

votes

















up vote
2
down vote















You can do that with find as follows:



find /path/to/test -depth -name ".*" -execdir rename -n 's|/.|/|' +


This only prints the renaming actions, if it shows what you want remove the -n option.



Explanations




  • -depth – lets find process from bottom to top, renaming files before their parent directories


  • -name ".*" – lets find search for files (everything is a file) beginning with a dot


  • -execdir … + – execute … in the matched file‘s directory


  • rename 's|/.|/|' – replace the first occurence of “/.” from the matched file’s path (find’s placeholder for which is ) with “/”, essentially removing the dot from the beginning of the filename

    This would be e.g. rename 's|/.|/|' ./.hiddenfile1 in your case, this would be renamed to ./hiddenfile1.

Example run



$ tree -a
.
├── sub1
│   └── .hiddenfile1
└── sub2
└── .hiddendir
├── .hiddendirsub
├── .hiddenfile2
└── not.hidden

$ find ~/test -depth -name ".*" -execdir rename 's|/.|/|' +
$ tree -a
.
├── sub1
│   └── hiddenfile1
└── sub2
└── hiddendir
├── hiddendirsub
├── hiddenfile2
└── not.hidden


Usage in a script



In a script you can simply use positional parameters instead of the path, it may be relative or absolute – just remember to quote correctly:



#!/bin/bash
find "$1" -depth -name ".*" -execdir rename -n 's|/.|/|' +





share|improve this answer





























    up vote
    1
    down vote













    $ tree -a test
    test
    ├── .alsohidden
    ├── sub1
    │   └── .hiddenfile1
    └── sub2
    ├── .hiddendirsub
    ├── .hiddenfile2
    └── not.hidden

    3 directories, 4 files



    $ find test/ -name ".*" -exec rename -n 's|(.*/).(.*)|$1$2|' +
    rename(test/.alsohidden, test/alsohidden)
    rename(test/sub2/.hiddendirsub, test/sub2/hiddendirsub)
    rename(test/sub2/.hiddenfile2, test/sub2/hiddenfile2)
    rename(test/sub1/.hiddenfile1, test/sub1/hiddenfile1)



    • find test/ begin paths with test/, search recursively from this path


    • -exec command + run command on the found files, constructing an argument list.


    • rename -n do not do anything, only show what will be done (remove -n after testing to actually rename)


    • s|old|new| replace old with new


    • (.*/).(.*) save all the chars up to and including the last directory separator/, skip a literal ., save all the subsequent characters


    • $1$2 print the saved patterns





    share|improve this answer




















    • Why does rename accept “|” as a delimiter, but not “_”? Should I ask a new question? ;)
      – dessert
      21 mins ago










    • @dessert omg I just assumed it would allow, I always use | if I need / in the regex (unless I need |). That would be a good question. Maybe it has to be a non-word char or there's a set of permitted chars
      – Zanna
      13 mins 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',
    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
    );



    );






    Lukman Hakim 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%2f1089485%2fhow-to-rename-unhide-all-files-and-subdirectories-within-a-directory%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote















    You can do that with find as follows:



    find /path/to/test -depth -name ".*" -execdir rename -n 's|/.|/|' +


    This only prints the renaming actions, if it shows what you want remove the -n option.



    Explanations




    • -depth – lets find process from bottom to top, renaming files before their parent directories


    • -name ".*" – lets find search for files (everything is a file) beginning with a dot


    • -execdir … + – execute … in the matched file‘s directory


    • rename 's|/.|/|' – replace the first occurence of “/.” from the matched file’s path (find’s placeholder for which is ) with “/”, essentially removing the dot from the beginning of the filename

      This would be e.g. rename 's|/.|/|' ./.hiddenfile1 in your case, this would be renamed to ./hiddenfile1.

    Example run



    $ tree -a
    .
    ├── sub1
    │   └── .hiddenfile1
    └── sub2
    └── .hiddendir
    ├── .hiddendirsub
    ├── .hiddenfile2
    └── not.hidden

    $ find ~/test -depth -name ".*" -execdir rename 's|/.|/|' +
    $ tree -a
    .
    ├── sub1
    │   └── hiddenfile1
    └── sub2
    └── hiddendir
    ├── hiddendirsub
    ├── hiddenfile2
    └── not.hidden


    Usage in a script



    In a script you can simply use positional parameters instead of the path, it may be relative or absolute – just remember to quote correctly:



    #!/bin/bash
    find "$1" -depth -name ".*" -execdir rename -n 's|/.|/|' +





    share|improve this answer


























      up vote
      2
      down vote















      You can do that with find as follows:



      find /path/to/test -depth -name ".*" -execdir rename -n 's|/.|/|' +


      This only prints the renaming actions, if it shows what you want remove the -n option.



      Explanations




      • -depth – lets find process from bottom to top, renaming files before their parent directories


      • -name ".*" – lets find search for files (everything is a file) beginning with a dot


      • -execdir … + – execute … in the matched file‘s directory


      • rename 's|/.|/|' – replace the first occurence of “/.” from the matched file’s path (find’s placeholder for which is ) with “/”, essentially removing the dot from the beginning of the filename

        This would be e.g. rename 's|/.|/|' ./.hiddenfile1 in your case, this would be renamed to ./hiddenfile1.

      Example run



      $ tree -a
      .
      ├── sub1
      │   └── .hiddenfile1
      └── sub2
      └── .hiddendir
      ├── .hiddendirsub
      ├── .hiddenfile2
      └── not.hidden

      $ find ~/test -depth -name ".*" -execdir rename 's|/.|/|' +
      $ tree -a
      .
      ├── sub1
      │   └── hiddenfile1
      └── sub2
      └── hiddendir
      ├── hiddendirsub
      ├── hiddenfile2
      └── not.hidden


      Usage in a script



      In a script you can simply use positional parameters instead of the path, it may be relative or absolute – just remember to quote correctly:



      #!/bin/bash
      find "$1" -depth -name ".*" -execdir rename -n 's|/.|/|' +





      share|improve this answer
























        up vote
        2
        down vote










        up vote
        2
        down vote











        You can do that with find as follows:



        find /path/to/test -depth -name ".*" -execdir rename -n 's|/.|/|' +


        This only prints the renaming actions, if it shows what you want remove the -n option.



        Explanations




        • -depth – lets find process from bottom to top, renaming files before their parent directories


        • -name ".*" – lets find search for files (everything is a file) beginning with a dot


        • -execdir … + – execute … in the matched file‘s directory


        • rename 's|/.|/|' – replace the first occurence of “/.” from the matched file’s path (find’s placeholder for which is ) with “/”, essentially removing the dot from the beginning of the filename

          This would be e.g. rename 's|/.|/|' ./.hiddenfile1 in your case, this would be renamed to ./hiddenfile1.

        Example run



        $ tree -a
        .
        ├── sub1
        │   └── .hiddenfile1
        └── sub2
        └── .hiddendir
        ├── .hiddendirsub
        ├── .hiddenfile2
        └── not.hidden

        $ find ~/test -depth -name ".*" -execdir rename 's|/.|/|' +
        $ tree -a
        .
        ├── sub1
        │   └── hiddenfile1
        └── sub2
        └── hiddendir
        ├── hiddendirsub
        ├── hiddenfile2
        └── not.hidden


        Usage in a script



        In a script you can simply use positional parameters instead of the path, it may be relative or absolute – just remember to quote correctly:



        #!/bin/bash
        find "$1" -depth -name ".*" -execdir rename -n 's|/.|/|' +





        share|improve this answer
















        You can do that with find as follows:



        find /path/to/test -depth -name ".*" -execdir rename -n 's|/.|/|' +


        This only prints the renaming actions, if it shows what you want remove the -n option.



        Explanations




        • -depth – lets find process from bottom to top, renaming files before their parent directories


        • -name ".*" – lets find search for files (everything is a file) beginning with a dot


        • -execdir … + – execute … in the matched file‘s directory


        • rename 's|/.|/|' – replace the first occurence of “/.” from the matched file’s path (find’s placeholder for which is ) with “/”, essentially removing the dot from the beginning of the filename

          This would be e.g. rename 's|/.|/|' ./.hiddenfile1 in your case, this would be renamed to ./hiddenfile1.

        Example run



        $ tree -a
        .
        ├── sub1
        │   └── .hiddenfile1
        └── sub2
        └── .hiddendir
        ├── .hiddendirsub
        ├── .hiddenfile2
        └── not.hidden

        $ find ~/test -depth -name ".*" -execdir rename 's|/.|/|' +
        $ tree -a
        .
        ├── sub1
        │   └── hiddenfile1
        └── sub2
        └── hiddendir
        ├── hiddendirsub
        ├── hiddenfile2
        └── not.hidden


        Usage in a script



        In a script you can simply use positional parameters instead of the path, it may be relative or absolute – just remember to quote correctly:



        #!/bin/bash
        find "$1" -depth -name ".*" -execdir rename -n 's|/.|/|' +






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited just now

























        answered 39 mins ago









        dessert

        20.5k55896




        20.5k55896






















            up vote
            1
            down vote













            $ tree -a test
            test
            ├── .alsohidden
            ├── sub1
            │   └── .hiddenfile1
            └── sub2
            ├── .hiddendirsub
            ├── .hiddenfile2
            └── not.hidden

            3 directories, 4 files



            $ find test/ -name ".*" -exec rename -n 's|(.*/).(.*)|$1$2|' +
            rename(test/.alsohidden, test/alsohidden)
            rename(test/sub2/.hiddendirsub, test/sub2/hiddendirsub)
            rename(test/sub2/.hiddenfile2, test/sub2/hiddenfile2)
            rename(test/sub1/.hiddenfile1, test/sub1/hiddenfile1)



            • find test/ begin paths with test/, search recursively from this path


            • -exec command + run command on the found files, constructing an argument list.


            • rename -n do not do anything, only show what will be done (remove -n after testing to actually rename)


            • s|old|new| replace old with new


            • (.*/).(.*) save all the chars up to and including the last directory separator/, skip a literal ., save all the subsequent characters


            • $1$2 print the saved patterns





            share|improve this answer




















            • Why does rename accept “|” as a delimiter, but not “_”? Should I ask a new question? ;)
              – dessert
              21 mins ago










            • @dessert omg I just assumed it would allow, I always use | if I need / in the regex (unless I need |). That would be a good question. Maybe it has to be a non-word char or there's a set of permitted chars
              – Zanna
              13 mins ago














            up vote
            1
            down vote













            $ tree -a test
            test
            ├── .alsohidden
            ├── sub1
            │   └── .hiddenfile1
            └── sub2
            ├── .hiddendirsub
            ├── .hiddenfile2
            └── not.hidden

            3 directories, 4 files



            $ find test/ -name ".*" -exec rename -n 's|(.*/).(.*)|$1$2|' +
            rename(test/.alsohidden, test/alsohidden)
            rename(test/sub2/.hiddendirsub, test/sub2/hiddendirsub)
            rename(test/sub2/.hiddenfile2, test/sub2/hiddenfile2)
            rename(test/sub1/.hiddenfile1, test/sub1/hiddenfile1)



            • find test/ begin paths with test/, search recursively from this path


            • -exec command + run command on the found files, constructing an argument list.


            • rename -n do not do anything, only show what will be done (remove -n after testing to actually rename)


            • s|old|new| replace old with new


            • (.*/).(.*) save all the chars up to and including the last directory separator/, skip a literal ., save all the subsequent characters


            • $1$2 print the saved patterns





            share|improve this answer




















            • Why does rename accept “|” as a delimiter, but not “_”? Should I ask a new question? ;)
              – dessert
              21 mins ago










            • @dessert omg I just assumed it would allow, I always use | if I need / in the regex (unless I need |). That would be a good question. Maybe it has to be a non-word char or there's a set of permitted chars
              – Zanna
              13 mins ago












            up vote
            1
            down vote










            up vote
            1
            down vote









            $ tree -a test
            test
            ├── .alsohidden
            ├── sub1
            │   └── .hiddenfile1
            └── sub2
            ├── .hiddendirsub
            ├── .hiddenfile2
            └── not.hidden

            3 directories, 4 files



            $ find test/ -name ".*" -exec rename -n 's|(.*/).(.*)|$1$2|' +
            rename(test/.alsohidden, test/alsohidden)
            rename(test/sub2/.hiddendirsub, test/sub2/hiddendirsub)
            rename(test/sub2/.hiddenfile2, test/sub2/hiddenfile2)
            rename(test/sub1/.hiddenfile1, test/sub1/hiddenfile1)



            • find test/ begin paths with test/, search recursively from this path


            • -exec command + run command on the found files, constructing an argument list.


            • rename -n do not do anything, only show what will be done (remove -n after testing to actually rename)


            • s|old|new| replace old with new


            • (.*/).(.*) save all the chars up to and including the last directory separator/, skip a literal ., save all the subsequent characters


            • $1$2 print the saved patterns





            share|improve this answer












            $ tree -a test
            test
            ├── .alsohidden
            ├── sub1
            │   └── .hiddenfile1
            └── sub2
            ├── .hiddendirsub
            ├── .hiddenfile2
            └── not.hidden

            3 directories, 4 files



            $ find test/ -name ".*" -exec rename -n 's|(.*/).(.*)|$1$2|' +
            rename(test/.alsohidden, test/alsohidden)
            rename(test/sub2/.hiddendirsub, test/sub2/hiddendirsub)
            rename(test/sub2/.hiddenfile2, test/sub2/hiddenfile2)
            rename(test/sub1/.hiddenfile1, test/sub1/hiddenfile1)



            • find test/ begin paths with test/, search recursively from this path


            • -exec command + run command on the found files, constructing an argument list.


            • rename -n do not do anything, only show what will be done (remove -n after testing to actually rename)


            • s|old|new| replace old with new


            • (.*/).(.*) save all the chars up to and including the last directory separator/, skip a literal ., save all the subsequent characters


            • $1$2 print the saved patterns






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 39 mins ago









            Zanna

            48.6k13120230




            48.6k13120230











            • Why does rename accept “|” as a delimiter, but not “_”? Should I ask a new question? ;)
              – dessert
              21 mins ago










            • @dessert omg I just assumed it would allow, I always use | if I need / in the regex (unless I need |). That would be a good question. Maybe it has to be a non-word char or there's a set of permitted chars
              – Zanna
              13 mins ago
















            • Why does rename accept “|” as a delimiter, but not “_”? Should I ask a new question? ;)
              – dessert
              21 mins ago










            • @dessert omg I just assumed it would allow, I always use | if I need / in the regex (unless I need |). That would be a good question. Maybe it has to be a non-word char or there's a set of permitted chars
              – Zanna
              13 mins ago















            Why does rename accept “|” as a delimiter, but not “_”? Should I ask a new question? ;)
            – dessert
            21 mins ago




            Why does rename accept “|” as a delimiter, but not “_”? Should I ask a new question? ;)
            – dessert
            21 mins ago












            @dessert omg I just assumed it would allow, I always use | if I need / in the regex (unless I need |). That would be a good question. Maybe it has to be a non-word char or there's a set of permitted chars
            – Zanna
            13 mins ago




            @dessert omg I just assumed it would allow, I always use | if I need / in the regex (unless I need |). That would be a good question. Maybe it has to be a non-word char or there's a set of permitted chars
            – Zanna
            13 mins ago










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









             

            draft saved


            draft discarded


















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












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











            Lukman Hakim 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%2f1089485%2fhow-to-rename-unhide-all-files-and-subdirectories-within-a-directory%23new-answer', 'question_page');

            );

            Post as a guest













































































            Comments

            Popular posts from this blog

            Long meetings (6-7 hours a day): Being “babysat” by supervisor

            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

            Confectionery