Posted: . At: 11:31 PM. This was 6 years ago. Post ID: 4940
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.


Deleting files on your Linux machine that have strange names. And how to use wildcards on the shell.


A novel solution for cooling your CPU...
A novel solution for cooling your CPU…

I ran a Perl script on my system that created a bunch of files on my computer named as shown below. I could have typed rm -f A* but that could have deleted other files that have a capital “A” as the first letter of the filename. That is where wildcards come into play. I can use the power of wildcards on the command-line to remove those offending files. The clue is in the ls command below; I have to use the wildcards to display the offending files and this is the same way to delete them. I am using the csh shell at the moment, but this will work with the ZSH and Bash shells. Anyway; read on and I will give you some awesome examples for using wildcards to filter the output of the ls command and how to delete only certain files from your system whilst leaving others intact. The Linux shell is very powerful in this respect; you can use many tricks to get rid of unwanted files that are nestled amongst others on your system.

Anyway; with my problem at hand; I just typed this command: rm -f **delete-m* and the files were deleted. This trick is very useful when you only want to display certain files in a listing.

[john@deusexmachina]:~/Documents% ls **delete-m*
-rw-r--r-- 1 john john 145 Dec  3 22:17 A-JXXP-ZJNPVMBKB.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 A-NVKGHILSUEVZOW.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 AALOKWE-HEVKPACL.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 AEAKDISEAOLKERZT.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 AGATQQNWOBOEETMV.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 AHYHDTJXUVBJQYED.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 AIBZTNUASEXBM-LD.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 AJCKEOQJTMVP-TPX.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 ALPMYAGRRR-XOCIR.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 APVOSECQOJXXHU-M.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 APVZEZPZHMLWQACU.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 ASVDR-LRMULYAX-N.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 ATLLQDRNPESHSCVS.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 AUPZWEENZUXZGZCE.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 AWGTRJEYIYAIEERR.delete-me
-rw-r--r-- 1 john john 145 Dec  3 22:17 AWZGHHGVJOJSA-UI.delete-me

Take this as another example. I am displaying a listing of clipart images within a certain range.

[john@deusexmachina]:/media/Elements/Wallpapers/photohm% ls j0144**9.jpg
-rw------- 1 john john 43K Aug 23  2011 j0144269.jpg
-rw------- 1 john john 13K Aug 23  2011 j0144309.jpg
-rw------- 1 john john 36K Aug 23  2011 j0144329.jpg
-rw------- 1 john john 32K Aug 23  2011 j0144349.jpg
-rw------- 1 john john 15K Aug 23  2011 j0144359.jpg
-rw------- 1 john john 32K Dec  5  2011 j0144379.jpg
-rw------- 1 john john 42K Aug 23  2011 j0144389.jpg
-rw------- 1 john john 37K Aug 23  2011 j0144399.jpg

And now I am only displaying one file.

[john@deusexmachina]:/media/Elements/Wallpapers/photohm% ls j01442*9.jpg
-rw------- 1 john john 43K Aug 23  2011 j0144269.jpg

Use the wildcards this way to display all files that end with a certain letter or number and the proper extension.

[john@deusexmachina]:/media/Elements/Wallpapers/photohm% ls **8.jpg
-rw------- 1 john john 30K Aug 23  2011 j0144228.jpg
-rw------- 1 john john 32K Aug 23  2011 j0144248.jpg
-rw------- 1 john john 40K Aug 23  2011 j0144288.jpg
-rw------- 1 john john 24K Aug 23  2011 j0144308.jpg
-rw------- 1 john john 15K Aug 23  2011 j0144318.jpg
-rw------- 1 john john 42K Aug 23  2011 j0144338.jpg
-rw------- 1 john john 28K Aug 23  2011 j0144348.jpg
-rw------- 1 john john 22K Aug 23  2011 j0144358.jpg
-rw------- 1 john john 43K Dec  5  2011 j0144378.jpg
-rw------- 1 john john 25K Aug 23  2011 j0144398.jpg

This usage of wildcards will find all files with the number 23 in their filename.

