🐧
Linux
  • Introduction
  • 🖇️general
    • essentials
    • other commands
    • Superuser-do (sudo)
    • SUID, SGID, sticky bit
    • /dev/null
    • environment variables
    • special variables
    • shebang
  • spool
  • 🔧bash
    • overview
    • redirection
    • loop
    • .bashrc
  • text processing
    • grep
    • sed
    • awk
  • xxd/hexdump
  • text editors
    • nano
    • vi/vim
  • 📂filesystem & directories
    • Filesystem Hierachy Standard (FHS)
      • /etc
        • hosts, hosts.allow, hosts.deny
        • /cron.d
        • /httpd
        • /samba.d
        • hostname
        • crontab
        • shadow
        • passwd
        • profile
        • services
      • /dev
      • /proc
        • version
      • /mnt
      • /opt
      • /sbin
      • /lib
      • /usr
      • /tmp
      • /var
      • /bin
      • /run
    • chroot
  • find
  • locate
  • ⌚processes & jobs
    • cronjob
    • daemon
  • ⛓️system
    • systemctl
    • hostname
    • systemd
  • 🗃️media
    • ffmpeg
    • pdftk
  • 🔒Security
    • ufw
  • firejail
  • apparmor
  • 📦Package management
    • dpkg
    • apt/apt-get
  • Storage
    • lsblk
    • mount/umount
  • df/du
  • user management
    • chsh
  • Networking
    • Introduction
    • routing table/interface management
    • /etc/hosts, /etc/hosts.allow, /etc/hosts.deny
Powered by GitBook
On this page

find

Basic command overview:

To search for files within a directory hierarchy

$ find <start_directory> [flags]

To search from the current directory, use a period . as the start directory

$ find . [flags]

To search from the root directory (/)

$ find / [flags]

# eg.
$ find / -name flag.txt 

Permission denied error message

Sometimes there may be an error message returned similar to the following:

find: ‘/...’: Permission denied

Simply append 2>/dev/null at the end of the command:

# 2>/dev/null redirects all errors to /dev/null. This essentially 
# flushes all the errors away
$ find ... 2>/dev/null

Useful flags

-name

To find by base of filename, with the leading directories removed. The -path flag discussed below can be used instead if leading directories searches are required

$ find <start-dir> -name [pattern]

# eg. 
$ find . -name 'pattern'
$ find . -name '*pattern*'

-path

The -pathflag allows matching of forward slash characters (/). This can allow searching within sub-directories of folders

$ find <start-dir> -path [pattern]

# eg. 
$ find . -path '*pattern*'

The asterisk (*) in the search pattern for -name and -path flags are used for wildcard matching. To illustrate ...

$ ...

Difference between -name and -path

# Example file directory
user@linux~/$ ls
test-dir test-dir2 

# test-dir2 contains a .txt file (random.txt) - the rest are empty
user@linux~/$ ls test-dir2
random.txt

#
# Example difference between -name and -path flags
#

user@linux~/$ find . -name 'test-dir*'
./test-dir
./test-dir2


user@linux~/$ find . -path '*test-dir*' 
./test-dir
./test-dir2
./test-dir2/random.txt


user@linux~/$ find ~ -name 'test-dir*'
/home/user/test-dir
/home/user/test-dir2


user@linux~/$ find ~ -path '*test-dir*' 
/home/user/test-dir 
/home/user/test-dir2
/home/user/test-dir2/random.txt

-type

To search by file type ...

-perm

To search files by permissions ...

owner-group-users -> rwxrwxrwx -> 111 111 111 -> 777 (?)

excerpt from https://man7.org/linux/man-pages/man1/find.1.html

   Searching files by permissions
       •      Search for files which are executable but not readable.

                  $ find /sbin /usr/sbin -executable \! -readable -print

       •      Search for files which have read and write permission for
              their owner, and group, but which other users can read but
              not write to.

                  $ find . -perm 664

              Files which meet these criteria but have other permissions
              bits set (for example if someone can execute the file)
              will not be matched.

       •      Search for files which have read and write permission for
              their owner and group, and which other users can read,
              without regard to the presence of any extra permission
              bits (for example the executable bit).

                  $ find . -perm -664

              This will match a file which has mode 0777, for example.

PreviouschrootNextlocate

Last updated 2 months ago