Locate Files On Linux System

LinuxLinuxBeginner
Practice Now

Introduction

In this challenge, you will learn how to find files on a Linux system using four different commands: find, locate, which, and whereis. Each command has its own set of strengths and weaknesses, and it's important to know which one to use in different situations.

Achievements

  • find: A command-line utility that searches through one or more directory trees of a file system, locating files that match certain criteria.
  • locate: A command that finds files by name quickly, using a pre-built index.
  • which: A command that locates a command in your shell's search path.
  • whereis: A command that locates the binary, source, and manual-page files for a command.

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/CompressionandArchivingGroup(["`Compression and Archiving`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/FunctionsandScopeGroup(["`Functions and Scope`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) linux/BasicSystemCommandsGroup -.-> linux/man("`Manual Access`") linux/CompressionandArchivingGroup -.-> linux/tar("`Archiving`") linux/TextProcessingGroup -.-> linux/grep("`Pattern Searching`") linux/FileandDirectoryManagementGroup -.-> linux/find("`File Searching`") linux/FileandDirectoryManagementGroup -.-> linux/locate("`File Locating`") linux/FileandDirectoryManagementGroup -.-> linux/which("`Command Locating`") linux/FileandDirectoryManagementGroup -.-> linux/whereis("`File/Command Finding`") linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/SystemInformationandMonitoringGroup -.-> linux/service("`Service Managing`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/FunctionsandScopeGroup -.-> shell/scope_vars("`Scope of Variables`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills linux/man -.-> lab-1505{{"`Locate Files On Linux System`"}} linux/tar -.-> lab-1505{{"`Locate Files On Linux System`"}} linux/grep -.-> lab-1505{{"`Locate Files On Linux System`"}} linux/find -.-> lab-1505{{"`Locate Files On Linux System`"}} linux/locate -.-> lab-1505{{"`Locate Files On Linux System`"}} linux/which -.-> lab-1505{{"`Locate Files On Linux System`"}} linux/whereis -.-> lab-1505{{"`Locate Files On Linux System`"}} linux/ls -.-> lab-1505{{"`Locate Files On Linux System`"}} linux/service -.-> lab-1505{{"`Locate Files On Linux System`"}} shell/quoting -.-> lab-1505{{"`Locate Files On Linux System`"}} shell/scope_vars -.-> lab-1505{{"`Locate Files On Linux System`"}} shell/globbing_expansion -.-> lab-1505{{"`Locate Files On Linux System`"}} linux/wildcard -.-> lab-1505{{"`Locate Files On Linux System`"}} end

Using the Find Command

The find command is a powerful tool for locating files on a Linux system. It searches through one or more directory trees of a file system, locating files that match certain criteria.

Target

  • To find all files in the current directory and its subdirectories with the .txt extension.
  • To find all files in the current directory and its subdirectories that are larger than 1 megabyte.
  • To find all files in the current directory and its subdirectories that were modified less than 7 days ago.

Result Example

Finding all files in the current directory and its subdirectories with the .txt extension:

./report.txt
./docx.txt
./subdirectory/d.txt
...

Finding all files in the current directory and its subdirectories that are larger than 1 megabyte:

./clash/clash
./clash/Country.mmdb
./dwm/.git/objects/pack/pack-46f7ed27f3f54357fda8f988dc3cd2de625a4331.pack
./vmware-tools-distrib/lib/icu/icudt44l.dat
./vmware-tools-distrib/lib/lib32/libgdk_pixbuf-2.0.so.0/libgdk_pixbuf-2.0.so.0
./vmware-tools-distrib/lib/lib32/libgio-2.0.so.0/libgio-2.0.so.0
./vmware-tools-distrib/lib/lib32/libglib-2.0.so.0/libglib-2.0.so.0
./vmware-tools-distrib/lib/lib32/libvmtools.so/libvmtools.so
./vmware-tools-distrib/lib/lib32/libgtk-x11-2.0.so.0/libgtk-x11-2.0.so.0
...

Find all files in the current directory and its subdirectories that were modified less than 7 days ago:

.
./c.docx
./report.txt
./a.bat
./docx.txt
./subdirectory
./subdirectory/d.txt
./subdirectory/e.report
...

Requirements

  • You should have a basic understanding of the command line interface.
  • You should have a Linux system with the find command installed.

Using the Locate Command

The locate command is a fast way to find files on a Linux system. It uses a pre-built index of the file system to quickly find files by name.

Target

  • To find all files on the system with the word "report" in the filename.
  • To find all files on the system with the .docx extension.

Result Example

Finding all files on the system with the word "report" in the filename:

/var/lib/dpkg/info/python3-reportlab-accel:amd64.md5sums
/var/lib/dpkg/info/python3-reportlab.list
/var/lib/dpkg/info/python3-reportlab.md5sums
/var/lib/dpkg/info/python3-reportlab.postinst
/var/lib/dpkg/info/python3-reportlab.prerm
/var/lib/systemd/deb-systemd-helper-enabled/apport-autoreport.path.dsh-also
/var/lib/systemd/deb-systemd-helper-enabled/paths.target.wants/apport-autoreport.path
...

Finding all files on the system with the .docx extension:

/etc/systemd/system/paths.target.wants/apport-autoreport.path
/home/sorria/.local/share/omf/.github/ISSUE_TEMPLATE/bug_report.md
/home/sorria/oh-my-fish/.github/ISSUE_TEMPLATE/bug_report.md
/lib/systemd/system/apport-autoreport.path
/lib/systemd/system/apport-autoreport.service
/usr/lib/firefox/crashreporter
/usr/lib/firefox/crashreporter.ini
...

Requirements

  • You should have a basic understanding of the command line interface.
  • You should have a Linux system with the locate command installed.

Using the Which Command

The which command is used to locate a command in your shell's search path.

Target

  • To find the location of the grep command.
  • To find the location of the tar command.
  • To find the location of the ls command.

Result Example

The result of the grep command:

/bin/grep

The result of the tar command:

/bin/tar

The result of the ls command:

/bin/ls

Requirements

  • You should have a basic understanding of the command line interface.
  • You should have a Linux system with the which command installed.

Using the Whereis Command

The whereis command is used to locate the binary, source, and manual-page files for a command.

Target

  • To find the location of the binary, source, and manual-page files for the ls command.
  • To find the location of the binary, source, and manual-page files for the grep command.
  • To find the location of the binary, source, and manual-page files for the tar command.

Result Example

The result of the location of the binary, source, and manual-page files for the ls command:

ls: /bin/ls /usr/share/man/man1/ls.1.gz

The result of the location of the binary, source, and manual-page files for the grep command:

grep: /bin/grep /usr/share/man/man1/grep.1.gz /usr/share/info/grep.info.gz

The result of the location of the binary, source, and manual-page files for the tar command:

tar: /usr/lib/tar /bin/tar /usr/include/tar.h /usr/share/man/man1/tar.1.gz

Requirements

  • You should have a basic understanding of the command line interface.
  • You should have a Linux system with the whereis command installed.

Summary

In this challenge, you learned how to find files on a Linux system using four different commands: find, locate, which, and whereis. Each command has its own strengths and weaknesses, and it's important to know which one to use in different situations.

Other Linux Tutorials you may like