Announcement

Collapse
No announcement yet.

BASH issue -- shopt -s dotglob

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    BASH issue -- shopt -s dotglob

    This applies to 8.04 and may also apply to 8.10.

    Does your file
    ~/.bashrc
    include the following:

    # Some comment, like:
    # This ensures that * includes hidden folders and files
    shopt -s dotglob


    Strangely, I've twice encountered it in 8.04 where mine does not include it.
    It ensures that hidden files are removed (rm) or copied (cp) when used with the * wildcard, like
    rm -r dir/*
    or
    cp -r dir1/* dir2

    Most users (myself included) would not think to look for this unless they got some errors after using cp, rm, etc., with *.


    An intellectual says a simple thing in a hard way. An artist says a hard thing in a simple way. Charles Bukowski

    #2
    Re: BASH issue -- shopt -s dotglob

    Do you mean that 'dotglobbing' only works sometimes?

    Any examples when it works or doesn't work?

    Comment


      #3
      Re: BASH issue -- shopt -s dotglob

      Actually, what I meant was that it always works but that it was not included in my ~/.bashrc file. I started noticing that
      cp -r directory1/* directory2
      was not picking up the hidden files, and that's what got me onto this. It was you who told me about shopt -s dotglob. I never noticed this problem before 8.04, but saw it in 8.04 and 8.10.

      My concern is that most "normal" users are taught how to write such statements as cp -r, rm -r, etc. using the wild card *, but no mention is made that the statement may not include hidden files. In fact, in the dozen or so beginner tutorials I've read on using CLI in Linux, I had never seen mention of this issue. You were the first to bring it to my attention.

      So:
      => Seems that BASH/Konsole/whatever should come shipped with shopt -s dotglob set up in ~/.bashrc.
      An intellectual says a simple thing in a hard way. An artist says a hard thing in a simple way. Charles Bukowski

      Comment


        #4
        Re: BASH issue -- shopt -s dotglob

        Interesting. For years I have known that the default behavior of bash is that * does not include *.*. In fact I count on it. I was unaware of the dotglob option, but it would screw me totally, because it is not what I expect.

        Comment


          #5
          Re: BASH issue -- shopt -s dotglob

          Yes, there sure seems to be a disconnect wrt this issue.
          I'm getting the impression that if your are a BASH expert/programmer, you take for granted that you must explicitly enable shopt -s dotglob. Whereas, the tutorials never mention it, yet they all cover the basic copy operations involving * :
          cp -r dir1/* dir2
          And how often do we post such code here for people to copy? All the time.
          There are hidden files everywhere, two very common situations are in /home/user_name and when copying Live CD contents (e.g., the .disk folder).
          Strange, indeed. I wonder how many "regular" people know/think about this?
          An intellectual says a simple thing in a hard way. An artist says a hard thing in a simple way. Charles Bukowski

          Comment


            #6
            Re: BASH issue -- shopt -s dotglob

            Originally posted by Qqmike
            => Seems that BASH/Konsole/whatever should come shipped with shopt -s dotglob set up in ~/.bashrc.
            1. No matter what the default was, there would always be people that found the behaviour different from what they are used to, in that regard no dotglobbing seems to be less destructive than dotglobbing for such people.

            2. Also, globbing is in most cases not necessary when copying whole directories, instead of
            Code:
            cp -r dir1/* dir2
            one could just do
            Code:
            cp -r dir1 dir2
            (or cp -r dir1/ dir2/)
            Which copies everything, including dotfiles

            3. Globbing itself is powerful, but also a dangerous tool for new users, most of whom don't use command line that much. Dotglobbing defaults probably won't matter to new users who work mostly with dolphin.

            4. There is also consistency between different shells (non-bash) to consider.

            I think the cons outweigh the pros in this matter, but that is just my opinion. I can understand the pro arguments as well.

            Comment


              #7
              Re: BASH issue -- shopt -s dotglob

              Points well taken, makes sense.

              I sure have a mental block here, stuck.
              cp -r dir1 dir2

              If dir2 does not already exist, then I see it: that command will do the trick (copies all dir1 CONTENTS into a (new) directory named dir2).

              But here's my usual situation:
              If dir2 already exists, that command will create a directory named dir1 within dir2, and so: dir2/dir1
              whereas the desired goal is to copy only all the contents of dir1 into dir2 (and not copy dir1 itself as a subdirectory of dir2).

              Example
              dir1 contains x, y, z
              dir2 contains w
              the desired end result is:
              dir2 contains w, x, y, z


              Just must have a mental block after fixing broken filesystems (that I broke) on (previously bootable) flash drives all day (at least I did succeed doing that )





              An intellectual says a simple thing in a hard way. An artist says a hard thing in a simple way. Charles Bukowski

              Comment


                #8
                Re: BASH issue -- shopt -s dotglob

                Originally posted by Qqmike
                But here's my usual situation:
                If dir2 already exists, that command will create a directory named dir1 within dir2, and so: dir2/dir1
                whereas the desired goal is to copy only all the contents of dir1 into dir2 (and not copy dir1 itself as a subdirectory of dir2).

                Example
                dir1 contains x, y, z
                dir2 contains w
                the desired end result is:
                dir2 contains w, x, y, z
                in this case, "cp -rT dir1 dir2" should do it

                Comment


                  #9
                  Re: BASH issue -- shopt -s dotglob

                  Lessons learned. Thanks.

                  Actually, thanks again. Second time you had to tell me this!
                  Just found my notes on it, including the two links you gave (in case someone who pays better attention than I is reading this):

                  Quote (kubicle)
                  The command
                  cp -RT dir1 DESTfolder
                  will always copy all files in dir1 into DESTfolder, including hidden files.
                  T (upper case)
                  http://www.gnu.org/software/coreutil...rget-directory
                  Target Directory: T
                  http://www.gnu.org/software/coreutil...rget-directory

                  Printed all this out on hard copy. Broke open a new box of bright yellow highlighters. Going to post it (on my bathroom mirror) and study it for a day or two. It oughta stick this time.

                  Thanks for your patience and for your responses.
                  --Mike

                  An intellectual says a simple thing in a hard way. An artist says a hard thing in a simple way. Charles Bukowski

                  Comment


                    #10
                    Re: BASH issue -- shopt -s dotglob

                    I edited my how-to to make this matter clear.
                    Commands at Konsole: Beginners
                    http://kubuntuforums.net/forums/inde...opic=3091607.0
                    Part 2 is the main section addressing recursive copy; and there is a reference to it in Part 3 under Wildcards, copying.

                    Thanks again for this clarification.
                    Mine is probably the only beginner how-to that treats this, thanks to you.
                    An intellectual says a simple thing in a hard way. An artist says a hard thing in a simple way. Charles Bukowski

                    Comment

                    Working...
                    X