6

I want to list all the files whose name begins with uppercase:

[root@localhost /]# ls /usr/bin/[[:upper:]]*
/usr/bin/AtoB            /usr/bin/GenIssuerAltNameExt   /usr/bin/PKCS12Export
/usr/bin/AuditVerify     /usr/bin/GenSubjectAltNameExt  /usr/bin/POST
/usr/bin/BtoA            /usr/bin/GET                   /usr/bin/PrettyPrintCert
/usr/bin/CMCEnroll       /usr/bin/HEAD                  /usr/bin/PrettyPrintCrl
/usr/bin/CMCRequest      /usr/bin/HtFileType            /usr/bin/RSA_SecurID_getpasswd
/usr/bin/CMCResponse     /usr/bin/HttpClient            /usr/bin/RunSimTest
/usr/bin/CMCRevoke       /usr/bin/IBMgtSim              /usr/bin/TokenInfo
/usr/bin/CRMFPopClient   /usr/bin/Mail                  /usr/bin/X
/usr/bin/ExtJoiner       /usr/bin/OCSPClient            /usr/bin/Xorg
/usr/bin/GenExtKeyUsage  /usr/bin/PKCS10Client

It works OK, but when applied the current folder, it seem weird:

[root@localhost /]# ls ./[[:upper:]]*
snk321cq
[root@localhost /]# ls -lt snk321cq
ls: cannot access snk321cq: No such file or directory
[root@localhost /]# ls -lt ./snk321cq
ls: cannot access ./snk321cq: No such file or directory

Why display snk321cq? Actually there is no such a file.

  • 1
    Try ls -b ./[[:upper:]]*, or ls ./[[:upper:]]* | cat -v – wurtel Jan 13 '15 at 8:16
  • @wurtel:It still diplays snk321cq. – Nan Xiao Jan 13 '15 at 8:17
  • 2
    OK, maybe you have a subdirectory whose name starts with a capital letter and that contains just one file snk321cq. Try ls -d ./[[:upper:]]* ... edit: Ah, I should have refreshed the page instead of clicking "show 1 more comment" :-) jlliagre came to the same conclusion before I did. – wurtel Jan 13 '15 at 8:36
15

This file is under a directory matching the pattern, use:

ls -d ./[[:upper:]]*

By default, when passed a directory name as argument, ls displays its content, not its name. The -d option is disabling this feature.

When using the [[:upper:]]* pattern, the shell is expanding it to every filename starting with an uppercase letter so ls receives the expanded directory name.

| improve this answer | |
  • Yeah, it is right. ls -d ./[[:upper:]]* displays a folder Users, and in this folder, there exists snk321cq folder. Why ls ./[[:upper:]]* will not display Users, instead displays its sub-folder name? – Nan Xiao Jan 13 '15 at 8:39
  • Answer updated with an explanation. – jlliagre Jan 13 '15 at 8:43
0

If you need just to list the files/directories without their contents, better use

find  /usr/bin/ -maxdepth 1 -name "[[:upper:]]*"

Anyway better to use find than ls for finding files/dirs
/usr/bin/Xvfb /usr/bin/Mail /usr/bin/GET /usr/bin/POST /usr/bin/HEAD /usr/bin/Test

and this way you can show the ls output of the find results:

find /usr/bin/ -maxdepth 1 -name [[:upper:]]* -ls 272304 1956 -rwxr-xr-x 1 root root 1999608 Oct 18 07:48 /usr/bin/Xvfb 269561 0 lrwxrwxrwx 1 root root 15 Nov 4 2011 /usr/bin/Mail -> ../../bin/mailx 283683 16 -rwxr-xr-x 1 root root 14519 Aug 19 2010 /usr/bin/GET 283685 16 -rwxr-xr-x 1 root root 14519 Aug 19 2010 /usr/bin/POST 283684 16 -rwxr-xr-x 1 root root 14519 Aug 19 2010 /usr/bin/HEAD 21421 4 drwxr-xr-x 2 root root 4096 Jan 13 08:17 /usr/bin/Test

| improve this answer | |
  • You should quote [[:upper:]]* otherwise this command will fail if there are filenames starting with an uppercase character in the current directory. While there are cases wherefind is better than ls, your last command can be replaced by the much simpler ls -dils /usr/bin/[[:upper:]]* – jlliagre Jan 13 '15 at 14:57
  • Correct with the quotes :). But what do you mean by simple..? This is still a one line command with less flags than your suggestion... Plus, find is faster that ls. – csny Jan 13 '15 at 16:39
  • You should edit your reply to fix the quote issues. By simpler, I mean easier to type, 32 keystrokes vs 52. Regarding performance, I doubt there would be any significant difference. The only case where find would be a better choice is for directories with a huge number of files matching the pattern. – jlliagre Jan 13 '15 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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