Search code examples
fileunixls

UNIX 'ls' command exclude unwanted files


Current directory has below files.

hello.log
hello.2019-12-23.1.log
hello.2019-12-23.2.log
hello.2019-12-23.3.log
hello.log.2019-12-22.log.gz
hello.log.2019-12-21.log.gz
abc.log
xyz.log

I would like my ls command return only current log files (exclude all archive, old log files)

Below output is expected

hello.log
abc.log
xyz.log

Solution

  • This depends greatly on the conditions of which one wants to ignore entries. Nonetheless, filename globbing expressions can be used to limit the results of the filename selection called with the ls command. The operations are not very sophisticated (unlike regex, for example), so only simple expressions can be used. In the example list of files in the original question, one could prune out only the desired files by only showing a specific extension (.e.g .log) that do not end with a number (e.g. [!0-9])

    So the ls command would look like this:

    ls *[!0-9].log
    

    This works for the example shown. However, it depends on the complexity of the filename list.