[john@deusexmachina]:/media/Elements/Wallpapers/photohm% ls *23*.jpg
-rw------- 1 john john 36K Aug 23  2011 j0144232.jpg
-rw------- 1 john john 26K Aug 23  2011 j0144233.jpg
-rw------- 1 john john 33K Aug 23  2011 j0144234.jpg
-rw------- 1 john john 34K Aug 23  2011 j0144235.jpg
-rw------- 1 john john 30K Aug 23  2011 j0144236.jpg
-rw------- 1 john john 34K Aug 23  2011 j0144237.jpg

Here is another example that is using my movies folder instead. This is how to list irrespective of the files extension.

[john@deusexmachina]:/media/Elements/Movies% ls *Trek*.*
-rw------- 1 john john 639M Mar 24  2012 Star Trek 1 - The Motion Picture.avi
-rw------- 1 john john 349M May 19  2012 Star Trek TNG - 1x01-1x02 - Encounter at Farpoint [NiteShdw].avi
-rw------- 1 john john 172M Jun 28 17:43 Star Trek TNG - 1x06 - Where No One Has Gone Before [NiteShdw].avi
-rw------- 1 john john 550M May 26  2012 StarTrek.TNG-s01e06.Lonely.Among.Us.mkv
-rw------- 1 john john 690M May 10  2012 Star_Trek_II.mp4

Here I have a few files that are in uppercase. And I needed them to be in lowercase.

[john@deusexmachina]:/media/Elements/Videos% ls IMG_00*.D*
-rw------- 1 john john 309K Jan  5  2012 IMG_0000.DAT
-rw------- 1 john john 309K Jan  5  2012 IMG_0001.DAT
-rw------- 1 john john 309K Jan  5  2012 IMG_0002.DAT
-rw------- 1 john john 309K Jan  5  2012 IMG_0004.DAT
-rw------- 1 john john 309K Jan  5  2012 IMG_0005.DAT
-rw------- 1 john john 309K Jan  5  2012 IMG_0006.DAT

I used this command: ls *.DAT | gawk '{system("mv " $0 " " tolower($0) )}'. This command would not work with the csh shell so I had to use the zsh shell to get this command to work. Then after listing the files again they are lowercase.

[Mon 12/12/03 23:05 EST][pts/0][x86_64/linux-gnu/3.5.0-17-generic][5.0.0]
<john@deusexmachina:/media/Elements/Videos>
zsh 9 % ls -hula img**.dat
-rw------- 1 john john 309K Jan  5  2012 img_0000.dat
-rw------- 1 john john 309K Jan  5  2012 img_0001.dat
-rw------- 1 john john 309K Jan  5  2012 img_0002.dat
-rw------- 1 john john 309K Jan  5  2012 img_0004.dat
-rw------- 1 john john 309K Jan  5  2012 img_0005.dat
-rw------- 1 john john 309K Jan  5  2012 img_0006.dat

Find all filenames that are not numbers.

jason@jason-desktop:/media/jason/Seagate Backup Plus Drive/Photos$ ls -hula *[A-Z]*.jpg
-rwxrwxrwx 2 jason jason  611K Apr 29  2014 1984-instruction-manual.jpg
-rwxrwxrwx 1 jason jason  763K Oct  4  2015 bridge.jpg
-rwxrwxrwx 1 jason jason   21K Oct  4  2015 kill9.jpg
-rwxrwxrwx 2 jason jason  3.2M Oct  4  2015 old-railway.jpg
-rwxrwxrwx 2 jason jason 1021K Oct  4  2015 rockymountians.jpg

There are some awesome shell tricks for the bash shell here: http://www.securitronlinux.com/bejiitaswrath/cispa-bill-still-a-threat-and-awesome-linux-shell-tricks/.

How to use brace expansion to create a bunch of new folders all at once: http://www.securitronlinux.com/debian-testing/useful-linux-shell-tricks-using-brace-expansions-to-create-folders/.

Some more useful Linux commands here: http://www.securitronlinux.com/useful-linux-commands-tips/.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.