How to list the files whose name begin with any of the character a to k (both inclusive)?
7 Answers
find . -type f -name '[a-k]*'
or (to be safe against locale problems)
find . -type f -name '[abcdefghijk]*'

- 90,279
-
locale problems, can you please provide an example? Are there locales where
[a-k] != [abcdefghijk]
? – A.L Jan 19 '15 at 17:47 -
2
-
I'll admit I'm fuzzy on this - but doesn't this match less than
[a-k]
where locale might be a problem? I thought the locale thing was about it not getting collation weights quite right - like outliers that didn't fit. MaybeLC_ALL=C find ...
? – mikeserv Jan 19 '15 at 21:10 -
@mikeserv Yes, the enumeration matches less than
[a-k]
(or equal to).LC_ALL=C
is another way to solve that but it may cause problems if thefind
call needs non-C chars elsewhere:find . -type f -name '[abcdefghijk]*' -printf "übereinstimmende Datei: %p\n"
– Hauke Laging Jan 19 '15 at 23:32 -
Interesting... I guess in that case
... -exec sh -c 'LC_ALL=..; printf ...'
and so on might apply but it definitely gets more complicated. Anyway, thanks, and good answer. – mikeserv Jan 19 '15 at 23:47
Try this:
find . -type f -name "[a-k]*"

- 9,226
-
3In most locales (including modern US ones), that includes
á
(oræ
ordz
) for instance but notḱ
(also note that nothing guarantees that file names are encoded in the same charset as that of the user's locale). – Stéphane Chazelas Jan 19 '15 at 14:15 -
@StéphaneChazelas Very interesting, thanks! So, yeah, Hauke Laging's answer is more complete. – Ketan Jan 19 '15 at 18:43
You can use Bash command line expansion feature for this.
$ ls -l [a-k]*
[a-k]
refers to alphabets from a
to k
.
*
refers to any character any number of times.
So now bash looks for files starting with letter a
to k
and followed by any character any number of times.

- 3,231
-
If some of those files are of type directory,
ls
will list their content. You may want to add the-d
option like similar answers. Note that what[a-k]
actually means depends on the user's locale (it's generally notabcdefghijk
even in US locales nowadays) – Stéphane Chazelas Jan 19 '15 at 13:52
Just use shell globbing (test: echo [a-k]*
). You usually need to iterate over files, so the usual pattern is for file in [a-k]*; do something; done
. Never use ls
for iteration.
Helpful read: http://mywiki.wooledge.org/ParsingLs

- 12,502
-
2
-
+1 to stephane. You want to :
for file in [abcdefghijk]*; do something_with "$file" ; done
. Don't use echo/ls/find/etc. (I believe that's what Orion means, but the first sentence is misleading, I think... it seems to say "use echo [a-k]* instead of ls" (but the rest of the paragraph shows you know it's best to just use the glob directly) (so +1 for you in the end, too) – Olivier Dulac Jan 19 '15 at 15:11 -
Sure, if you need iteration, you use
for
.echo
was just for demonstration purposes, I'll fix it. – orion Jan 19 '15 at 19:19
-To Be more specific since it was not clear whether it was to display the current directory files or system files, you can use one of the defined modes.
ls -d [a-k]* -> to display the files/directories of the current place/directory/folder only.
or
find / -type f -name '[k]'* -> to display the files/whole system directories and by remembering that the difference here would be that the hidden items are not displayed, as with so already shown.
find . -type f -name '[a-k]*'
if used the 'ls' command to also view the hidden use of this mode.
ls -da [a-k]*

- 903
- 1
- 8
- 11
-
Hidden files are those whose name starts with
.
(which usually doesn't sit betweena
andk
).find . -name '[a-k]*'
will generally not list hidden files as a result, but it will still list files in hidden directories.-a
forls
doesn't make a difference, it's the shell that builds the file list to pass tols
,ls
only displays them. You might as well have usedprintf '%s\n' [a-k]*
. – Stéphane Chazelas Jan 19 '15 at 13:18
Just for reference, with zsh: (And not getting into the ls/echo/etc. thread)
Display plain files in current directory:
ls [a-k]*(.)
Display all types of files in current directory:
ls -d [a-k]*
The same recursively:
ls -d -- **/[a-k]*(.)
(Not looking inside hidden directories) and
ls -d -- **/[a-k]*

- 544,893

- 363
- 1
- 5
a
tok
? Isḱ
one of them for instance? – Stéphane Chazelas Jan 19 '15 at 12:35