Find Files on Linux

28/12/2020
Chưa phân loại

Introduction

Linux is an operating system family which makes use of the Linux kernel. The operating systems under the Linux banner are generally known as distributions, and are often for free of charge. Here in this article it explains how to find files with ease, and how to expand its capability to get more precise details. As the operating system it uses Ubuntu 17.04, but this is the same in pretty much any other Linux operating system depending on its version.

Basic Search

The basic search involves typing the name of the file in “File” manager which by default searches files in the Home directory only, but by navigating to “Other Locations”, it can be commanded to search files in both “Network” and “On this Computer” locations. “On this computer” lists out locally available hard drives to the operating system, whereas “Networks” lists out discovered network locations.

  1. Click on “File” manager.
  2. Navigate to “Other Locations”
  3. Click on either “Computer” or any network location listed in the file manager.
  4. Use the search bar on the “File” manager to start searching the files as in following screenshot.

“Find” Command (Terminal Command)

Find command is a part of findutils directory searching utility, and which searches files by traversing in a folder hierarchy. The process is relatively faster then “File” manager search, and is capable of finding files in real time. Find command uses many parameters to change the behavior of the search, and therefore it’s a powerful tool to get into action.

Basic Syntax

The basic search of “Find” goes as following. It consists of the name parameter which specifies name of the file to be searched, and the location which specifies to where the file to be searched.  “~” symbol indicates the search is made in the “home” directory. Since it’s a basic search the quotation marks are not used, but generally it’s recommended to used them to avoid unexpected results.

             find ‘path’ -name ‘file name’

             find ~ -name readme.txt

             find ~ -name ‘readme.txt’

Search in Current Directory

This is same as the aforesaid one. The only difference here is having “.” Which indicates the current search to be made in “current directory” (denoted by “.”).

             find . -name readme.txt

             find . -name ‘readme.txt’

Search as Administrator

Sometimes when searching in certain protected directories or from root  (“/”) directory the terminal might complain it doesn’t have enough permission to access denoted by “Permission denied” message. In such cases using sudo along with the password of the current user helps to solve the problem. As seen in the following screenshot cd /<directoryname> helps to specify the current directory to use with find.

             cd /etc (to change the current directory)

             sudo find . –name ‘file name’

             sudo find . -name ‘README’

             sudo find . -name README

Search case insensitive words

In an operating system it’s normal for having both uppercase and lowercase file names, but find is unable to detect them by default, and therefore this new parameter –iname which ignores the case of the file’s name has to be used instead of the default –name parameter.

             find /etc –iname ‘readme’

Search by extensions

Extension specifies the type of the file as in whether it’s a text file or system file or any other file. Here the “*” symbol is used to denote one or more characters are considered when making the search. In the following example it uses “*.txt”, and thus any text file is included in the result.

             find /etc -iname ‘*.txt”

Search by size

A file always has a size which indicates how much contents it bears within. Find supports search by file size with –size parameter which supports megabyte denoted by M, kilobytes denoted by k, gigabyte denoted by G sizes. Since size also needs either greater than or lesser than option, it also has to be stated with either + or – respectively.

             find / -iname ‘file name’ –size <Number>k/M/G

Greater than

When greater than is used, files larger than the stated size are filtered. In the following examples files larger than 1 kilobyte are filtered.

             find /etc -iname ‘readme’ –size +1k

Less than

When less than is used, files lesser than the stated size are filtered. In the following examples files lesser than 1 kilobyte are filtered.

             find / -iname ‘readme’ –size -1k

Search File by Date

Search file by date as the name itself implies for searching files based on the time and date. Find supports three parameters access time, change time, and modified time.

Access time

Access time changes when a file is read or processed by any process directly or through a script. In the following example it returns any file with name linux which was not accessed for 1 days.

             find / -name ‘linux* –atime +1

Change time

When the file had its contents updated or when its permission was changed the change time is changed accordingly, and thus using the ctime which denotes change time returns any file which was changed within the given time period. In the following example it returns any file with name ‘readme’ changed for 22 days or more.

             find / -name ‘readme’ –ctime +22

Modify time

Modify time and change time both are almost same, except modify time doesn’t include changes in the file permission. The following example as earlier returns any file with the name readme which was changed for 22 days or more.

             find / -name ‘readme’ –mtime +22

And or OR

And, OR operators join two or more parameters together; hence they are useful for searching multiple files at the same time. In the following example it searches for both ‘linux’ OR ‘readme’ files.

             find / -iname ‘linux’ –or –iname ‘readme’

In the following example it searches for files with the name readme with the size greater than 5 kilobytes

             find / -iname ‘readme’ –and –size +5k

Not indicates the opposite of what had been mentioned. In the following example it indicates not to filter files with names ‘linux’ and ‘log*’ but return any other files with type txt which denotes text files.

             find / -name ‘*.txt’ –not –iname ‘log*’ –not –iname ‘linux’

Prefix

Prefix is useful when a part of a file name is known and it starts with the given keyword, for instance, if there are a large amount of files which start with ‘linux’ in their file names, that particular files can be filtered by using the ‘*linux’

             find / -name ‘*linux’

Suffix

Suffix is useful when a part of a file name is known and it ends with the given keyword, for instance if there are a large amount of files which end with ‘linux’ in their file names, that particular files can be filtered by using the ‘linux*’

As you can see the find command is what you really want to use when you get sophisticated, but you can get started with the Graphical User Interface if you are a Linux newbie.

 

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

[KVM] Sử dụng virt-manager để quản lý VMs

Ở bài viết này mình sẽ chia sẻ cách sử dụng virt-manager – một công cụ quản trị VM đắc...
30/12/2020

MySQL GUI Tools for Ubuntu 18.04

In this lesson, we will look at some most popular GUI tools to access MySQL database on Ubuntu 18.04. These vary in application...
29/12/2020

Vim Awesome

As a text editor, Vim offers a ton of built-in features. In fact, Vim offers as much as everything a “modern” text...
29/12/2020