Find the directories and files in the current directory that contain a certain string

-3

Show on screen all the directories, only directories, of the current directory that contain in their name the string "pra".

I have this:

find . -type d 

Show on screen all the files, only files, of the current directory that contain in their name the string "pra".

I have this:

find . -type f 

But it's wrong because I get an infinite loop. Can someone help me with them?

    
asked by Fernando 25.09.2017 в 20:29
source

3 answers

5

Directories:

find . -maxdepth 1 -type d -name \*pra\*

Files:

find . -maxdepth 1 -type f -name \*pra\*

-maxdepth indicates how many levels of directories will be down . 1 equals do not enter subdirectories .

-maxdepth X is an option ; must go before of the expressions.

So that the shell does not expand the possible wildcards that you use, you have 2 options:

  • Place \ before of wildcard characters ( -name \*pra\* )

  • Use single quotes ' ( -name '*pra*' )

  • Thanks to @fedorqui for reminding me that the quotes exist: -)

        
    answered by 25.09.2017 / 20:52
    source
    3

    Solutions with find are practical and correct.

    By completeness, we can perform this task also with the expansion parameters:

    for archivo in *pra*
    do
       [ -f "$archivo" ] && echo "$archivo"
    done
    

    By parts:

    • for archivo in *pra* : expands in a list of elements whose name contains "pra". As these elements can be files, directories, links ... it is necessary to add the following validation:
    • [ -f "$archivo" ] && echo "$archivo" that looks if it is a file, in which case it writes its value per screen.
    answered by 26.09.2017 в 15:12
    2

    Try with:

    find . -type f -name 'pra*'
    

    The -name option allows you to specify the name of the file or directory that is searched for. The simplest syntax is like that of the shell and in this case, you have to put the term to search in inverted commas so that the shell does not expand what you want to search and what interpret literally Double quotes would also work.

        
    answered by 25.09.2017 в 20:53