The bash wildcards: 10 practical examples of how to use them

I have previously written an article about the asterisk and question mark wildcards and another article about the square brackes and the curly brackets wildcards.

In post I will give you 10 examples of how to use the wildcards in bash:

Example 1.
Copy all the files from the current directory, to a chosen destination:

$ ls
a1 a1b a2 a2b a3 a3b a4 a4b
$ cp ./* ~/work
$ ls ~/work
a1 a1b a2 a2b a3 a3b a4 a4b

Example2.
List only the files containing the string conf in their name, from /etc/ssh:

$ ls /etc/ssh/*conf*
/etc/ssh/ssh_config /etc/ssh/sshd_config

Example 3.
Remove all the files from the current folder:

$ ls
a1 a1b a2 a2b a3 a3b a4 a4b
$ rm *
$ ls -l
total 0

Example 4.
List only the files having a single character name and the extensions .err or .out:

$ ls
1.err 2.err 3.err1 4.err2 aa.err a.out bb.out b.out c d e f
$ ls ?.{out,err}
1.err 2.err a.out b.out

Example 5.
List all the files with a three letters extension:

$ ls
a.txt b.ext c.exe d.ext e.html f.html
$ ls *.???
a.txt b.ext c.exe d.ext

Example 6.
List all the files having part but not having 1 2 in their names.

$ ls
a b c part1 part2 part3 part4 part5
$ ls part[!12]
part3 part4 part5

Example 7.
Copy to a new destination all the files having the strings unix and linux in their names:

$ ls
freebsd linux linux3.2 macosx pcbsd unix unix2.0
$ cp {unix,linux}* ~/naboo
ls ~/naboo
linux linux3.2 unix unix2.0

Example 8.
List only the files having the unix string in their name and the file named pcbsd.

$ ls
freebsd linux linux3.2 macosx pcbsd unix unix2.0
$ ls {unix*,pcbsd}
pcbsd unix unix2.0

Example 9.
Combine touch with the { } wildcard:

$ touch a{1,2,3,4}
$ ls
a1 a2 a3 a4

Example 10.
Display all the files name a.txt or b.txt or c.txt:
$ ls
a a.txt b b.txt c c.txt d d.txt e e.txt
$ ls [a-c].txt
a.txt b.txt c.txt

Share and Enjoy

  • Twitter
  • Facebook
  • Google Plus
  • Email
  • RSS
Tagged with: , , , , ,
Posted in The Linux and Unix Articles!
2 comments on “The bash wildcards: 10 practical examples of how to use them
  1. WX says:

    example7 cp ls,the ls doesn’t work

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!