Top

A Linux User Reference

Search tips
  • search ignores words that are less than 4 characters in length
  • searches are case insensitve
  • if a search does not return anything try it in Boolean mode then Query expansion mode by checking the appropriate radio button e.g. searching for 'cron' in just the Administration category returns nothing - presumably because the 50% threshold is reached. Boolean mode ignores this threshold so a search for 'cron' returns several hits
  • in Boolean mode preceding a word with a '+' means the result must include that word, a '-' means it must not
  • in Boolean mode '+crontab -anacron' means match articles about crontab that DO NOT mention anacron
  • to match a phrase e.g. 'manage system' check the Boolean mode radio button and enclose the phrase in quotes "some phrase ..."
  • in Query expansion mode the search context is expanded beyond the keywords you entered - relevancy of hits may well be degraded

FILES AND DIRECTORIES

Finding files

  • File name globbing

    Globbing involves using groups of one or more charcaters, symbols to represent a pattern to be matched

    Glob character Meaning
    * Match zero or more characters
    ? Match exactly any single character
    [chars] Match any single character listed in brackets
    [!chars] Match any single character not listed in brackets
    [char-char] Match any single character in the range in brackets
    [!char-char] Match any single character not in the range in brackets
    {char, char, ..} Brace expansion

    List all files whose filenames start with file

    $ ls file*
    file11  file2  file22  file3
    

    List all files whose filenames start with file followed by any single character

    $ ls file?
    file2 file3
    

    List all files with filenames file2 or file3

    $ ls file[23]
    file2 file3
    

    List all files with filenames that start with file followed by and single character except 3

    $ ls file[!3]
    file2
    

    List all files whose filenames start with file followed by zero or any single letter

    $ ls fil[a-z]
    file
    

    List all files whose filenames start with fil followed by zero or any single non alphabet letter

    $ ls fil[!a-z]
    fil1
    

    List all files with filenames file2, file3, file11 and file22

    $ ls file{2,3,11,22}
    
  • Locate a command
    /usr/bin/which

    Command usage

    which [-a] filename ...
    

    Display all matching pathnames for the which command file

    $ which -a which
    /usr/bin/which
    /bin/which
    

    Display cuurent path to command

    $ which  which
    /usr/bin/which
    
  • Search for files
    /usr/bin/find

    Command usage

    find [-H] [-L] [-P] [path...] [expression]
    
    Common options:
     -H                Never follow symlinks except when processing cmd-line args
     -L                Follow symlinks
     -P                Never follow symlinks
    

    See manpages for full details.

    Find all files in /home with 'file' in the name

    $ find /home -name "*file*"
    

    Find all files with the same inode as file1 - hard links

    # find . -samefile file1
    ./linkdir/file1
    ./file3
    ./file1
    

    Find all symbolic links to file1

    $ find . -lname file1 -exec ls -al {} \;
    lrwxrwxrwx 1 root root 5 2009-03-31 16:37 ./file2 -> file1
    

    Find all files that have the SUID bit set, do not search /proc and it's sub dirs

    # find / -path '/proc' -prune -or -perm -u+s -ls
    1086450    8 -rwsr-xr-x   1 mark  mark    194 Nov 21 13:50 /home/mark/if_script
      62829   68 -rwsr-xr-x   1 root  root  60432 Oct  8 14:41 /bin/mount
      62843   44 -rwsr-xr-x   1 root  root  40648 Apr  6  2007 /bin/ping
      62821   36 -rwsr-xr-x   1 root  root  30752 Oct 30 17:59 /bin/su
    .....
    

    Find all files that have the GUID bit set, do not search /proc and it's sub dirs

    # find / -path '/proc' -prune -or -perm -g+s -ls**
    1086449    8 -rwxr-sr-x   1 mark  mark     1194 Nov 21 16:32 /home/mark/getops_script
    2164386   96 -rwxr-sr-x   1 root  nobody  87160 Mar 20  2007 /usr/bin/ssh-agent
    2163045  316 -rwsr-sr-x   1 root  root   315392 Sep 11 10:14 /usr/bin/crontab
    .....
    

    Find (6) next level directories in my home directory

    $ find ~ -maxdepth 1 -type d | head -n 6
    /home/mark
    /home/mark/mess
    /home/mark/lighttpd
    /home/mark/.compiz
    /home/mark/.gnupg
    /home/mark/.tomboy
    

    Find files in /home that have been modified within the last 24 hours

    $ find /home -mtime -1 | head -n 6
    find: '/home/lost+found': Permission denied
    /home/mark
    /home/mark/.dmrc
    /home/mark/.config/gtk-2.0
    /home/mark/.config/gtk-2.0/gtkfilechooser.ini
    /home/mark/.gnome2/accels/evolution
    /home/mark/.gnome2/accels/aisleriot
    

    Find all files with the same inode as file1 - hard links

    # find . -samefile file1
    ./linkdir/file1
    ./file3                                             (Same inode as file1)
    ./file1                                             (Same inode as file3)
    
    # rm file1                                          (Delete file1)
    # ls -al file[13]
    -rw-r--r-- 1 mark mark 9 2009-03-31 15:52 file3     (file3 remains)
    
    # stat file3 | grep Links
    Device: 808h/2056d   Inode: 115458   Links: 1       (file1's inode still exists)
    

    In older versions you had to tell 'find' to print what it found - not so now

    $ find . -name '*.php' 
    ./lunuxref/static_a/sec/sec-psent.php
    ./lunuxref/static_a/sec/sec-snort.php
    ./lunuxref/static_a/sec/sec-fail2ban.php
    .....
    
  • Find files by name
    /usr/bin/locate

    Parses /etc/updatedb.conf.

    locate [options] pattern ...
    
    Common options:
     -b | --basename               Match only the base name of path names
     -c | --count                  Only print number of found entries
     -L | --follow                 Follow trailing symbolic links when checking file 
                                   existence (default)
     -i | --ignore-case            Ignore case distinctions when matching patterns
     -l | --limit | -n LIMIT       Limit output (or counting) to LIMIT entries
     -P | --nofollow | -H          Don't follow trailing symbolic links when checking
                                   file existence
     -r | --regexp REGEXP          Search for basic regexp REGEXP instead of patterns
     --regex                       Patterns are extended regexps
     -w | --wholename              Match whole path name (default)
    

    Find all files named exactly bash

    $ locate '\bash'
    /archives/usrdocs/doc/bash
    /bin/bash
    /etc/apparmor.d/abstractions/bash
    /usr/share/bash
    /usr/share/menu/bash
    

    Locate files with 'eastwood' in the filename

    $ locate 'eastwood'
    /home/mark/my-website-backup/10.24.2013/bandwidth/blog.markweastwood.co.uk
    /home/mark/my-website-backup/10.24.2013/bandwidth/blog.markweastwood.co.uk-all-peak.xml
    .....
    /home/mark/my-website-backup/10.24.2013/vf/skeleton.markweastwood.co.uk
    /home/mark/my-website-backup/10.24.2013/vf/steves.markweastwood.co.uk
    
  • Security enhanced find files by name
    /usr/bin/slocate

    Could well be dated ...

    Parses /etc/updatedb.conf though not all the options provided by this .conf file are supported. Read manpage for more information.

    slocate [options] search-string
    
  • Update mlocate database
    /usr/bin/updatedb
    • /etc/updatedb.conf is the configuration file but is not required to run.
    • the configuration file allows setting of some extra options as variables.
    updatedb [options]
    
    Common options:
     --prunefs FS             Override config file's PRUNEFS variable with this one
                              FS is a white-space separated list of filesystems to ignore
     --prunepaths PATHS       Overide config file's PRUNEFS variable with this one
                              PATH is a white-space separated list of dirs to ignore
    

    Ubuntu default with additional comments - /etc/updatedb.conf

    # Format: <variable> = <value>
    
    # <variable>              Is an alphanumeric string which does not start with a digit
    # <value>                 Can contain any character except for " (double quote).  No escape
    #                         mechanism is supported nor can the value span more than one line.
    
    # Unknown variable values are considered an error.  
    #Defined variables are:
    # PRUNEFS                 Space-separated list of file system types (as used in /etc/mtab) 
    #                         which should not be scanned.
    # PRUNENAMES              Space-separated list of dirnames (without paths) which should 
    #                         not be scanned.
    # PRUNEPATHS              Space-separated list of pathnames of dirs which should not be 
    #                         scanned.
    # PRUNE_BIND_MOUNTS       0|no|1|yes.  If PRUNE_BIND_MOUNTS is 1 or yes, bind mounts are 
    #                         not scanned.
    
    PRUNE_BIND_MOUNTS="yes"
    # PRUNENAMES=".git .bzr .hg .svn"
    PRUNEPATHS="/tmp /var/spool /media"
    PRUNEFS="NFS nfs nfs4 rpc_pipefs afs binfmt_misc proc smbfs autofs iso9660 ncpfs \
    coda devpts ftpfs devfs mfs shfs sysfs cifs lustre_lite tmpfs usbfs udf"
    
    • When a directory is matched by PRUNEFS, PRUNENAMES or PRUNEPATHS 'updatedb' does not scan the contents of the directory.
    • The path of the directory though is entered in the created database.

    For example, if /tmp is in the PRUNEPATHS list, 'locate' will not show any files stored in /tmp, but it can show the /tmp directory.