Announcement

Collapse
No announcement yet.

Bash Tricks and Tips requests

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

    #16
    Can someone give some commonplace, simple examples of using "find".

    .

    Comment


      #17
      Originally posted by sealbhach View Post
      Can someone give some commonplace, simple examples of using "find".
      To find a file called example.txt
      Code:
      find -name example.txt
      To find any file with the extension ".pdf"
      Code:
      find -name "*.pdf"
      To find any file or directory with "pattern" in its name:
      Code:
      find -name "*pattern*"
      To find a file with "pattern" in its name in side the directory "/path/to/search"
      Code:
      find /path/to/search -name "*pattern*"
      Use -iname instead of -name to ignore the case matching.

      The * is important in find, but you cannot let bash try to expand it so you have to escape or quote it.
      ie
      Code:
      find -name *.pdf    # Wrong
      find -name "*.pdf"  # Right
      find -name \*.pdf   # Right
      You can also add extra filters which are covered in the man page.
      Last edited by james147; Nov 21, 2012, 03:15 PM.

      Comment


        #18
        This thread is advanced beyond the basics of, say, using Konsole, vs this basic how-to:
        http://www.kubuntuforums.net/showthr...inners-3-parts

        But I wonder if the following fits in here somehow. Not being any expert on BASH, I was kind of surprised when I ran across this "subtlety" (with the help of kubicle). You see people using cp -r everywhere; but I wonder how many people (beginners) realize this? If you are copying system files to build, say, a bootable flash drive, this becomes very important.

        I'll just copy the brief discussion from Part 2 of my How-to here (again, thanks to kubicle):

        - - - - -

        Hidden folders and files, use of *: shopt -s dotglob
        Copying hidden folders (and files)

        Consider the statement
        cp -r dir1/* dir2
        which copies the contents of directory dir1 into directory dir2.
        To ensure that the wild card * includes ALL files, including HIDDEN files and HIDDEN folders, you might run it this way:

        shopt -s dotglob
        cp -r dir1/* dir2

        Or, edit your file
        ~/.bashrc to include the following:

        # This ensures that * includes hidden folders and files
        shopt -s dotglob

        (Then File > Save, File > Quit)

        Another way:
        cp -RT dir1 dir2
        has exactly the same end result as
        shopt -s dotglob
        cp -r dir1/* dir2

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

        Comment


          #19
          Gems I wish I'd come across when I first used bash (or ksh, for that matter):

          alias path='echo -e ${PATH//:/\\n}' # display $PATH legibly
          alias b='cd -' # go back

          function e { env | grep -i ${*-.} | sort ; } # sorted, case insensitive, env

          Regards, John Little
          Regards, John Little

          Comment


            #20
            Originally posted by SteveRiley View Post
            Someplace I found a very informative webpage explaining, with several examples, why $() is better than backquotes. I'll try to find it later when I'm on my PC.
            Here's one: http://wiki.bash-hackers.org/syntax/expansion/cmdsubst

            Comment


              #21
              Originally posted by james147 View Post
              Going to cover this but you might be interest now:
              $ cat somefile
              $ kate !$

              or my preferred method:
              $ alias open=kde-open
              $ cat somefile
              $ open !$

              bash history is such a wonderful thing
              Great idea.

              Done!

              Please Read Me

              Comment

              Working...
              X