Working With Files

Key Goals

  • Working with files
  • Working with links
  • Analyzing text files
  • Comparing text and binary files
  • Modifing file content through command line
  • Search for files

Section Objectives

  1. Finding files:
    • which
    • find
    • locate
  2. List file content:
    • less
    • cat
    • head
    • tail
    • wc
  3. Working with files:
    • cp
    • mv
    • rm
    • touch
    • mkdir
  4. Working with links:
    • Hard link
    • Soft link
    • inode
  5. Searching in files:
    • grep
  6. Editing files through the command line:
    • sed
  7. Comparing files
    • diff
    • md5sum

Exercise

Note

Before starting the exercise, install the following packages

yum install -y words # Installs a file that contains a long list of words
yum install -y ntp
yum install -y tree

Finding files

  1. Which binary file executes when running the lsb_release command?
  2. Find the words file
  3. Find the ntp.conf file
  4. Copy all the pdf files located in /usr/share/doc/ to your home directory
  5. Delete all the pdf files located in your home directory
  6. List all the files that are bigger than 10MB inside the /boot directory
  7. List all the files that are bigger than 10MB inside the /boot directory and their sizes

Reading from files

  1. Scenario:
    • Display the top 27 lines of the words file.
    • Display the bottom 30 lines of the words file.
  2. Which flag is needed in order to open a file using tail in a continus mode?
  3. List the number of lines of the words file.

Working with files

  1. Scenario:
    • Copy the /etc/hosts file to your home directory.
    • Copy it again in an interactive mode.
    • Move the file to the /tmp directory.
    • Remove the /tmp/hosts file.
  2. Create the following structure inside your home directory (use wilde cards) and then delete it recursively:
../../_images/dir_tree.jpg

Searching in files

  1. Scenario:
    • Run yum list installed (it prints all the installed packages on your system)
    • Run it again but now grep only the installed packages that their name starts with ‘kernel’.
    • Run it again but now paste the output to a kernel_pack.txt file.
    • Append to the kernel_pack.txt file the following 3 lines (use: ‘echo’ and ‘>>’):
      • newline1
      • newline2
      • newline3
    • Cat the kernel_pack.txt and grep only the newlines.
    • Use only grep (without cat) to select all the lines execpt the ‘newline2’ of the kernel_pack.txt file.
  2. Which alias the grep command is uses by default?
  3. Scenario (note: use the words file):
    • List all the words that start with ‘io’.
    • List all the words that end with ‘ion’.
    • List all the words that start with ‘po’, end with ‘ute’ and contain 7 characters.
    • List all the words that contain five vowels in a row (vowels: a,e,i,o,u).

Editing files through the command line

  1. Scenario:
    • Install the ntp package, run: yum install -y ntp
    • Copy the ntp.conf file to your home directory
    • Delete all blank lines
    • Delete all commented lines

Compering Files

  1. Scenario:
    • Copy the top 10 words of the words file to a new file inside your home directory (name it file1)
    • Copy the top 9 words of the words file to a new file inside your home directory (name it file2)
    • Use diff to compare the files and examin the difference
    • Change the first 2 lines inside file1 to ‘hello’ (using vim)
    • Use diff to compare the files and examine the difference
  2. Explain what is a hash of a file?
  3. In which use cases would you use hash to comapre files instead of the diff command?
  4. Use the md5sum command to calculate the hash of the /etc/hosts file.