Вопрос: Как я могу рекурсивно найти все файлы в текущих и подпапках на основе подстановочных знаков?


Как я могу рекурсивно найти все файлы в текущих и подпапках на основе подстановочных знаков?


1168


источник


Ответы:


использование найти для этого:

find . -name "foo*"

findтребуется начальная точка, а .(точка) указывает на текущий каталог.


1735



Трубопроводы в grep часто более удобны; он дает вам полную силу регулярных выражений для произвольного сопоставления подстановочных знаков.

Например, чтобы найти все файлы с регистрозависимой строкой «foo» в имени файла:

~$ find . -print | grep -i foo

159



findнайдет все файлы, соответствующие шаблону:

find . -name "*foo"

Однако, если вы хотите изображение:

tree -P "*foo"

Надеюсь это поможет!


101



find -L . -name "foo*"

In a few cases, I have needed the -L parameter to handle symbolic directory links. By default symbolic links are ignored. In those cases it was quite confusing as I would change directory to a sub-directory and see the file matching the pattern but find would not return the filename. Using -L solves that issue. The symbolic link options for find are -P -L -H


15



If you shell supports a new globbing option (enable it by: shopt -s globstar), you can use:

echo **/*foo*

to find any files or folders recursively. This is supported by Bash 4, zsh and similar shells.


Personally I've got this shell function defined:

f() { find . -name "*$1*"; }

Note: Above line can be pasted directly to shell or added into your user's ~/.bashrc file.

Then I can look for any files by typing:

f some_name

Although it's almost the same as using fd utility.


15



find <directory_path>  -type f -name "<wildcard-match>"

In the wildcard-match you can provide the string you wish to match e.g. *.c (for all c files)


13



You can use:

# find . -type f  -name 'text_for_search'

If you want use REGX use -iname

# find . -type f  -iname 'text_for_search'

6



Default way to search for recursive file, and available in most cases is

find . -name "filepattern"

It starts recursive traversing for filename or pattern from within current directory where you are positioned. With find command, you can use wildcards, and various switches, to see full list of options, type

man find

or if man pages aren't available at your system

find --help

However, there are more modern and faster tools then find, which are traversing your whole filesystem and indexing your files, one such common tool is locate or slocate/mlocate, you should check manual of your OS on how to install it, and once it's installed it needs to initiate database, if install script don't do it for you, it can be done manually by typing

sudo updatedb

And, to use it to look for some particular file type

locate filename

Or, to look for filename or patter from within current directory, you can type:

 pwd | xargs -n 1 -I {} locate "filepattern"

It will look through its database of files and quickly print out path names that match pattern that you have typed. To see full list of locate's options, type: locate --help or man locate

Additionally you can configure locate to update it's database on scheduled times via cron job, so sample cron which updates db at 1AM would look like:

0 1 * * * updatedb

These cron jobs need to be configured by root, since updatedb needs root privilege to traverse whole filesystem.


4