How can I use grep to show just filenames on Linux?

For displaying only the filenames on Linux by using the grep command, we can follow the below-listed methods in the Linux Operating System:

  • Method 1: Using the “grep” Command with “-l” Argument
  • Method 2: Using the “grep” Command with the “find” Utility
  • Method 3: Using the “grep” Command with the “xargs” Utility

So, let’s explore all the above-listed methods one by one with a detailed understanding of the command structure.

Method 1: Using the “grep” Command with “-l” Argument

In this method, we will see the grep command with the “l” argument for the flag. The “l” (files-with-matches) is used to display the files that match the specified pattern or string. Below we have given the command to use grep with the l argumement. Execute the command in the terminal.

Command:

grep -rl "gfg"



  • grep: This is the main command that is used to search any text patterns in files. It takes the argument as various flags and strings that need to be searched in the file.
  • -l: This option or flag stands for “list” and is used along with the grep command to display the list of files that contains the specified pattern.
  • -r: This option tells grep to search recursively. It means that it will search not only in the specified directory but also in all its subdirectories.
  • gfg: This is the custom string or pattern that we want to be searched within the specified file. This string can be a simple text or it can also be a complex regular expression.

Output:

grep with -l flag

In the above output, we can see that we got the list of three files after the recursive search in the Desktop directory of the Linux system. All these files contain the string “gfg“.

Method 2: Using the “grep” Command with the “find” Utility

In this method, we will see the the grep command with the “find” utility or command. The Find command is used to search for files or folders, and also “-exec‘ flag is used that allow us to execute other commands on the files and folders that are found.

Command:

find . -iname "*.txt" -exec grep -l "gfg" {} \;



  • find: This command is used mostly for searching files and directories within the specified starting directory and its sub-directories. We can use various options along with the find command to filter out our results.
  • -iname “*.txt”: This option is used along with the ‘find‘ command to specify a search filter. This option tells the find command to search for the files that end with the “.txt” extension. The “iname” flag is used to make the search case insensitive”.
  • -exec: This option or flag is used to execute another command on each file or directory that matches the filter criteria which is specified in the find command.
  • grep -l file: This command i used to search patterns or text within the files.
  • {}: This is the placeholder that is used with the find command to represent each of the files found during the search. Here, the find replaces {} with the actual file or directory path before executing the command.
  • \;: This is the termination option used to terminate the ‘exec’ command for each of the files.

Output:

grep with the find command

In the above output, we can see that we have got the filename which is of .txt extension, and matches the string that is given as the input in the command.

Method 3: Using the “grep” Command with the “xargs” Utility

In this method, we will be using the “grep” command with the “xargs” command. Here, we are trimming the file path using the basename utility and only printing the filenames in the terminal.

Command:

grep -l gfg ./* | xargs -L 1 basename

  • grep -l gfg./*: This command uses the “grep” to search the string “gfg” in all the files in the current directory and the subdirectories. -l flag is used to list only the filenames.
  • |: This is the pipe symbol used to redirect the output of the previously executed command to the next upcoming command. It just takes the output of one command and uses it as input for another command.
  • xargs -L 1 basename: This command is mainly used to take lines from the standard input and use them as an argument to run another command i.e. basename. basename command mainly extracts the base filenames from the actual full path.

Output:

grep with xargs command

In the above output, we can see that we have got the files that contain the string “gfg” in them. We are searching only for the files that we have stated by using the “l” flag in the command.

How To Show Only Filenames with grep on Linux

In Linux, the ‘grep’ command is one of the most used commands by many administrators to perform the advanced search of text within the files. Grep is one of the important text-search commands in Unix-based operating systems. Along with this, we can also use this command to filter and simultaneously display the list or the directories preset on our system. This command enables us to manipulate or properly organize the files that are based on the names and the file extensions. We can use the grep command to only show the filenames on Linux. In this article, we will see how we can use this grep command to show just filenames on Linux, we will discuss different methods to show filenames on Linux using the grep command.

Syntax of grep command in Linux

grep <OPTIONS> [pattern] [files...]



Similar Reads

How can I use grep to show just filenames on Linux?

For displaying only the filenames on Linux by using the grep command, we can follow the below-listed methods in the Linux Operating System:...

Conclusion

In conclusion, the grep command in Linux is one of the most powerful commands that can help customize the output by applying various search patterns and writing the command with a combination of other commands like find, xargs, etc. We can use the above methods and commands to get the specific files as the output that matches the input pattern. This gives a detailed view of how we can use the grep command to get the filenames on Linux....

Contact Us