https://www.maketecheasier.com/find-a-file-in-linux
The Linux
The Linux
find
command is one of the most important and handy commands in Linux systems. It can, as the name suggests, find files on your Linux PC
based on pretty much whatever conditions and variables you set. You can
find files by permissions, users, groups, file type, date, size and
other possible criteria using the find
command.
The
In this tutorial we will show you how to find files on Linux using various common combinations of search expressions in the command line.
If you want to find a file by name that contains both capital and small letters, run:
If you want to find a file in the root directory, prefix your search with
If you want to find files with the “.txt” extension under the “/home” directory, run:
To find files whose name is “test.txt” under multiple directories like “/home” and “/opt,” run:
To find hidden files in the “/home” directory, run:
To find a single file called “test.txt” and remove it, run:
To find all empty files under the “/opt” directory, run:
To file all empty directories under “/home,” run:
To find all files whose permissions are “777” in the “/home” directory, run:
To find all the files without permission “777,” run:
To find all read only files, run:
To find all executable files, run:
To find all the sticky bit set files whose permissions are “553,” run:
To find all SUID set files, run:
To find all files whose permissions are “777” and change their permissions to “700,” run:
To find all the files under “/opt” which are accessed twenty days earlier, run:
To find all the files under “/opt” which are modified more than thirty days earlier and less than fiffy days after:
To find all the files under “/opt” which are changed in the last two hours, run:
To find all the files under the “/home” directory which are greater than 10MB and less than 50MB, run:
To find all “.mp4” files under the “/home” directory with more than 10MB and delete them using a single command, run:
find
command is available on most Linux distro by default, so you do not have to install a package for it.In this tutorial we will show you how to find files on Linux using various common combinations of search expressions in the command line.
Find Files by Name in Current Directories
The most obvious way of searching for files is by name. To find a file by name in the current directory, run:find . -name photo.png
If you want to find a file by name that contains both capital and small letters, run:
find . -iname photo.png
If you want to find a file in the root directory, prefix your search with
sudo
which will give you all permissions required to do so, and also the ‘/’ symbol which tells Linux to search in the root directory. Finally, the -print
expression displays the directories of your search results. If you were looking for Gzip, you’d type:sudo find / -name gzip -print
Find Files Under Specific Directory
If you want to find files under a specific directory like “/home,” run:find /home -name filename.txt
find /home -name *.txt
find /home /opt -name test.txt
find /home -name ".*"
find /home -type f -name test.txt -exec rm -f {}
find /opt -type f -empty
Find Directories Using Name
If you want to find all directories whose name is “testdir” under the “/home” directory, run:find /home -type d -name testdir
find /home -type d -empty
Find Files with Certain Permissions
Thefind
command can be used to find files with a specific permission using the perm
option.To find all files whose permissions are “777” in the “/home” directory, run:
find /home -type f -perm 0777 -print
find . -type f ! -perm 777
find /home -perm /u=r
find /home -perm /a=x
find /home -perm 1553
find /home -perm /u=s
find /home -type f -perm 0777 -print -exec chmod 700 {} ;
Find Files and Directories Based on Date and Time
To find all the files under “/opt” which are modified twenty days earlier, run:find /opt -mtime 20
find /opt -atime 20
find /opt -mtime +30 -mtime -50
find /opt -cmin -120
Find Files and Directories Based on Size
To find all 10MB files under the “/home” directory, run:find /home -size 10M
find /home -size +10M -size -50M
find /home -type f -name *.mp4 -size +10M -exec rm {} ;
No comments:
Post a Comment