Finding Files with Capital Letters Only in Shell Script

In summary, the conversation is about a task that involves creating a shell script to pipe ls to grep and only output files with capital letters. The person has been struggling with this task for over an hour and has tried using regular expressions, but is having trouble with symbols in the output. They are seeking advice on how to properly use regular expressions to achieve their goal.
  • #1
Bimpo
9
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
  • #2
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.
 

1. What is a shell script?

A shell script is a computer program written in a shell programming language, which is a command-line interpreter used in operating systems such as Unix, Linux, and Mac OS. It is designed to automate repetitive tasks and perform system administration tasks.

2. How can I find files with capital letters only in a shell script?

You can use the "find" command with the "-name" flag and a regular expression to search for files with capital letters only. For example, "find . -name '[A-Z]*' will search the current directory and its subdirectories for any file names that start with a capital letter.

3. Can I use wildcards in my search for files with capital letters only?

Yes, you can use wildcards such as "*" or "?" in your regular expression to expand your search for files with capital letters only. For example, "find . -name '[A-Z]*.txt' will search for any text files with capital letters in their file names.

4. How can I make my search case-insensitive?

You can use the "-iname" flag instead of "-name" in the "find" command to make your search case-insensitive. This will include files with both capital and lowercase letters in their names. For example, "find . -iname '[A-Z]*' will search for files with capital letters in their names, regardless of case.

5. Can I save the results of my search to a file?

Yes, you can save the results of your search to a file by using the ">" symbol to redirect the output of the "find" command to a file. For example, "find . -name '[A-Z]*' > results.txt" will save the list of files with capital letters in their names to a file called "results.txt" in the current directory.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Programming and Computer Science
Replies
4
Views
5K
  • DIY Projects
Replies
20
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
2
Views
12K
Back
Top