Friday, July 4, 2014

Unix Shell: Find Files(1)

1. locate
terminal:
1) locate command list all files whose name and path includes the given strnig as substring
2) User tries to use * to represent any characters, but unfortunately, in this implementation, locate just take * literally
3) It still take * literally even if using the double quote
4) It still take * literally even if using the single quote
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ locate gcc-4.9  
 /usr/share/doc/gcc-4.9-base  
 /usr/share/doc/gcc-4.9-base/README.Debian.i386.gz  
 /usr/share/doc/gcc-4.9-base/TODO.Debian  
 /usr/share/doc/gcc-4.9-base/changelog.Debian.gz  
 /usr/share/doc/gcc-4.9-base/copyright  
 /var/lib/dpkg/info/gcc-4.9-base:i386.list  
 /var/lib/dpkg/info/gcc-4.9-base:i386.md5sums  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ locate gcc-4.9*  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ locate "gcc-4.9*"  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ locate 'gcc-4.9*'  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$   

locate doesn't scan through the entire file system in the real time, which is very time consuming. Instead, it extracts information from a compressed database, in current system, it is: /var/lib/mlocate/mlocate.db.

This compressed database is updated by tool "updatedb", which is controlled by cron. And cron is one daemon process who is operating crontab file time by time to do scheduled job. Crontab file has one special format to do the scheduled job.
System admin has one own crontab file and only admin can access that.

2. type: find where are commands
type is internal shell command, so it could identify aliases and functions
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ type ls  
 ls is aliased to `ls --color=auto'  

type can indicate the location of daily used task
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ type mkdir  
 mkdir is /bin/mkdir  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ type rm  
 rm is /bin/rm  

type search the command based on the "PATH" environment variable.
1) Searching script_1 fails, since "script_1" doesn't exist at PATH place. Instead, it exists at the local place.
2) Printing out the PATH variable, it doesn't include the local place
3) Add the local place to PATH variable
4) Printing out the PATH variable, it includes the local place now.
5) Searching script_1 succeeds, since PATH already includes local folder.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ type script_1  
 bash: type: script_1: not found  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $PATH  
 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ PATH=$PATH:~/Desktop/xxdev  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ echo $PATH  
 /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/aubinxia/Desktop/xxdev  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ type script_1  
 script_1 is /home/aubinxia/Desktop/xxdev/script_1  

3. Find Command Basic:
terminal:
1) - 4): We create 3 temporary files in local directory and create another directory called dir, inside dir, we create dir_one and dir_two temporary files.
5) Use command to find files in local directory.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ touch one two three  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ mkdir dir  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ touch ./dir/dir_one  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ touch ./dir/dir_two  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find *  
 dir  
 dir/dir_one  
 dir/dir_two  
 one  
 three  
 two  

Find command will search from current place and dig into further directories recursively.

Find command won't sort the result normally we have to use pipe and sort to further process the result.

4. Find Command: -ls
We can use -ls option to get more detailed information of searching result:
terminal:
1) First command is using ls option to get more information
2) We use pipe to transfer the output to sort command, sorting based on 9th column.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find * -ls  
 920592  4 drwxrwxr-x  2 aubinxia aubinxia   4096 Jul 4 11:26 dir  
 920593  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 4 11:26 dir/dir_one  
 920594  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 4 11:26 dir/dir_two  
 131137  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 4 11:25 one  
 134293  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 4 11:25 three  
 134271  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 4 11:25 two  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ find * -ls | sort -k9  
 131137  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 4 11:25 one  
 134293  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 4 11:25 three  
 134271  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 4 11:25 two  
 920592  4 drwxrwxr-x  2 aubinxia aubinxia   4096 Jul 4 11:26 dir  
 920593  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 4 11:26 dir/dir_one  
 920594  0 -rw-rw-r--  1 aubinxia aubinxia    0 Jul 4 11:26 dir/dir_two  

No comments:

Post a Comment