000032835 - How to find particular files on Linux, Unix or POSIX operating systems

Document created by RSA Customer Support Employee on Jun 14, 2016Last modified by RSA Customer Support Employee on Apr 21, 2017
Version 2Show Document
  • View in full screen mode

Article Content

Article Number000032835
Applies ToRSA Product Set: All
O/S: Linux/Unix (POSIX) 
O/S Version: All
 
IssueSome files on the Linux/Unix (POSIX) operating system can have dynamic locations or may have been moved from their expected locations.  This can lead to an issue when following instructions which specify static file locations.  Sometimes the file contents may be known but the file location or even the name of the file may be unknown.
Tasks
  • Use the locate or find commands to locate the files being sought.
  • Use the grep command to locate text files where  something is known about the content of the file, but not its file name or location.
ResolutionThe locate command
  • The locate command is used to identify the directory path under which a particular file can be found.  Options include control over the case sensitivity or the ability to specify regex matching.
  • This command relies on an internal database which saves location information about each file on the system.  This database may not have been built at the time the locate command is used, or be out of date.  In the case where the database does not exist, the locate command will either build the database at that time or provide a command which can be used to collect and build the database.  In the case where the database is out of date, the updatedb command can be used to update the database.  For more information regarding the locate database run man updatedb.
  • Note that changes to the file system since the last database update (updatedb) will not be reflected in locate results until the next database update.
  • Examples:

  • acm-691:/ # locate aveksa
    /etc/ssh/sshd_config.aveksa
    /etc/vsftpd.conf.aveksa
    acm-691:/ #

    acm-691:/ # locate locate
    /etc/apparmor/profiles/extras/etc.cron.daily.slocate.cron
    /etc/postfix/relocated
    /etc/postfix/relocated.db
    /etc/sysconfig/locate
    /u01/app/oracle/product/11.2.0/db_1/sysman/admin/emdrep/sql/core/latest/jobTypes_sql/RelocateTarget.sql
    /u01/app/oracle/product/11.2.0/db_1/sysman/admin/emdrep/sql/core/latest/target/relocate_target_pkgbody.sql
    /u01/app/oracle/product/11.2.0/db_1/sysman/admin/emdrep/sql/core/latest/target/relocate_target_pkgdef.sql
    /usr/bin/dislocate
    /usr/bin/fallocate
    /usr/bin/locate
    /usr/bin/rpmlocate
    /usr/lib/gnome-settings-daemon/gsd-locate-pointer
    (...)

  • Example using updatedb, and if it succeeds (&&), locate:

  • acm-691:/ # updatedb && locate aveksaServer.log
    /home/oracle/jboss-4.2.2.GA/server/default/deploy/aveksa.ear/aveksa.war/log/aveksaServer.log
    acm-691:/ #

The find command
  • The find command is a more traditional search.  The search parameters are specified and the file structure inspected to locate any matches to those criteria.  There are a great many options available, including searches based on size or type of file, ownership information, and a large number of other options.
  • The most basic find usage is to search for a particular file name in the entire files system:

  • acm-691:/ # find / -name grep
    /bin/grep
    /usr/bin/grep
    /usr/share/doc/packages/grep
    acm-691:/ #

  • Wildcards or spaces can be used in the -name option, but the value used must be surrounded by double quotes ("):

  • acm-691:/ # find / -name "*grep"
    /bin/egrep
    /bin/grep
    /bin/fgrep
    /usr/bin/zgrep
    /usr/bin/mgrep
    (...)

  • Because we love Aveksa!

  • acm-691:/ # find / -name aveksaServer.log
    /home/oracle/jboss-4.2.2.GA/server/default/deploy/aveksa.ear/aveksa.war/log/aveksaServer.log
    acm-691:/ #

The grep command
  • The grep command is intended to search text.  At it's most basic it will search through text and print lines which match the pattern specified.  When a file to search is specified it will do so, supporting recursive searches of all files at a lower point in the directory hierarchy.  In the absence of a specified file, grep will attempt to read from standard input (allowing it to be used with Linux/Unix pipes, |).
  • Example:

  • acm-691:/ # mkdir -p /tmp/to/test/grep/commands/
    acm-691:/ # echo "this is my grep text to search" >> /tmp/to/test/grep/commands/test1
    acm-691:/ # echo "this is my OTHER grep text to search" >> /tmp/to/test/grep/commands/test2
    acm-691:/ # ll /tmp/to/test/grep/commands/
    total 12
    -rw-r--r-- 1 root root 31 Mar 28 19:28 test1
    -rw-r--r-- 1 root root 37 Mar 28 19:28 test2
    acm-691:/ # grep -r "my grep text" /tmp/to/
    /tmp/to/test/grep/commands/test1:this is my grep text to search
    acm-691:/ # grep -r "my other grep text" /tmp/to/
    acm-691:/ # grep -r -i "my other grep text" /tmp/to/
    /tmp/to/test/grep/commands/test2:this is my OTHER grep text to search
    acm-691:/ # grep -r "my O* grep text" /tmp/to/
    acm-691:/ # grep -r "my OTHER grep text" /tmp/to/
    /tmp/to/test/grep/commands/test2:this is my OTHER grep text to search
    acm-691:/ # rm -rf /tmp/to/


    The above example creates a directory structure under /tmp/ for testing.  Echo some sample text, output it to two test files at the end of the temporary directory structure, then verify they were written.  Then use several grep commands to show different ways to search for these files based on the content, how to use -r to recursively search the directory structure, and -i to toggle case sensitive/insensitive searches.  As a last step, delete the directories and files that were created while testing (after it is verified that /tmp/to/ was not used by any other files).
  • The grep command supports a large number of options and gives quite a bit of flexibility in forming patterns to match against.  Some more commonly used options would be recursive searching, case insensitivity, inverting matches, or retrieving match counts.  The manual page (man grep) is the best resource for customizing grep searches.
NotesEach Linux command will have a manual entry which is the help documentation for that command.  The following commands will open the help info for each:
man locate
man find
man grep

When in the manual page for a particular command, exit back to the command prompt at any time by pressing the Q key to quit.

Attachments

    Outcomes