Monday, July 20, 2015

Day 21: The Linux Command Line Ch17 Notes


Chapter 17 - Searching for Files

Commands:

locate  - Find files by name.
find - Search for files in a directory hierarchy.

xargs - Build and execute command lines from standard input.
touch - Create/Change file name.
stat - Display file/file system status

Locate utility finds files based on the database stored at:
/var/lib/mlocate/mlocate.db

This database is updated using: 'updatedb' utility.

Find searches a given directory and its subdirectories in order to find the file name. It also supports type of the file (-type f = file, -type -d = directory). For instance:

$ find ~ -type d | wc -l

will find all directories (-type d) in our home directory (~) and will list their number (| wc -l). Find is very powerful. Check for all options in the "The Linux Command Line" (p. 222-224).



Lab 11

  1. Update database locate uses and find all files with 'passwd' name.
  2. Using 'locate' display how many times 'passwd' was found.
  3. In your home directory find all files with ".avi" extenstion and that are 10MB or larger in size.
  4. Find files whose content or attributes were changed in the last 20 minutes.
  5. Find files whose content were changed in the last 2 or less minutes.
  6. Repeat the step 4 and 5 using only one search (hint: group both queries in two logical expressions with an -or)
  7. Which command can follow 'find' with user defined action?
  8. As a bonus follow the instructions as per 'A Return to the Playground' section in the book. It's awesome!
Update database locate uses and find all files with 'passwd' name.
(raspberry pi in my version did not have locate utility installed)

$ sudo apt-get install locate
$ sudo updatedb

Using 'locate' display how many times 'passwd' was found.

$ locate passwd

In your home directory find all files with ".avi" extenstion and that are 10MB or larger in size.

find ~ -type f -name '*.avi' -size +10M

Find files whose content or attributes were changed in the last 2 or less minutes.

$ find ~ -type f -cmin -2

Find files whose content were changed in the last 2 or less minutes.

find ~ -type f -mmin -2

Repeat the step 4 and 5 using only one search (hint: group both queries in two logical expressions with an -or).

find ~ \( -type f -cmin -2 \) -or \( -type f -mmin -2 \)

Which command can follow 'find' with user defined action?

-exec command '{}' ';'
or 
$  | xargs command

for instance:

$ find ~ -name '*.avi' | xargs --null ls -l
Notice!
--null option allows to interpret embedded spaces in file names to be treated as such rather than as space + another command/option

As a bonus follow the instructions as per 'A Return to the Playground' section in the book. It's awesome!


Create 100 directories as per example:

$ cd playground
$ mkdir dir-{00{1..9},0{10..99},100}

Create 100 files in each directory as per example:

dir-{00{1..9},0{10..99},100}/file-{A..Z}

Confirm that 100 file-A have been created (if you are in 'playground directory type the following):

find . -name 'file-A' | wc -l

Create file 'timestamp' for our reference:

$ cd ~
$ touch playground/timestamp
$ stat playground/timestamp

Change times on the file timestamp

$ touch playground/timestamp
$ stat playground/timestamp

Let's update some of the files:

find playground -type f -name 'file-B' -exec touch '{}' ';'

and find newer than timestamp:

find playground -type f -newer playground/timestamp

Finally, let’s go back to the bad permissions test we performed earlier and apply it to playground:

$ find playground \( -type f -not -perm 0600 \) -or \( -type d -not -perm 0700 \)