• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Friday, May 9, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

Five File Searching Commands On Linux

How VPS by How VPS
January 1, 2020
in Linux
0
0
SHARES
15
VIEWS
Share on FacebookShare on Twitter

Contents

  1. Introduction
  2. whereis
  3. locate
  4. which
  5. type
  6. find
  7. Want to contribute?


Introduction

There are five file searching commands on Linux: whereis, locate, which, type, and find. Each of them have their own characteristics and is designed for particular search scenarios. This guide will introduce you to each command and show example usages. For further reading on any of these commands, it is best to review the appropriate manpage.

whereis

The whereis command is used to search binary files, source code, and the online manual pages at several standard install directories for any program name specified.

Because whereis does not search every location on your system, any files out of these specific directories will not be found. For the same reason, you will get your search result quickly, whether found or not.

Also, it will not search for those commands which are built directly into the shell.

For example, if you want to find info about the ls command, run the following command on your terminal:

whereis ls

You will get some feedback like:

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

In the result, /bin/ls is the binary you want to locate, the other two are manpages for the ls program, no source code for ls program was found.

Copy the /bin/ls file to your home directory and to the /usr/bin directory, and then run the whereis command again:

cp /bin/ls ~
cp /bin/ls /usr/bin/ls
whereis ls

As you see, only /bin/ls and /usr/bin/ls were found in the search result. The ls program in your home directory was not found, because your home directory is not one of those standard install directories.

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

If you run the following command:

whereis cd

You will see output similar to the line below. There was no binary file was discovered because cd is a built-in command of the shell.

cd: /usr/share/man/man1/cd.1.gz /usr/share/man/man1p/cd.1p.gz

locate

The locate command is used to find files by name with the help of a database (/var/lib/mlocate/mlocate.db). This database is basically a snapshot of the layout of your filesystem. Any records in this database that match your query will be listed in the search result. By default, the locate command will use your query to match any part of the whole name of each record, including the path name. Therefore, the number of matched files in the search result could be more than you expected.

As the database becomes outdated, the search result of the locate command becomes less accurate. You may notice files that no longer exist, or you won’t see matches for newly-created files. By default, the mlocate.db database is automatically updated once a day. You can use the updatedb command to manually update it.

Because the search is performed on the database instead of the filesystem, the search speed is very fast.

If you run the following command:

locate ls

You will get many records instead of your expected result.

In order to make the search result more accurate, you can use the -b flag to restrict the search range, using your query to match only the basename of each record:

locate -b "/ls"

This time, you will get the location of each file exactly called ls on your filesystem:

/bin/ls

which

The which command will search for the command you specified in the value of environment variable PATH and return the first result by default. If you are querying an alias, the which command will determine the actual command for the alias before performing your search.

The which command is also very fast because of its simplicity.

The usage of the which command is simple:

which your_command

If you want to show all of the matched commands instead of the first one, use the -a flag:

which -a your_command

Here is an example for searching for a command that has been aliased:

which ll

The search result would be:

alias ll='ls -l --color=auto'
    /bin/ls

type

By default, the type command will indicate how a command name would be interpreted. Possible results include an alias, a keyword, a function, a builtin, or a file. Just like the whereis command, the type command will only search in several standard install directories to answer your query.

Some examples for typing different commands:

A shell builtin command:

type cd

cd is a shell builtin

A binary file on the filesystem:

type sudo

sudo is /usr/bin/sudo

An alias:

type ls

ls is aliased to `ls --color=auto'

find

Among the five file searching commands introduced in this tutorial, the find command is the most powerful one. It is also the slowest one. Unlike the other four commands, the find command will actually search for your file on the entire filesystem, one i-node by one i-node. With the find command, you can use sophisticated query criteria to find every file you need, even additionally execute actions on the files that were found.

Search criteria for the find command is too sophisticated to explain in such a short article, here are a few examples instead.

Basic format of the find command:

find [path] [option] [action]

To find all files in the working directory and all of its sub-directories:

find

To find a file called aaa in your home directory and all of its sub-directories:

find ~ -name 'aaa'

To find all of the files in the filesystem that were modified in the last 24 hours:

find / -mtime 0

To find all of the files in the web directory and all of its sub-directories that belong to user nginx:

find /usr/share/nginx/html/ -user nginx

To find all of the files in the working directory whose permissions are 0744:

find -perm -0744

To find a file with the name aaa in the working directory and list its detailed info:

find -name 'aaa' -exec ls -l {} /;

Want to contribute?

You could earn up to $300 by adding new articles

Submit your article
Suggest an update
Request an article
Previous Post

How To Upgrade Your Server From Ubuntu 14.04 To Ubuntu 15.04

Next Post

How to Install Review Board on Ubuntu 16.10

Next Post

How to Install Review Board on Ubuntu 16.10

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet