![]() ![]() The GNU ls has provided the -I option to ignore glob patterns. Most modern Linux distributions use the GNU ls from the pre-installed GNU coreutils package. Therefore, the ls | grep -v approach may work for some cases. It cannot handle the filenames with newlines correctly. This is because grep is a line-based pattern-matching tool. The filename ‘some’$’\n”whatever warning.txt’ has been broken. Obviously, the output above isn’t expected. Now, let’s use the ls | grep approach to list all files whose names have “ warning“: $ ls -l | grep 'warning' The ls -l output displays the filename as ‘some’$’\n”whatever warning.txt’. rw-r-r- 1 kent kent 0 Mar 21 23:36 'some'$'\n''whatever warning.txt'Īs the output above shows, we’ve created a file whose name contains newline and space characters. So next, let’s create a new file under this directory: $ touch "some The grep command simply checks each output line and cannot tell whether a match is in the filename part.Īpart from that, many filesystems under Linux support special characters in filenames, such as space and newlines. This is because all files’ owners and groups are “ kent” too. This time, the command list all files under the directory. rw-r-r- 1 kent kent 0 Mar 21 21:24 warning.txt rw-r-r- 1 kent kent 0 Mar 21 21:24 system_warning.log Now, let’s see what ls | grep produces: $ ls -l | grep 'kent' Under the current directory, as we can see, only the kent.file should be in the output. Let’s say we want to get a list of files whose name contains “ kent“. However, “grepping” on ls output is considered a bad practice, and we shouldn’t go for it.Ī couple of examples can quickly explain the reason. rw-r-r- 1 kent kent 0 Mar 21 21:24 user.logĪs we can see, all filenames containing “ warning” has been filtered out. rw-r-r- 1 kent kent 0 Mar 21 21:24 server.log rw-r-r- 1 kent kent 0 Mar 21 21:37 readme.txt rw-r-r- 1 kent kent 0 Mar 21 21:59 kent.file rw-r-r- 1 kent kent 0 Mar 21 21:24 console.log So, why not pipe the output of ls -l to grep -v? First, let’s see if it works with our example: $ ls -l | grep -v 'warning' ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |