Search code examples
regexlinuxls

How can I modify this regular expression to grab the filename from ls -a output?


Let's say I already have the output of an 'ls -la' command and I now want to parse out just the filenames. I have this regular expression so far that I found, but how can I modify this to account for there being whitespace in the filename itself?

Example 'ls -la' output:

-rw-r--r--   1 admin  staff  460340057 May  4 14:19 test_large_file.xml

Regular Expression I have so far:

\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(\S+)

Solution

  • You can use something like:

    /^.*\d\s+(.*?)$/m
    

    https://regex101.com/r/mR4rH1/1