Unit 13 - Finding and Processing Files

From Msamir projects

Jump to: navigation, search

Contents

Introduction

Filesystem Hierarchy Standard (FHS) help system administrator and users locate files more easily, however not solve file searching problem completely we still need accurate method to search files in file system. Linux have very power tools doing this tasks, some tools use pre-built database to search files like locate/slocate and whatis other do real time search in file system like find command each group have advantage and disadvantage.

The first group advantage it's do search in very short time and can be done away form system by copy the database to other system, disadvantage it's accurate as much as how fresh database can be and consider not suitable for files recently created or renamed.

In the other hand for the second group it's have advantage

  • very accurate as it search the directory trees in real-time
  • vast criteria searching file files by name, owner, group, type, permissions, date, and many other criteria. The search is recursive in that it will search all subdirectories too

However disadvantage it's more slow and more resource consuming.

Find command

redhat@RHEL:/tmp/redhat/labs/RH033$ ls -ali
total 22456
188108 drwxr-xr-x 2 redhat redhat     4096 2009-07-09 22:34 .
188106 drwxr-xr-x 3 redhat redhat     4096 2009-07-09 22:32 ..
188115 -rw-r--r-- 1 redhat redhat 10485760 2009-07-09 23:07 lpi
188116 -rw-r--r-- 1 redhat redhat   921600 2009-07-09 23:08 LPI
188120 -rw-r--r-- 1 redhat redhat        0 2009-07-09 22:33 lpi101
188117 -rw-r--r-- 1 redhat redhat        0 2009-07-09 22:32 LPI101
188121 -rw-r--r-- 1 redhat redhat        0 2009-07-09 22:33 lpi102
188118 -rw-r--r-- 1 redhat redhat        0 2009-07-09 22:33 LPI102
188123 -rw-r--r-- 1 redhat redhat        0 2009-07-09 22:33 rh033
188110 -rw-r--r-- 1 redhat redhat     3072 2009-07-09 23:06 rhce
188133 -rw-r--r-- 1 redhat redhat        0 2009-07-09 22:34 rhcE
188130 -rw-r--r-- 1 redhat redhat  1048576 2009-07-09 23:07 rhCe
188131 -rw-r--r-- 1 root    redhat        0 2009-07-09 22:34 Rhce
188114 -rw-r--r-- 1 root    redhat        0 2009-07-09 22:32 RHCE
188127 -rw-r--r-- 1 redhat redhat        0 2009-07-09 22:33 rhct
188128 -rw-r--r-- 1 root    redhat 10485760 2009-07-09 23:02 RHCT

search files by name

redhat@RHEL:~$ find /tmp/redhat -name rhce
/tmp/redhat/labs/RH033/rhce
redhat@RHEL:~$ find /tmp/redhat -iname rhce
/tmp/redhat/labs/RH033/Rhce
/tmp/redhat/labs/RH033/rhCe
/tmp/redhat/labs/RH033/rhce
/tmp/redhat/labs/RH033/RHCE
/tmp/redhat/labs/RH033/rhcE

search files by owner

redhat@RHEL:~$ find /tmp/redhat -user redhat
/tmp/redhat
/tmp/redhat/labs
/tmp/redhat/labs/RH033
/tmp/redhat/labs/RH033/lpi
/tmp/redhat/labs/RH033/LPI
/tmp/redhat/labs/RH033/rhct
/tmp/redhat/labs/RH033/rhCe
/tmp/redhat/labs/RH033/rhce
/tmp/redhat/labs/RH033/rh033
/tmp/redhat/labs/RH033/lpi101
/tmp/redhat/labs/RH033/LPI101
/tmp/redhat/labs/RH033/LPI102
/tmp/redhat/labs/RH033/lpi102
/tmp/redhat/labs/RH033/rhcE
redhat@RHEL:~$ find /tmp/redhat -user root
/tmp/redhat/labs/RH033/Rhce
/tmp/redhat/labs/RH033/RHCE
/tmp/redhat/labs/RH033/RHCT

search files by permissions

search files by size

redhat@RHEL:~$ find /tmp/redhat -size +1024k
/tmp/redhat/labs/RH033/lpi
/tmp/redhat/labs/RH033/RHCT
redhat@RHEL:~$ find /tmp/redhat -size -1024k
/tmp/redhat
/tmp/redhat/labs
/tmp/redhat/labs/RH033
/tmp/redhat/labs/RH033/LPI
/tmp/redhat/labs/RH033/rhct
/tmp/redhat/labs/RH033/Rhce
/tmp/redhat/labs/RH033/rhce
/tmp/redhat/labs/RH033/rh033
/tmp/redhat/labs/RH033/lpi101
/tmp/redhat/labs/RH033/RHCE
/tmp/redhat/labs/RH033/LPI101
/tmp/redhat/labs/RH033/LPI102
/tmp/redhat/labs/RH033/lpi102
/tmp/redhat/labs/RH033/rhcE
redhat@RHEL:~$ find /tmp/redhat -size 4k
/tmp/redhat
/tmp/redhat/labs
/tmp/redhat/labs/RH033

search files by time

access time

file status

modification time

search files by type

redhat@RHEL:~$ find /tmp/redhat -type d
/tmp/redhat
/tmp/redhat/labs
/tmp/redhat/labs/RH033
redhat@RHEL:~$ find /tmp/redhat -type f
/tmp/redhat/labs/RH033/lpi
/tmp/redhat/labs/RH033/LPI
/tmp/redhat/labs/RH033/rhct
/tmp/redhat/labs/RH033/Rhce
/tmp/redhat/labs/RH033/rhCe
/tmp/redhat/labs/RH033/rhce
/tmp/redhat/labs/RH033/rh033
/tmp/redhat/labs/RH033/lpi101
/tmp/redhat/labs/RH033/RHCE
/tmp/redhat/labs/RH033/LPI101
/tmp/redhat/labs/RH033/RHCT
/tmp/redhat/labs/RH033/LPI102
/tmp/redhat/labs/RH033/lpi102
/tmp/redhat/labs/RH033/rhcE

Find logical expressions

Find and -exec

mlocate package

updatedb

[root@server1 ~]# updatedb 
[root@server1 ~]# file /var/lib/mlocate/mlocate.db
/var/lib/mlocate/mlocate.db: data
[root@server1 ~]# ls -ali /var/lib/mlocate/mlocate.db
13717187 -rw-r----- 1 root slocate 1517909 Jul  9 23:43 /var/lib/mlocate/mlocate.db
[root@server1 ~]# updatedb 
[root@server1 ~]# ls -ali /var/lib/mlocate/mlocate.db
13717188 -rw-r----- 1 root slocate 1516155 Jul  9 23:44 /var/lib/mlocate/mlocate.db

locate

[msamir@server1 ~]$ locate hany
/home/hany
/var/spool/mail/hany
[msamir@server1 ~]$ locate msamir
/home/msamir
/home/msamir/.bash_history
/home/msamir/.bash_logout
/home/msamir/.bash_profile
/home/msamir/.bashrc
/home/msamir/Mail
/home/msamir/Mail/.imap
/home/msamir/Mail/.imap/INBOX
/home/msamir/Mail/.imap/INBOX/dovecot.index
/home/msamir/Mail/.imap/INBOX/dovecot.index.cache
/home/msamir/Mail/.imap/INBOX/dovecot.index.log
/var/spool/mail/msamir
« RedHat Linux Essentials
Finding and Processing Files
»
Configuring the Bash Shell Network Clients
Personal tools