1

This question already has an answer here:

I have a bunch of files in a folder that all end in a different version number, like 1, 2, 3, etc. Some of them have extensions, some of them don't. Is there any way I could use a command like ls, find, or grep that can list all of the files that end in a specific character, like '1', that may or may not end in an extension?

For example, in a directory, I have the following files: ver1.txt, ver1, and file.1. I'd want to do something that would return ver1.txt and ver1, but not file.1, if that helps any. I've tried using something like ls *[1.]* but that's not doing what I want...

marked as duplicate by muru, RalfFriedl, Romeo Ninov, Archemar, taliezin Sep 12 '18 at 9:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

Pure find:

find . -regex '\.[^.]*1\|.*1\.[^.]*'

This will match ver1 and ver1.txt and file1.ver1.txt, but not file1.ver2.txt. If you want to match that too:

find . -regex '\.[^.]*1\|.*1\..*'

You must run these commands inside searched directory, find somepath ... whouldn't work, as regex match whole path.

  • pure find prints: find: Bad Option: '-regex'. – schily Sep 12 '18 at 7:55
  • @schily there are many find variants, not 100% compatible with each other. Most popular is gnu find and regex is available there. – jimmij Sep 12 '18 at 10:38
  • A pure find is a find that implements POSIX compliance - not more. Whether gfind is most popular iss disputable since embedded Linux platforms come with busybox and a fake find implementatoion. – schily Sep 12 '18 at 10:47
1

As it isn't possible for you to give an example of every filename that could ever exist and every pattern that you could ever search for, I'll go on what you have:

find . -type f | awk -F / '{print $2}' | grep -v '\.[1-100]$'

Considering that you've given ver1.txt, ver1, and file.1 as the files in the directory where you are searching, the above command will return your desired output:

ver1.txt
ver1

To explain each part:

find . -type f Returns every file in the current directory

awk -F / '{print $2}' Removes the ./ at the beginning of each file appearing from the find command

grep -v '\.[1-100]$' Returns any files not ending in .# where # is any number from 1-100. The v switch finds any lines not matching .# In the case of the example you've given, it's necessary to use \ to escape the . and to use single quotes around the string. The $ indicates files ending with that string although in your case, it will work without it. I only included it in case there may be files that have a . somewhere else in the filename.

You'll have to change the parameters depending on what you're looking for but that should get you started.

Not the answer you're looking for? Browse other questions tagged or ask your own question.