Linux grep file by name

Содержание
  1. How can I «grep» for a filename instead of the contents of a file? [closed]
  2. 10 Answers 10
  3. Grep In a Directory
  4. [#grep-current-directory][.inline-code]grep[.inline-code] search current directory[#grep-current-directory]
  5. [#grep-current-directory-and-subdirectories][.inline-code]grep[.inline-code] search current directory and subdirectories[#grep-current-directory-and-subdirectories]
  6. [#grep-r-vs-R][.inline-code]grep -r[.inline-code] is different from [.inline-code]grep -R[.inline-code][#grep-r-vs-R]
  7. [#grep-specific-directory][.inline-code]grep[.inline-code] search specific directory[#grep-specific-directory]
  8. [#grep-multiple-files][.inline-code]grep[.inline-code] across multiple files[#grep-multiple-files]
  9. [#grep-all-files][.inline-code]grep[.inline-code] search all files[#grep-all-files]
  10. [#grep-modifiers-multiple-files]Modifiers to the [.inline-code]grep[.inline-code] command across multiple files[#grep-modifiers-multiple-files]
  11. [#grep-exclude-directories][.inline-code]grep[.inline-code] exclude directories[#grep-exclude-directories]
  12. [#grep-counting-multiple-files][.inline-code]grep[.inline-code] counting across multiple files[#grep-counting-multiple-files]
  13. [#grep-ignore-case][.inline-code]grep[.inline-code] ignore case[#grep-ignore-case]
  14. [#grep-return-line-number][.inline-code]grep[.inline-code] return line number[#grep-return-line-number]
  15. [#grep-show-file-name][.inline-code]grep[.inline-code] show file name[#grep-show-file-name]
  16. [#more-on-grep]Find out more about [.inline-code]grep[.inline-code][#more-on-grep]

How can I «grep» for a filename instead of the contents of a file? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

grep is used to search within a file to see if any line matches a given regular expression. However, I have this situation — I want to write a regular expression that will match the filename itself (and not the contents of the file). I will run this from the system’s root directory, to find all those files that match the regular expression. For example, if I want to find all Visual Basic form files that start with an «f» and end with .frm, I’ll use the regular expression —

Not a duplicate (controlling the output from grep (independent of the matching, etc.)): How can I use grep to show just filenames on Linux?

@PeterMortensen the irony is by closing this and commenting you successfully put this into the first page of google results for this kind of question.

10 Answers 10

You need to use find instead of grep in this case.

Читайте также:  Clonezilla linux to iso

You can also use find in combination with grep or egrep :

@alpha_989, I said grep supports it, which it does: gnu.org/software/grep/manual/html_node/… point is piping to grep is only needed if you need pcre. whereswalden has the right answer

@RM: No, that still searches file contents. It just prints file names instead of printing the matching lines.

From manual:

Base of file name (the path with the leading directories removed) matches shell pattern pattern. Because the leading directories are removed, the file names considered for a match with -name will never include a slash, so «-name a/b» will never match anything (you probably need to use -path instead). The metacharacters («*», «?», and «[]») match a «.» at the start of the base name (this is a change in find‐ utils-4.2.2; see section STANDARDS CONFORMANCE below). To ignore a directory and the files under it, use -prune; see an example in the description of -path. Braces are not recognised as being special, despite the fact that some shells including Bash imbue braces with a special meaning in shell patterns. The filename matching is performed with the use of the fnmatch(3) library function. Don’t forget to enclose the pattern in quotes in order to protect it from expansion by the shell.

Источник

Grep In a Directory

[#grep-current-directory][.inline-code]grep[.inline-code] search current directory[#grep-current-directory]

When you want to search in all the files of the current directory, regardless of their name or extension, you can use the wildcard character after your [.inline-code]grep[.inline-code] command as follows:

Note that this command does not search in subdirectories. Instead, it tells you that [.inline-code]model1[.inline-code] and [.inline-code]model2[.inline-code] are directories and that they are not searched.

[#grep-current-directory-and-subdirectories][.inline-code]grep[.inline-code] search current directory and subdirectories[#grep-current-directory-and-subdirectories]

To search within the current directory and all subdirectories, you can use the recursive [.inline-code]-r[.inline-code] flag as follows:

Читайте также:  Install yum debian linux

As you can see, instead of stating that [.inline-code]model1[.inline-code] and [.inline-code]model2[.inline-code] are subdirectories, they have been searched and two results have appeared in files within each.

[#grep-r-vs-R][.inline-code]grep -r[.inline-code] is different from [.inline-code]grep -R[.inline-code][#grep-r-vs-R]

Note that the [.inline-code]-r[.inline-code] flag is slightly different from the [.inline-code]-R[.inline-code] flag. While [.inline-code]-r[.inline-code] searches all files that are present in the current directory and all subdirectories, [.inline-code]-R[.inline-code] will also follow symbolic links to go to the original file.

[#grep-specific-directory][.inline-code]grep[.inline-code] search specific directory[#grep-specific-directory]

If you want to recursively search a specific directory instead of the current one, you can replace the wildcard character with the name of the directory you want to search:

[#grep-multiple-files][.inline-code]grep[.inline-code] across multiple files[#grep-multiple-files]

If you are wanting to match a search string across multiple files that you know the name of, the [.inline-code]grep[.inline-code] command takes the form:

After the string you want to match, you specify the individual files separated by a space character.

If you don’t know the actual file names but instead you know the file extension, you can use the wildcard character ([.inline-code]*[.inline-code]) to specify all files with the given extension. This command would take the form:

Which searchers all files in the current directory with the given file extension. Note that this could also be used to search all files with the same name but different extensions by changing where the wildcard character appears.

[#grep-all-files][.inline-code]grep[.inline-code] search all files[#grep-all-files]

To search all files, you can run the commands identified above but from the root of your system. This is not recommended as you would get the results from folders that aren’t relevant to your search, such as your configuration settings. Instead, navigate to the root of where would be useful to search, such as [.inline-code]/home[.inline-code] or [.inline-code]/usr[.inline-code] or [.inline-code]/etc[.inline-code] and then run the grep command with the recursive search flag ([.inline-code]-r[.inline-code]).

Читайте также:  Ftp services in linux

[#grep-modifiers-multiple-files]Modifiers to the [.inline-code]grep[.inline-code] command across multiple files[#grep-modifiers-multiple-files]

[#grep-exclude-directories][.inline-code]grep[.inline-code] exclude directories[#grep-exclude-directories]

In some cases you may want to exclude certain directories from your search. In this case you add the [.inline-code]—exclude-dir[.inline-code] flag to the command:

This can also be modified to take a list of directories by replacing [.inline-code]dir[.inline-code] with [.inline-code][.inline-code].

[#grep-counting-multiple-files][.inline-code]grep[.inline-code] counting across multiple files[#grep-counting-multiple-files]

To match across multiple files and count the occurrences of the pattern you are searching for, you can use the following command, which will print out the occurrences as well as the file they appear in:

 $ grep -0 “string”  | cut -d ‘:’ -f 1 | uniq -c

[#grep-ignore-case][.inline-code]grep[.inline-code] ignore case[#grep-ignore-case]

The [.inline-code]grep[.inline-code] command is case sensitive. To ignore the case you can use the [.inline-code]-i[.inline-code] flag. For example, when searching for the [.inline-code]hello[.inline-code] string, the [.inline-code]grep[.inline-code] command will also match [.inline-code]Hello[.inline-code] and [.inline-code]hellO[.inline-code] within a file.

[#grep-return-line-number][.inline-code]grep[.inline-code] return line number[#grep-return-line-number]

When matching across multiple files you might want to print off the line number along with the output. In that case you can add the [.inline-code]-n[.inline-code] flag to your command.

[#grep-show-file-name][.inline-code]grep[.inline-code] show file name[#grep-show-file-name]

In some cases, given that a lot of files will be searched which may contain multiple matches, you only want to print out the file name. For this you can add the [.inline-code]-l[.inline-code] flag which stands for “show the file name, not the result itself”.

[#more-on-grep]Find out more about [.inline-code]grep[.inline-code][#more-on-grep]

As always if you want to find out more about how to use the [.inline-code]grep[.inline-code] tool you can use:

Which will print out all the options with explanations. Or:

Which will print out a short page of all the available options.

Источник

Оцените статью
Adblock
detector