Find a file in windows is very easy just go in
Searching files in Windows are easy, Just go to the search box and type your query (name of the file), and hit enter, you will get the result of all files with the name. it happens only in the graphical user interface.
The GUI (Graphical User Interface) is not available in the Linux operating system every time, but the command line interface is available always.
To find a file by name, size, type, etc in the graphical interface is quite easy, And it is not really so easy in CLI (Command Line Interface).
In this article, I am going to cover “How to find a file in Linux by using CLI mode.
When you read the full article you will be expert to find the files in Linux by using the command line interface.
And you will hate GUI for the same work.
The find is one of the most powerful commands in Linux in the system administrator’s point of view.
Find is a command line utility that allows you to search for files and directories in a directory hierarchy based on user-given expression and applies user-specified action on each matched file.
Course Content
- Find a File in Linux by Type
- Find a File in Linux by Name
- Find a File in Linux by Extension
- Find a File in Linux by Size
- Find a File in Linux by Modification Date
- Find a File in Linux by Permissions
- Find a File in Linux by Owner
- Find a Delete File in Linux
- Video: How to Find a File in Linux
- Conclusion
Find Command Syntax in Linux
If you want to be an expert in using commands You must know what is the basic syntax of the same command.
Review the basic syntax of find command before go “How to use find command in Linux or how to find a file in Linux by using find command.
The basic usage of find utility expressions take the following form:
Usage: find [-H] [-L] [-P] [-Olevel] [-D debugopts] [path…] [expression]
- The options attribute [-H] [-L] [-P] [-Olevel] [-D debugopts] controls the treatment of the symbolic links, debugging options and optimization method.
- The [path… ]an attribute defines the path where find will look for the file. If you want to search in complete system use only / or if you want to search file in etc directory path would be /etc/
- The [expression] attribute is made up of options, search patterns and actions separated by operators.
Let’s take a look at the following example:
$find -L /etc/ -name passwd
- Options is -L which allow to follow symbolic links.
- Path: /etc/ which allow to search entire directory as per path.
- Expression: -name which allow to search with given name
vijay@Ubuntu-19:~$ find -L /etc -name passwd /etc/passwd find: ‘/etc/cups/ssl’: Permission denied find: ‘/etc/ssl/private’: Permission denied /etc/pam.d/passwd /etc/cron.daily/passwd find: ‘/etc/polkit-1/localauthority’: Permission denied vijay@Ubuntu-19:~$
vijay@Ubuntu-19:~$ find -L /home/ -name vijay /home/vijay vijay@Ubuntu-19:~$
Find a File in Linux by Type
As you know, different files have different types, regular file, directory and more. you can use -type expression to specify file type.
If you want to search file by type then you can use the following expressions:
- f: a regular file
- d: directory
- l: symbolic link
- c: character devices
- b: block devices
- p: named pipe (FIFO)
- s: socket
Specifying path is really important because if you don’t define path it will search only in current directory.
If you want to search from your current directory then syntax will as following
$find . -type d
From specific directory you must give the right path
$find /etc/ -type d -name passwd
vijay@Ubuntu-19:~$ find /etc -type d -name passwd find: ‘/etc/cups/ssl’: Permission denied find: ‘/etc/ssl/private’: Permission denied find: ‘/etc/polkit-1/localauthority’: Permission denied vijay@Ubuntu-19:~$
vijay@Ubuntu-19:~$ find /etc -type f -name passwd /etc/passwd find: ‘/etc/cups/ssl’: Permission denied find: ‘/etc/ssl/private’: Permission denied /etc/pam.d/passwd /etc/cron.daily/passwd find: ‘/etc/polkit-1/localauthority’: Permission denied vijay@Ubuntu-19:~$
Find a File in Linux by Name
You know the file name but forget where is it? you
The
To search for a file called vijay.txt in the /home/
vijay@Ubuntu-19:~$ find /home -type f -name vijay.txt /home/vijay/vijay.txt vijay@Ubuntu-19:~$
If you are searching a case sensitive file, use -iname instead of -name. Example given below, look and compare the result.
vijay@Ubuntu-19:~$ find /home -type f -name VIJAY.txt vijay@Ubuntu-19:~$
vijay@Ubuntu-19:~$ find /home -type f -iname VIJAY.txt /home/vijay/vijay.txt vijay@Ubuntu-19:~$
Find a File in Linux by Extension
I want to have a look over all files with .log.gz extension files in my home directory. I can search file with extension.
The syntax would we following
vijay@Ubuntu-19:~$ sudo find /var/log -type f -name *.log /var/log/alternatives.log /var/log/dpkg.log /var/log/bootstrap.log /var/log/fontconfig.log /var/log/gpu-manager.log /var/log/auth.log /var/log/unattended-upgrades/unattended-upgrades-shutdown.log /var/log/boot.log /var/log/installer/casper.log /var/log/kern.log /var/log/apt/term.log /var/log/apt/history.log vijay@Ubuntu-19:~$
vijay@Ubuntu-19:~$ sudo find /var/log -type f -name *.gz /var/log/dmesg.3.gz /var/log/dmesg.2.gz /var/log/dmesg.1.gz /var/log/installer/initial-status.gz vijay@Ubuntu-19:~$
Find a File in Linux by Size
You can use -size option to search file based on size. After -size you must define size criteria.
Following suffix can be used to define file size
- b: 512-byte blocks (default)
- c: bytes
- w: two-byte words
- k: Kilobytes
- M: Megabytes
- G: Gigabytes
To find the file with accurate size, you can give the size after using -size option. For
You can also search
Find a File in Linux by Modification Date
The find command also allows searching files based on its last modification, access, and change time.
$find /etc/dovecot/conf.d -name “*.conf” -mtime 5
$find /home -mtime +30 -daystart
-amin n: File was last accessed n minutes ago.
–
–
-cmin n : File’s status was last changed n minutes ago.
–
–
Find a File in Linux by Permissions
To filter the files based on the file / directory permissions, use the -perm option followed by permission number.
You can find all files with permissions of exactly 775 inside the /var/www/
$find /var/www/html -perm 775
Two type prefix can be used – or /
When slash / is used as the prefix, then at least one category (user, group or others) must have at least the respective bits set for a file to match.
$find . -perm /444
If minus – is used as the prefix then for the file to match at least the specified bits must be set.
The following command will search for files that have read and write permission for the owner and group and are readable by other users:
find . -perm -664
Find a File in Linux by Owner
The user, who created file in Linux operating system called owner. Linux is a multiple user based operating system.
You are working as a system administrator and want to filter files based on ownership then this method is really very useful.
To search the files in linux system owned by user vijay
Here is a more advanced example, If you want to find all files owned by the user
$find / -user ftp-user -type f -exec chown www-data {} \;
Find and Delete File in Linux
Find and Delete file is considered as two work, one is find the file another is delete the finding files.
If you are SysAdmin and want some delete .temp files then first command (find) will search all .temp files and then -delete option will delete all files.
$find /var/log/ -name ‘*.temp’ -delete
Video: How to Find a File in Linux
Conclusion
Find command is very interesting Linux, you can use it any linux distro like Ubuntu, RHEL, Centos, Kali Linux, Arch Linux, Free BSSD etc.
Expert command over find utility make you advanced user of linux, and find command will help you to work fast.
If you have any question please comment below.
Have a nice day
Cheers!
Source Find a File in Linux
No comments:
Post a Comment