Image

Knowledge base → Finding files on the Linux command line

[Virtual servers] [Shared hosting]
Date of publication: 08.10.2023

There are several options for searching files on the command line, we will look at the most used ones. Please note that in all commands it is important to be case sensitive, i.e. filename.txt and FileName.txt will be 2 different files and also with search expressions inside them.

1. Search by file name

find / -name "filename.txt"

1.1 Search by matching part of a file name

find / -name "*.conf"

1.2 Displaying all files that do not contain .log in the name

find . ! -name "*.mp3"

2. Search by modification date for files that were modified more than 30 days ago

find . -type f -mtime +30

2.1 Find files that changed more than 10 minutes ago, but not more than 30

find . -mmin -30 -mmin +10 -type f

2.2 Find files between specific dates

find . -type f -newerct 2023-10-01 ! -newerct "2023-10-08 23:59:00"

3. Find a file by content, where string-to-find is the search string

find / -type f -exec grep -i -H "string-to-find" {} \;

4. To search for large files, for example larger than 2 gigabytes

find . -mount -type f -size +2G 2>/dev/null

4.1 Search and display the 5 largest files in a directory

du-ahx. | sort -rh | head -5

5. Search inside the file with replacement, where we find old and change to new

sed -i 's/new/old/g' text.txt




No Comments Yet