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

Posted on in Categories , , last updated August 24, 2017

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:

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

5 comment

Leave a Comment