{"id":11618,"date":"2019-03-15T01:39:13","date_gmt":"2019-03-15T01:39:13","guid":{"rendered":"http:\/\/www.appservgrid.com\/paw92\/?p=11618"},"modified":"2019-03-15T01:39:13","modified_gmt":"2019-03-15T01:39:13","slug":"10-practical-examples-using-wildcards-to-match-filenames-in-linux","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/15\/10-practical-examples-using-wildcards-to-match-filenames-in-linux\/","title":{"rendered":"10 Practical Examples Using Wildcards to Match Filenames in Linux"},"content":{"rendered":"<p><strong>Wildcards<\/strong>\u00a0(also referred to as meta characters) are symbols or special characters that represent other characters. You can use them with any command such as\u00a0<a href=\"https:\/\/www.tecmint.com\/tag\/linux-ls-command\/\" target=\"_blank\" rel=\"noopener\">ls command<\/a>\u00a0or\u00a0<strong>rm command<\/strong>\u00a0to list or remove files matching a given criteria, receptively.<\/p>\n<p>Read Also:\u00a0<a href=\"https:\/\/www.tecmint.com\/chaining-operators-in-linux-with-practical-examples\/\" target=\"_blank\" rel=\"noopener\">10 Useful Practical Examples on Chaining Operators in Linux<\/a><\/p>\n<p>These wildcards are interpreted by the shell and the results are returned to the command you run. There are three main wildcards in Linux:<\/p>\n<ul>\n<li>An asterisk\u00a0<code>(*)<\/code>\u00a0\u2013 matches one or more occurrences of any character, including no character.<\/li>\n<li>Question mark\u00a0<code>(?)<\/code>\u00a0\u2013 represents or matches a single occurrence of any character.<\/li>\n<li>Bracketed characters\u00a0<code>([ ])<\/code>\u00a0\u2013 matches any occurrence of character enclosed in the square brackets. It is possible to use different types of characters (alphanumeric characters): numbers, letters, other special characters etc.<\/li>\n<\/ul>\n<p>You need to carefully choose which wildcard to use to match correct filenames: it is also possible to combine all of them in one operation as explained in the examples below.<\/p>\n<h3>How to Match Filenames Using Wildcards in Linux<\/h3>\n<p>For the purpose of this article, we will use following files to demonstrate each example.<\/p>\n<pre>createbackup.sh  list.sh  lspace.sh        speaker.sh\r\nlistopen.sh      lost.sh  rename-files.sh  topprocs.sh\r\n<\/pre>\n<p><strong>1.<\/strong>\u00a0This command matches all files with names starting with\u00a0<code>l<\/code>\u00a0(which is the prefix) and ending with one or more occurrences of any character.<\/p>\n<pre>$ ls -l l*\t\r\n<\/pre>\n<div id=\"attachment_27491\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/List-Files-with-Character.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27491\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/List-Files-with-Character.png\" alt=\"List Files with Character\" width=\"642\" height=\"116\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Files with Character<\/p>\n<\/div>\n<p><strong>2.<\/strong>\u00a0This example shows another use of\u00a0<code>*<\/code>\u00a0to copy all filenames prefixed with\u00a0<code>users-0<\/code>\u00a0and ending with one or more occurrences of any character.<\/p>\n<pre>$ mkdir -p users-info\r\n$ ls users-0*\r\n$ mv -v users-0* users-info\/\t# Option <strong>-v<\/strong> flag enables verbose output\r\n<\/pre>\n<div id=\"attachment_27492\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/List-and-Copy-All-Files.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27492\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/List-and-Copy-All-Files.png\" alt=\"List and Copy All Files\" width=\"642\" height=\"287\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List and Copy All Files<\/p>\n<\/div>\n<p><strong>3.<\/strong>\u00a0The following command matches all files with names beginning with\u00a0<code>l<\/code>\u00a0followed by any single character and ending with\u00a0<code>st.sh<\/code>\u00a0(which is the suffix).<\/p>\n<pre>$ ls l?st.sh\t\r\n<\/pre>\n<div id=\"attachment_27493\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Match-File-with-Character-Name.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27493\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Match-File-with-Character-Name.png\" alt=\"Match File with Character Name\" width=\"642\" height=\"78\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Match File with Character Name<\/p>\n<\/div>\n<p><strong>4.<\/strong>\u00a0The command below matches all files with names starting with\u00a0<code>l<\/code>\u00a0followed by any of the characters in the square bracket but ending with\u00a0<code>st.sh<\/code>.<\/p>\n<pre>$ ls l[abdcio]st.sh \r\n<\/pre>\n<div id=\"attachment_27494\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Matching-Files-with-Names.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27494\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Matching-Files-with-Names.png\" alt=\"Matching Files with Names\" width=\"642\" height=\"78\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Matching Files with Names<\/p>\n<\/div>\n<h3>How to Combine Wildcards to Match Filenames in Linux<\/h3>\n<p>You can combine wildcards to build a complex filename matching criteria as described in the following examples.<\/p>\n<p><strong>5.<\/strong>\u00a0This command will match all filenames prefixed with any two characters followed by\u00a0<code>st<\/code>\u00a0but ending with one or more occurrence of any character.<\/p>\n<pre>$ ls\r\n$ ls ??st*\r\n<\/pre>\n<div id=\"attachment_27495\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Find-File-Names-with-Prefix.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27495\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Find-File-Names-with-Prefix.png\" alt=\"Match File Names with Prefix\" width=\"672\" height=\"154\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Match File Names with Prefix<\/p>\n<\/div>\n<p><strong>6.<\/strong>\u00a0This example matches filenames starting with any of these characters\u00a0<code>[clst]<\/code>\u00a0and ending with one or more occurrence of any character.<\/p>\n<pre>$ ls\r\n$ ls [clst]*\r\n<\/pre>\n<div id=\"attachment_27497\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Match-Files-with-Characters.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27497\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Match-Files-with-Characters.png\" alt=\"Match Files with Characters\" width=\"672\" height=\"154\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Match Files with Characters<\/p>\n<\/div>\n<p><strong>7.<\/strong>\u00a0In this examples, only filenames starting with any of these characters\u00a0<code>[clst]<\/code>\u00a0followed by one of these\u00a0<code>[io]<\/code>\u00a0and then any single character, followed by a\u00a0<code>t<\/code>\u00a0and lastly, one or more occurrence of any character will be listed.<\/p>\n<pre>$ ls\r\n$ ls [clst][io]?t*\r\n<\/pre>\n<div id=\"attachment_27498\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/List-Files-with-Multiple-Characters.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27498\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/List-Files-with-Multiple-Characters.png\" alt=\"List Files with Multiple Characters\" width=\"632\" height=\"135\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">List Files with Multiple Characters<\/p>\n<\/div>\n<p><strong>8.<\/strong>\u00a0Here, filenames prefixed with one or more occurrence of any character, followed by the letters\u00a0<code>tar<\/code>\u00a0and ending with one or more occurrence of any character will be removed.<\/p>\n<pre>$ ls\r\n$ rm *tar*\r\n$ ls\r\n<\/pre>\n<div id=\"attachment_27499\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Remove-Files-with-Letters.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27499\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Remove-Files-with-Letters.png\" alt=\"Remove Files with Character Letters\" width=\"742\" height=\"211\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Remove Files with Character Letters<\/p>\n<\/div>\n<h3>How to Match Characters Set in Linux<\/h3>\n<p><strong>9.<\/strong>\u00a0Now lets look at how to specify a set of characters. Consider the filenames below containing system users information.<\/p>\n<pre>$ ls\r\n\r\nusers-111.list  users-1AA.list  users-22A.list  users-2aB.txt   users-2ba.txt\r\nusers-111.txt   users-1AA.txt   users-22A.txt   users-2AB.txt   users-2bA.txt\r\nusers-11A.txt   users-1AB.list  users-2aA.txt   users-2ba.list\r\nusers-12A.txt   users-1AB.txt   users-2AB.list  users-2bA.list\r\n<\/pre>\n<p>This command will match all files whose name starts with\u00a0<code>users-i<\/code>, followed by a number, a lower case letter or number, then a number and ends with one or more occurrences of any character.<\/p>\n<pre>$ ls users-[0-9][a-z0-9][0-9]*\r\n<\/pre>\n<p>The next command matches filenames beginning with\u00a0<code>users-i<\/code>, followed by a number, a lower or upper case letter or number, then a number and ends with one or more occurrences of any character.<\/p>\n<pre>$ ls users-[0-9][a-zA-Z0-9][0-9]*\r\n<\/pre>\n<p>This command that follows will match all filenames beginning with\u00a0<code>users-i<\/code>, followed by a number, a lower or upper case letter or number, then a lower or upper case letter and ends with one or more occurrences of any character.<\/p>\n<pre>$ ls users-[0-9][a-zA-Z0-9][a-zA-Z]*\r\n<\/pre>\n<div id=\"attachment_27500\" class=\"wp-caption aligncenter\">\n<p><a href=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Match-Characters-in-Filenames.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-27500\" src=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Match-Characters-in-Filenames.png\" sizes=\"auto, (max-width: 782px) 100vw, 782px\" srcset=\"https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Match-Characters-in-Filenames.png 782w, https:\/\/www.tecmint.com\/wp-content\/uploads\/2017\/10\/Match-Characters-in-Filenames-768x319.png 768w\" alt=\"Match Characters in Filenames\" width=\"782\" height=\"325\" data-lazy-loaded=\"true\" \/><\/a><\/p>\n<p class=\"wp-caption-text\">Match Characters in Filenames<\/p>\n<\/div>\n<h3>How to Negate a Set of Characters in Linux<\/h3>\n<p><strong>10.<\/strong>\u00a0You can as well negate a set of characters using the\u00a0<code>!<\/code>\u00a0symbol. The following command lists all filenames starting with\u00a0<code>users-i<\/code>, followed by a number, any valid file naming character apart from a number, then a lower or upper case letter and ends with one or more occurrences of any character.<\/p>\n<pre>$ ls users-[0-9][!0-9][a-zA-Z]*\r\n<\/pre>\n<p>That\u2019s all for now! If you have tried out the above examples, you should now have a good understanding of how wildcards work to match filenames in Linux.<\/p>\n<p>You might also like to read these following articles that shows examples of using wildcards in Linux:<\/p>\n<ol>\n<li><a href=\"https:\/\/www.tecmint.com\/extract-tar-files-to-specific-or-different-directory-in-linux\/\" target=\"_blank\" rel=\"noopener\">How to Extract Tar Files to Specific or Different Directory in Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/delete-all-files-in-directory-except-one-few-file-extensions\/\" target=\"_blank\" rel=\"noopener\">3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/useful-tips-for-writing-bash-scripts-in-linux\/\" target=\"_blank\" rel=\"noopener\">10 Useful Tips for Writing Effective Bash Scripts in Linux<\/a><\/li>\n<li><a href=\"https:\/\/www.tecmint.com\/use-linux-awk-command-to-filter-text-string-in-files\/\" target=\"_blank\" rel=\"noopener\">How to Use Awk and Regular Expressions to Filter Text or String in Files<\/a><\/li>\n<\/ol>\n<p>If you have any thing to share or a question(s) to ask, use the comment form below.<\/p>\n<p><a href=\"https:\/\/www.tecmint.com\/use-wildcards-to-match-filenames-in-linux\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Wildcards\u00a0(also referred to as meta characters) are symbols or special characters that represent other characters. You can use them with any command such as\u00a0ls command\u00a0or\u00a0rm command\u00a0to list or remove files matching a given criteria, receptively. Read Also:\u00a010 Useful Practical Examples on Chaining Operators in Linux These wildcards are interpreted by the shell and the results &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2019\/03\/15\/10-practical-examples-using-wildcards-to-match-filenames-in-linux\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;10 Practical Examples Using Wildcards to Match Filenames in Linux&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11618","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11618","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=11618"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11618\/revisions"}],"predecessor-version":[{"id":11619,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/11618\/revisions\/11619"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=11618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=11618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=11618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}