4

How to list the files whose name begin with any of the character a to k (both inclusive)?

Braiam
  • 35,991
Lavanya
  • 57

7 Answers7

6
find . -type f -name '[a-k]*'

or (to be safe against locale problems)

find . -type f -name '[abcdefghijk]*'
Hauke Laging
  • 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
    @A.L See Stephane's comment on Ketan's answer – Izkata Jan 19 '15 at 18:01
  • 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. Maybe LC_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 the find 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
6

Try this:

find . -type f -name "[a-k]*"
Ketan
  • 9,226
  • 3
    In most locales (including modern US ones), that includes á (or æ or dz) 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
3

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.

Kannan Mohan
  • 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 not abcdefghijk even in US locales nowadays) – Stéphane Chazelas Jan 19 '15 at 13:52
2

For the current directory only you can use

ls -d [a-k]*
1

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

orion
  • 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
0

-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]*
Joke Sr. OK
  • 903
  • 1
  • 8
  • 11
  • Hidden files are those whose name starts with . (which usually doesn't sit between a and k). find . -name '[a-k]*' will generally not list hidden files as a result, but it will still list files in hidden directories. -a for ls doesn't make a difference, it's the shell that builds the file list to pass to ls, ls only displays them. You might as well have used printf '%s\n' [a-k]*. – Stéphane Chazelas Jan 19 '15 at 13:18
0

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]*
Bgs
  • 363
  • 1
  • 5