HB Computer Security | Aide Memoire
  • Introduction
  • Core Technical Skills
    • Core Skills
      • Linux
        • Getting to Know Linux
          • Using the Shell
            • Shells, Terminals, and Virtual Consoles
            • Choose your Shell
            • Shell Commands
              • Non-PATH Commands
              • Command History
                • Command Line Editing
                  • Keystrokes for Navigating Command Lines
                  • Keystrokes for Editing Command Lines
                  • Keystrokes for Cutting and Pasting Text from within Command Lines
                • Command Line Recall
                  • Keystrokes for Command Line Recall
              • Connecting and Expanding Commands
                • Piping Between Commands
                • Sequential Commands
                • Expanding Commands
            • Shell Variables
              • Common Shell Variables
            • Aliases
            • Create your Own Shell Environment
              • Modification Ideas
          • Navigating the Linux File System (LFS)
            • Filesystem Commands
            • Listing Files and Directories
            • File Permissions and Ownership
              • Modifying Permissions with chmod
              • Modifying Default Permissions with umask
              • Change File Ownership with chown
            • Copying, Moving, and Removing Files
            • Finding Files
              • locate
              • find
              • grep
            • Downloading Files
              • axel
              • wget
              • curl
                • User-Agent: Googlebot
          • Working with Text Files
            • Using vim and vi to Edit Text Files
              • Starting with vi
              • Adding Text
              • Moving Around in the Text
            • Text Manipulation
        • System Administration
          • Installing Linux
            • Installing from Live Media
            • Installing in the Enterprise
            • Partitioning Hard Disks
              • Tips for Creating Partitions
          • Account Administration
            • The root Account
              • Becoming root with su
              • sudo
                • The /etc/sudoers File
                • Granting sudo privileges
                  • visudo Guidance
                • Useful sudo Hints
            • Other Administrative Accounts
            • Standard User Accounts
              • Risks of userdel: Orphaned Files
          • Graphical Remote Administration
            • Cockpit
              • Installation Guide
            • Remote Desktop Protocol with xrdp
              • Installation and Configuration
            • Remote Desktop with vnc
              • Installation and Configuration
              • Running VNC as a System Service
          • Managing Running Processes
            • Listing Processes
              • ps
              • top
              • htop
            • Backgrounding and Foregrounding
              • Starting a Background Process
              • Using Foreground and Background Commands
            • Killing and Recining Processes
              • kill and killall
          • Managing Software
            • Managing Software from the Desktop
            • Going Beyond the Limitations of Software Center
              • Debian Packages
                • Advanced Package Tool (apt)
                • Repositories
                • dpkg
        • Shell Scripting
          • Variables
            • Command Substitution
            • Arguments
          • Reading User Input
          • if, else, and elif
          • BOOLEAN Logic
          • Loops
            • for Loops
            • while Loops
          • Functions
          • Local Vs Global Variables
          • Summary
        • Securing Linux
      • Windows
        • Security Hardening
Powered by GitBook
On this page
  • Using locate

Was this helpful?

  1. Core Technical Skills
  2. Core Skills
  3. Linux
  4. Getting to Know Linux
  5. Navigating the Linux File System (LFS)
  6. Finding Files

locate

On most Linux systems, the updatedb command runs once per day to gather the names of files throughout your Linux system and stores them in a local database. You can then run the locate command to search this database to find the location of named files stored within it.

Like anything, there are pros and cons to this approach...

Advantage

Disadvantage

The locate command finds files much faster than alternative methods because it searches a database instead of having to search the whole filesystem in live time

The locate command cannot find any files added to the system since the last time the database was updated via updatedb

Not every file in your filesystem is stored in the database - see below

The contents of the /etc/updatedb.conf file limit which filenames are collected by excluding specific mount types, filesystem types, file types, and mount points. For instance, filenames are excluded from remotely mounted filesystems like CIFS and NFS, as well as locally mounted CDs or DVDs in the iso9660 format. Additionally, paths containing temporary files in /tmp and spool files in /var/spool/cups are pruned.

You have the flexibility to add or remove items from the locate database pruning list to customise it according to your requirements. For instance, in RHEL 8, the updatedb.conf file includes the following:

PRUNE_BIND_MOUNTS = "yes"
PRUNEFS = "9p afs anon_inodefs auto autofs bdev binfmt_misc cgroupcifs coda configfs cpuset debugfs devpts ecryptfs exofs fuse fuse.sshfs fusectl gfs gfs2 gpfs hugetlbfs inotifyfs iso9660 jffs2lustre mqueue ncpfs nfs nfs4 nfsd pipefs proc ramfs rootfs rpc_pipefs securityfs selinuxfs sfs sockfs sysfs tmpfs ubifs udf usbfsceph fuse.ceph"
PRUNENAMES = ".git .hg .svn .bzr .arch-ids {arch} CVS"
PRUNEPATHS = "/afs /media /mnt /net /sfs /tmp /udev /var/cache/ccache /var/lib/yum/yumdb /var/lib/dnf/yumdb /var/spool/cups /var/spool/squid /var/tmp /var/lib/ceph"

As a regular user, you can't see any files from the locate database that you can't see in the filesystem normally. For example, if you can't type ls to view files in the /root directory, you can't locate files stored in that directory either.

Using locate

When you search for a string, the string can appear anywhere in a file’s path. For example, if you search for passwd, you could turn up:

  • /etc/passwd

  • /usr/bin/passwd

  • /home/example/passwd/pwdfiles.txt

  • ...and any number of other places

If you add files to your system after updatedb runs, you can’t locate those files until updatedb runs again. To get the database to contain all files up to the current moment, you can simply run updatedb from the shell, but this must be done with root privileges.

Here are some examples of using the locate command to search for files:

locate .bashrc

When run as a regular user, locate only finds .bashrc in /etc/skel and the user’s own home directory. However, when run as root, the same command locates .bashrc files in everyone’s home directory.

Using locate -i, filenames are found regardless of case sensitivity.

PreviousFinding FilesNextfind

Last updated 1 year ago

Was this helpful?

Running locate to find any files that contain the string ".bashrc" in their name