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
SUMMARY

The discussion focuses on creating a shell script to filter files that contain only capital letters using the `ls` command and `grep`. The initial attempt used the command `ls | grep [A-Z][^a-z0-9]`, which incorrectly included files with symbols. The correct approach involves using regular expressions to ensure that the filenames start and end with capital letters exclusively, utilizing the `^` and `$` anchors along with the `*` quantifier to match zero or more uppercase letters.

PREREQUISITES
  • Basic understanding of shell scripting
  • Familiarity with the `ls` command
  • Knowledge of regular expressions
  • Experience using `grep` for pattern matching
NEXT STEPS
  • Learn how to use regular expressions in shell scripting
  • Research the `grep` command options for advanced pattern matching
  • Explore the use of anchors (`^` and `$`) in regex
  • Practice writing shell scripts that manipulate file listings
USEFUL FOR

This discussion is beneficial for beginners in shell scripting, developers looking to refine their regex skills, and anyone needing to filter file names based on specific character criteria.

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
6K
  • · 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