0

I have some files containing date and timestamp which I want to exclude from the output of ls as in:

 test.log.15-05-25_20:41:05.20150611
 test.log.15-05-27_20:28:32.20150611
 test.log.15-05-26_20:29:40.20150611
 test.log.
 test-file~df_gp~1.log.
 test.log.20150616
 test.log.15-05-28_20:28:27.20150616
 test.log.15-05-27_20:28:32.20150616
 test.log.15-05-26_20:29:40.20150616
 test.log.15-05-25_20:41:05.20150616
 test-bat-test.log.

and the output should only list the below files meaning any file containing a date and timestamp should be omitted

 test.log.
 test-file~df_gp~1.log.
 test-bat-test.log.

Is there a way in which this can be done?

3
  • Is ls a requirement or would find file listing be ok?
    – user43791
    Jun 16, 2015 at 17:37
  • find will also do as long as the files with the above date and time suffix are excluded
    – user68112
    Jun 16, 2015 at 17:47
  • find . -type f ! -name 'test.log.[0-9][0-9]*'
    – cuonglm
    Jun 16, 2015 at 17:57

2 Answers 2

1

You could use GNU find :

 find * -regextype posix-extended \! -regex '.*[0-9]{8}.*' -prune

If the date is always at the end of the string, and preceded by a ., you could also use the regex .*\.[0-9]{8} which would reflect that and reduce the risk of wrongly excluded files.

How it works :

  • -regextype posix-extended selects a type of regex suitable for the regex below
  • \! negates the meaning of the statement that follows it
  • -regex '.*[0-9]{8}.*' matches any file that has eight consecutive digits (ie a date of the form 20150616)
  • -prune prevents find from listing the sub directories

Other option :

  • Add the -f option to only list files

EDIT:

On AIX with no support for regular expression in find, you could use the following (using the standard globing mechanism) :

find * \! -name '*[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]*' -prune

Or, if the date is always at the end of the string, and preceded by a ., the following pattern can be used : *.[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]

EDIT 2:

If you want the files to begin with test, you could use :

find * -prune -type f \! -name 'test*.[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'

If you don't want to use the filename expansion by the shell and don't have additional directory in that directory, you could also use :

find . -type f \! -name 'test*.[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
8
  • I am getting find: bad option -regextype error. Please can you suggest how to fit this for the example I've shown? I am running this on AIX
    – user68112
    Jun 16, 2015 at 17:56
  • Oh... AFAIK, find on AIX doesn't support regex at all...
    – user43791
    Jun 16, 2015 at 18:00
  • @user68112 : Edited with a new solution with that in mind
    – user43791
    Jun 16, 2015 at 18:07
  • so how do I use this in my example where all filenames start with 'test' in the current directory, have other names in between like test.log. or test-file~df_gp~1.log. or test-bat-test.log. but all ending with a date preceded by a . as you rightly said. Am new to this usage
    – user68112
    Jun 16, 2015 at 18:11
  • @user68112 find * -prune -type f \! -name 'test*.[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
    – user43791
    Jun 16, 2015 at 18:18
0

I generally use | grep -v to filter out lines I do not want. If the grep does not remove enough, I add another one:

$ ls | grep -v 2015 |grep -v 2014

This is easier than having to remember regex posix parameters for find :)

1
  • 1
    You could also add patterns using the -e option of grep and use one instance to rule all your patterns. ;)
    – user43791
    Jun 16, 2015 at 20:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .