[ad_1]
Every thing in Linux is saved in directories, and when writing bash scripts, it’s typically helpful to seek for directories by title. Fortunately, you should use the discover command to recursively search listing names and show matches.
Looking Directories
The discover command is used to look by directories in Linux. By default, it’s absolutely recursive, so it should search by all sub-directories to seek out matches.
Should you use the -type d flag, discover will function in “listing mode,” and solely seek for directories, not matching any recordsdata. You should utilize it alongside -name to seek for directories by title:
discover . -type d -name “search”
This command begins within the present listing however may also search in different directories like ~.
The issue with utilizing -name is it should solely match direct names, that means it should fail until it matches your complete listing title. You should utilize wildcards to resolve this although, and placing a wildcard earlier than and after the search string will match the substring wherever within the listing title. Should you’re together with filenames too, you should use wildcards to match recordsdata ending in a specific extension
discover . -type d -name “*search*”
Nevertheless, this may solely match the listing’s title, and can nonetheless ignore the mum or dad listing. Should you’d prefer to match utilizing your complete file path, you’ll want to make use of the Regex possibility lined under.
discover will print out an inventory of each listing that matches, however you’ll need to watch out to be sure to’re constant in utilizing both absolute or relative paths, as a result of it should have an effect on the ultimate response. Should you use a relative path, just like the interval for “present listing,” the responses will probably be relative. However in case you specify the trail immediately, even when it’s the present listing, the trail will probably be absolute, beginning at root.
discover additionally does extra than simply textual content looking out—it may be used to match recordsdata primarily based on timestamps, file sizes, and different Linux identifiers. It may also be used with -exec to run instructions on every file or listing.
RELATED: Learn how to Use the discover Command in Linux
Looking With Regex
You may also use extra superior filtering with discover, utilizing it with common expressions (Regex) to seek out matches for complicated search queries.
One main advantage of utilizing Regex is that it’s going to match your complete listing, together with the bottom directories main as much as.
You should utilize Regex with -regex rather than -name. It additionally helps to activate sed-compatible Regex utilizing -regextype sed.
discover . -type d -regextype sed -regex “.*one/.*”
On this instance, the regex begins with .*one to match all directories ending in “one.” The interval and wildcard will match any substring main as much as this. Then the ahead slash is escaped with / to match the top of the listing, after which one other wildcard to match any listing title.
Total, this may match any listing whose mum or dad ends with “one,” wherever it’s, even in subdirectories. Regex is highly effective, and also you’ll need to watch out that yours matches precisely what you need it to do—no extra, no much less.
Utilizing grep With discover
Since discover may also output a uncooked listing of directories, it may be piped to different instructions for processing. For instance, grep is used as a textual content search utility, and is fast to make use of on the command line for easy search and highlighting.
discover . -type d | grep foo
grep can be a fully-fledged search utility by itself and can be utilized with instruments like common expressions to boost the looking out. You’ll be able to learn our information to utilizing it to study extra.
RELATED: Learn how to Use the grep Command on Linux
[ad_2]