This particular use of the grep command doesn’t make much sense unless you use it with the -l (lowercase "L") argument as well.Note that in this case, quotes are required around my search pattern. Since I tend to mark comments in my code with my initials ("aja") or my name ("alvin"), this recursive egrep command shows how to search for those two patterns, again in a case-insensitive manner: You can also perform recursive searches with the egrep command, which lets you search for multiple patterns at one time. In this example, the search is made case-insensitive by adding the -i argument to the grep command. This next example shows how to recursively search two unrelated directories for the case-insensitive string "alvin": Your recursive grep searches don’t have to be limited to just the current directory. If you haven’t used commands like these before, to demonstrate the results of this search, in a PHP project directory I’m working in right now, this command returns a list of files like this: As you’ll see below, you can also add -i for case-insensitive searches.The -l option (lowercase letter L) says “list only filenames”.The -r option says “do a recursive search”.However, I was just reminded that a much easier way to perform the same recursive search is with the -r flag of the grep command:Īs you can see, this is a much shorter command, and it performs the same recursive search as the longer command, specifically: This command can be read as, “Search all files in all subdirectories of the current directory for the string ‘alvin’, and print the filenames that contain this pattern.” It’s an extremely powerful approach for recursively searching files in all subdirectories that match the pattern I specify. Solution 1: Combine 'find' and 'grep'įor years I always used variations of the following Linux find and grep commands to recursively search subdirectories for files that match a grep pattern:įind. Two solutions are shown next, followed by some additional details which may be useful. But you can do more with those utilities with enough knowledge.Unix/Linux grep FAQ: How can I perform a recursive search with the grep command in Linux? This was my take on how you can print files recursively in Linux. The du command is used to show the storage size of files and when used with the -a option, it will count and print the files recursively.īut I'm also going to use the -c option that will sum the total number of files available in the specified directory: Wrapping Up Similarly, you can also use the -print option with the find command if you just want to list files recursively: find Directory_name -print Use the du command to list files recursively It will show additional information such as read-write permissions: find Directory_name -ls It will list all the files but not the hidden files. You can recursively search sub-directories with the -ls option of the find command. You'll find me often praising the find command being so extensive with more than 50 options and can also be used for listing files recursively. Once you are done with the installation, you just have to append the filename with the tree command: tree Directory_name Use the find command to list files recursively If you're on a Debian-based distro, you can use this command for installation: sudo apt install tree Use the tree command to list files recursivelyīy far, this is my favorite utility when it comes to listing files recursively as it gets the output in the easiest way possible.īut it does not come pre-installed in most distributions. Here are some other ways of listing files recursively. the lowercase r is used for the reverse display of ls output.īut wait, you can't expect Linux to have just one solution. ls -R Directory_nameĪs you can see, it shows the contents of every subdirectory one by one. You can change the default behavior of the ls command to list files recursively by using the -R option. The ls command list the contents of the present directory, but it doesn't show the contents of the subdirectories by default. This is despite the existence of the dedicated dir command. The ls command is the default for viewing the contents of a directory.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |