Basic Linux File Operations

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn to use some basic operation command that allows you to perform actions such as find files and find string in files.

Achievements

  • find - find files in the specified directory.
  • locate - find eligible files.
  • which - look in the directory set by the environment variable $PATH to find the files that match the conditions.
  • whereis - find eligible files in a specific directory.
  • grep - find the string in the file that matches the criteria.

Find Files with a Certain Extension in Current Directory and Subdirectories

The find command searches for files or directories by name, usually the find command needs paramters for searching.

To facilitate the experiment, the startup script will create new folders and files.

lab-file-location-1-1
lab-file-location-1-2

The find . -name "*.cpp" command find files in the current directory. The following example shows how to find all the cpp files in the current directory and subdirectories.

find . -name "*.cpp"

## or

find . -name '*.cpp'
lab-file-location-1-3

Parameter Description:

  • .: The dot character (.) is a special character in Unix, it indicates the current directory.
  • -name: The -name parameter is used to search for a file or directory by name.
  • *.cpp: The asterisk character (*.cpp) is a special character in Unix, it indicates the file or directory that is .cpp extension.

Find All Files in Current Directory and Subdirectories

The find . -type f command find all files in the current directory and subdirectories. The following example shows how to find all files in the current directory and subdirectories.

find . -type f
lab-file-location-2-1
find . -type d
lab-file-location-2-2

Parameter Description:

  • .: The dot character (.) is a special character in Unix, it indicates the current directory.
  • -type: The -type parameter is used to search for a file or directory by type.
  • f: The f character (f) is a special character in Unix, it indicates a file.
  • d: The d character (d) is a special character in Unix, it indicates a directory.

Locate File with Full Name

The locate command is used to find eligible documents, it will go to the database where the names of documents and directories are stored, to find the documents or directories that meet the conditions of the model style.

Once new file has been created we need to execute the sudo updatedb command to save the new file to the database.

lab-file-location-3-1
lab-file-location-3-2

The locate test.cpp command will search the system for all files named test.cpp, the result is retrieved from the database.

locate test.cpp
lab-file-location-3-3

Locate Files with Regular Formula

The locate *.cpp command will search the system for all files whose name contains *.cpp, the result is retrieved from the database.

locate *.cpp
lab-file-location-4-1
lab-file-location-4-2

Locate Files in the Specified Directory

The locate ~/lab/*.cpp command will search all files in the specified directory, the result is retrieved from the database.

locate ~/lab/*.cpp
lab-file-location-5-1

The which command will look in the directory set by the environment variable $PATH for files that match the criteria.

We usually use this command to search system programs.

The which bash command will search the system file.

which bash

## or

which locate
lab-file-location-6-1

The whiereis command will look in the directory set by the environment variable $PATH for files that match the criteria, this command will display full information about the file.

We usually use this command to search system programs.

The whiereis bash command will search the system file.

whiereis bash

## or

whiereis locate
lab-file-location-7-1

Paramters Description:

  • /bin/bash: The output inforamtion in the screenshot /bin/bash means the name of the program.
  • /etc/bash.bashrc: The output inforamtion in the screenshot /etc/bash.bashrc means the path of the program.
  • /usr/share/man/man1/bash.1.gz: The output inforamtion in the screenshot /usr/share/man/man1/bash.1.gz means the path of the man manual.

Find String in the File

The grep command is used to find files whose contents contain the specified string. If a file is found to match the specified string, the grep command is preset to display the column containing the string.

the grep command is used to find string in a file and show all searched string by line.

grep hello main.cpp
lan-file-lication-8-1

Find String in the Output

The cat main.cpp | grep labex command will search the strings in output and show all searched string by line.

cat main.cpp | grep labex
lab-file-location-9-1

Find Strings in All Files in the Specified Directory and Subdirectories

The grep -rn hello . command will search the strings in all files in the specified directory and subdirectories and show all searched string by line.

The -rn parameter will display the file name and line number of the string.

grep -rn hello .
lab-file-location-10-1

Paramters Descriptions:

  • -rn: Display the file and line number of the string.
  • hello: Dpecify a string.
  • .: Dpecify current directory.

Summary

Congratulations! You have completed the basic operations lab.

In this lab, you learned five basic operations commands find, locate, which, whereis, grep.

These commands that are used frequently, so be sure to keep them in mind.

Keep exploring!

Other Linux Tutorials you may like