No matter how determined I was to organize the data, every time I looked for the file, I couldn’t find it anywhere. Sometimes it’s because I can’t remember the file name. But remember the file name but don’t remember where to save it, it’s the same. However, in this article, I will guide you to find files or folders on Linux with the find command.
Install find . command
The find command is defined by POSIX, which creates the open standard by which POSIX systems (including Linux, BSD, and macOS) will be compatible. Simply put, you should be able to install this command as long as you’re running Linux, BSD, or macOS.
However, not all find commands are exactly the same. For example, the GNU find command has features that the BSD or Busybox or Solaris find commands may not have, or have, but work differently. This article uses GNU find from the package findutils because it is available and quite popular. Most of the commands presented in this article work with other implementations of find, but if you try the find command on a non-Linux platform and get unexpected results, try downloading and installing the find command. set GNU version.
Find files by name
You can find files by name by providing the full filename or parts of the filename using regular expressions. The find command asks for the path to the directory you want to search, there are also other options like -name for the case-sensitive filename and then the search string. By default, the find command looks for filenames that are exactly the string you enter between double quotes unless you use the egular expression syntax.
Let’s say your Documents folder contains four files: Foo, foo, foobar.txt, and foo.xml. Here’s how to find the file named “foo”:
$ find ~ -name "foo" /home/tux/Documents/examples/foo
You can expand your search by being case sensitive with the -iname option:
$ find ~ -iname "foo" /home/tux/Documents/examples/foo /home/tux/Documents/examples/Foo
Find files by wildcard
You can use basic shell wildcards to expand your search. Example: asterisk
$ find ~ -iname "foo*" /home/tux/Documents/examples/foo /home/tux/Documents/examples/Foo /home/tux/Documents/examples/foo.xml /home/tux/Documents/examples/foobar.txt
represent any number or character:
$ find ~ -iname "foo*.???" /home/tux/Documents/examples/foo.xml /home/tux/Documents/examples/foobar.txt
The question mark (?) represents a character:
This is not the syntax of a regular expression, so the dot (.) represents a literal dot.
Find files with Regular expressions
$ find ~ -iregex ".*foo" /home/tux/Documents/examples/foo /home/tux/Documents/examples/Foo
You can also use regular expressions. As with -iname and -name, are case-sensitive and case-insensitive options. However, unlike the -name and -iname options, the -regex and -iregex options are applied to the entire path, not just the filename. That means if you search for foo you will get no results because foo doesn’t match /home/tux/Documents/foo. You have to search the entire path instead, or use wildcards:
Find files modified in the last week
$ find ~ -mtime -7 /home/tux/Documents/examples/foo /home/tux/Documents/examples/Foo /home/tux/Documents/examples/foo.xml /home/tux/Documents/examples/foobar.txt
To find the file you last modified last week, use the -mtime option along with the number of (negative) days in the past:
Find files that have been modified within a few days
$ find ~ -mtime +1 -mtime -7
You can combine the -mtime options to find files in a date range. For the first -mtime option, provide the most recent number of days on which you may have modified the file, and for the second option, provide the largest number of days. For example, the command below searches for files that have been modified more than one day in the past, but not more than seven:
Limit search by file type
dYou can optimize search results by specifying the file type you are looking for. You shouldn’t use this option if you’re not sure what you’re looking for, but if you know you’re looking for a file and not a folder, or a folder but not a file, it can be an option. great choice. The option is -type , and its arguments are wildcards for several different data types. The most popular are:
s– symbolic link
b– named pipe (used for FIFO)
– special block (usually hard drive symbol)
$ find ~ -type d -name "Doc*" /home/tux/Documents $ find ~ -type f -name "Doc*" /home/tux/Downloads/10th-Doctor.gif $ find /dev -type b -name "sda*" /dev/sda /dev/sda1
Here are some examples:
$ find /usr -iname "*xml" | wc -l 15588 $ find /usr -maxdepth 2 -iname "*xml" | wc -l 15
The find command is recursive by default, which means it searches for results in subdirectories. This can take time for a large filesystem, but you can use the -maxdepth option to control how deep the recursion is.
$ find /usr -mindepth 8 -iname "*xml" | wc -l 9255
You can alternately set the minimum recursion depth with -mindepth: In addition, you can also see 10 more dangerous Linux commands that absolutely should not be tried