Finding Files with Capital Letters Only in Shell Script

  • Thread starter Thread starter Bimpo
  • Start date Start date
  • Tags Tags
    files Shell
Click For Summary
To find files with only capital letters in a shell script, the user needs to refine their grep command. The initial attempt, `ls | grep [A-Z][^a-z0-9]`, incorrectly matches files with symbols. A correct approach involves using regular expressions to ensure that filenames start and end with capital letters only, utilizing `^` and `$` for boundaries and `*` for repetitions. This will filter out any lowercase letters, numbers, or symbols. Understanding regular expressions is crucial for achieving the desired output.
Bimpo
Messages
9
Reaction score
0

Homework Statement



I have a really basic task in which I have to make a shell script, pipe ls to grep and
only output files that has capitals in it, meaning no lower case, no symbols, no numbers, etc.

I've been searching all over google and my notes but I've been doing this over an hour and still can't figure it out

Homework Equations



None.

The Attempt at a Solution



I tried ls | grep [A-Z][^a-z0-9]
but it prints words with symbol such as "_" eg: "DAFA_FDSA".
 
Physics news on Phys.org
Welcome to PF, Bimpo! :smile:

Apparently you need to learn a little more about regular expressions.
Right now you're matching files that have at least one capital in it, followed by 1 symbol that is not a lowercase letter of digit.

You will need to match the beginning and end of the filename with ^ resp. $, and you will have to specify that they all have to be uppercase letters, using * to indicate zero or more repetitions.
 

Similar threads

Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
12K
  • · Replies 20 ·
Replies
20
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
9
Views
3K