How to delete files containing a character or number/digit in their filename on Linux or Unix

I am a new Linux system user. How do I delete file containing a character ‘a’ or ‘z’ in their filename or digit such as ‘4’ or ‘2’ in their filename on Linux or Unix bash shell prompt?

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.
Bash shell Globbing for ls, rm and any other command
Please note that wildcard patterns are not regular expressions. They match and work on filenames, rather than text. Bash shell support the following wildcards:

ADVERTISEMENTS

  • * : 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

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
5 comments… add one
  • David G Aug 24, 2017 @ 12:27

    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

  • Erin Aug 24, 2017 @ 19:16

    I think the example for “List txt files that start with a capital letter:” is backwards?

  • Erin Aug 24, 2017 @ 21:06

    I really love your troubleshooting. It’s fun reading them.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.