You need to use the rm command to remove the files specified on the command line. You need to use bash special feature called globbing (a “wildcard”) for filename expansion.
Please note that wildcard patterns are not regular expressions. They match and work on filenames, rather than text. Bash shell support the following wildcards:
- * : Matches any string
- *a* : Matches any string containing an ‘a’
- *9* : Matches any string containing a digit ‘9’
- *.[xy] : Matches any string ending with .x or .y
- *[ab]* : Matches any string containing a character ‘a’ or ‘b’
- *[42]* : Matches any string containing a digit ‘4’ or ‘2’
- ? : Matches any single character
Examples
Let us consider the following files:
$ ls -l
Sample outputs:
total 0 -rw-r--r-- 1 veryv wheel 0 Aug 24 01:37 001 -rw-r--r-- 1 veryv wheel 0 Aug 24 01:37 002 -rw-r--r-- 1 veryv wheel 0 Aug 24 01:39 1.txt -rw-r--r-- 1 veryv wheel 0 Aug 24 01:37 13 -rw-r--r-- 1 veryv wheel 0 Aug 24 01:37 13aa -rw-r--r-- 1 veryv wheel 0 Aug 24 01:37 42 -rw-r--r-- 1 veryv wheel 0 Aug 24 01:37 4213aa -rw-r--r-- 1 veryv wheel 0 Aug 24 01:42 A.txt -rw-r--r-- 1 veryv wheel 0 Aug 24 01:22 bar -rw-r--r-- 1 veryv wheel 0 Aug 24 01:39 c.txt -rw-r--r-- 1 veryv wheel 0 Aug 24 01:37 cd4213aa -rw-r--r-- 1 veryv wheel 0 Aug 24 01:37 file2.txt -rw-r--r-- 1 veryv wheel 0 Aug 24 01:37 file4.txt -rw-r--r-- 1 veryv wheel 0 Aug 24 01:37 file40.txt -rw-r--r-- 1 veryv wheel 0 Aug 24 01:22 foo -rw-r--r-- 1 veryv wheel 0 Aug 24 01:22 raj -rw-r--r-- 1 veryv wheel 0 Aug 24 01:22 sai -rw-r--r-- 1 veryv wheel 0 Aug 24 01:22 vivek
To list all .txt file run:
$ ls *.txt
1.txt c.txt file2.txt file4.txt file40.txt
To see txt files with 1 char names (eg z.txt, 3.txt)
$ ls ?.txt
1.txt c.txt
List txt files that start with a capital letter:
$ ls [A-Z]*.txt
A.txt
Can you guess what the following command does?
$ ls [A-Za-z]*.txt
A.txt c.txt file2.txt file4.txt file40.txt
You can use a wildcard with any Linux/Unix command such as rm command,cp command,mv command,tar command and so on. To delete files containing a number ‘4’ or ‘2’ in their filename:
$ ls *[42]*
$ rm -v *[42]*
To limit to .jpg extension:
$ ls *[42]*.jpg
$ rm -v *[42]*.jpg
Another example where file name start with a capital ‘X’ and ends with .JPG extension and files containing a number ‘4’ or ‘2’ in their filename:
$ ls X*[42]*.jpg
$ rm -v X*[42]*.jpg
A note about option which change globbing behavior for bash shell
Turn on extglob by running the following shopt command:
$ shopt -s extglob
From the bash man page:
Composite patterns may be formed using one or more of the following sub-patterns: ?(pattern-list) Matches zero or one occurrence of the given patterns *(pattern-list) Matches zero or more occurrences of the given patterns +(pattern-list) Matches one or more occurrences of the given patterns @(pattern-list) Matches one of the given patterns !(pattern-list) Matches anything except one of the given patterns
To see all the mp4 and mov files that start with either “foo” or “bar”:
$ ls +(foo|bar)*+(.mp4|.mov)
To list all the files except ones matching *.mp4:
$ ls -l !(*.mp4)
OR delete all the files except ones matching *.gif files:
$ rm -v !(*.gif)
For more info see your shell man page:
$ man bash
$ man ksh
🐧 5 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Can you double check this part?
List txt files that start with a capital letter:
$ ls [A-Z]*.txt
c.txt file2.txt file4.txt file40.txt
Yes, it was a typo on my part.
I think the example for “List txt files that start with a capital letter:” is backwards?
Thanks for the heads up! I fixed it.
I really love your troubleshooting. It’s fun reading them.