What are shell wildcards

What are shell wildcards >

Wildcards are a shell feature that makes the command line much more powerful than any GUI file managers. You see, if you want to select a big group of files in a graphical file manager, you usually have to select them with your mouse. This may seem simple, but in some cases it can be very frustrating. For example, suppose you have a directory with a huge amount of all kinds of files and subdirectories, and you decide to move all the HTML files, that have the word "linux" somewhere in the middle of their names, from that big directory into another directory. What's a simple way to do this? If the directory contains a huge amount of differently named HTML files, your task is everything but simple!

In the Linux CLI that task is just as simple to perform as moving only one HTML file, and it's so easy because of the shell wildcards. Wildcards are special characters that allow you to select filenames that match certain patterns of characters. This helps you to select even a big group of files with typing just a few characters, and in most cases it's easier than selecting the files with a mouse.

Here's a list of the most commonly used wildcards in bash:

Wildcard Matches
* zero or more characters
? exactly one character
[abcde] exactly one character listed
[a-e] exactly one character in the given range
[!abcde] any character that is not listed
[!a-e] any character that is not in the given range
{debian,linux} exactly one entire word in the options given

You can use wildcards with any command that accepts file names as arguments.


Wildcard examples >

Let's have a few examples. Probably the * character is already familiar to you, because it's widely used in many other places, too, not just in Linux. For example, the following removes every file from the current directory:
rm *

The following command moves all the HTML files, that have the word "linux" in their names, from the working directory into a directory named dir1:
mv *linux*.html dir1

See, I told you that moving multiple files can be just as simple as moving only one file!

The following displays all files that begin with d and end with .txt:
less d*.txt

The following command removes all files whose names begin with junk., followed by exactly three characters:
rm junk.???

With this command you list all files or directories whose names begin with hda, followed by exactly one numeral:
ls hda[0-9]

This lists all files or directories beginning with hda, followed by exactly two numerals:
ls hda[0-9][0-9]

What are shell wildcards >

Wildcards are a shell feature that makes the command line much more powerful than any GUI file managers. You see, if you want to select a big group of files in a graphical file manager, you usually have to select them with your mouse. This may seem simple, but in some cases it can be very frustrating. For example, suppose you have a directory with a huge amount of all kinds of files and subdirectories, and you decide to move all the HTML files, that have the word "linux" somewhere in the middle of their names, from that big directory into another directory. What's a simple way to do this? If the directory contains a huge amount of differently named HTML files, your task is everything but simple!

In the Linux CLI that task is just as simple to perform as moving only one HTML file, and it's so easy because of the shell wildcards. Wildcards are special characters that allow you to select filenames that match certain patterns of characters. This helps you to select even a big group of files with typing just a few characters, and in most cases it's easier than selecting the files with a mouse.

Here's a list of the most commonly used wildcards in bash:

Wildcard Matches
* zero or more characters
? exactly one character
[abcde] exactly one character listed
[a-e] exactly one character in the given range
[!abcde] any character that is not listed
[!a-e] any character that is not in the given range
{debian,linux} exactly one entire word in the options given

You can use wildcards with any command that accepts file names as arguments.


Wildcard examples >

Let's have a few examples. Probably the * character is already familiar to you, because it's widely used in many other places, too, not just in Linux. For example, the following removes every file from the current directory:
rm *

The following command moves all the HTML files, that have the word "linux" in their names, from the working directory into a directory named dir1:
mv *linux*.html dir1

See, I told you that moving multiple files can be just as simple as moving only one file!

The following displays all files that begin with d and end with .txt:
less d*.txt

The following command removes all files whose names begin with junk., followed by exactly three characters:
rm junk.???

With this command you list all files or directories whose names begin with hda, followed by exactly one numeral:
ls hda[0-9]

This lists all files or directories beginning with hda, followed by exactly two numerals:
ls hda[0-9][0-9]

The following lists all files or directories whose name starts with either hd or sd, followed by any single character between a and c:
ls {hd,sd}[a-c]

This command copies all files, that begin with an uppercase letter, to directory dir2:
cp [A-Z]* dir2

This deletes all files that don't end with ceh or g:
rm *[!cehg]

I could continue on and on with these examples, but you get the idea. You can use simple patterns or combine different wildcards and construct very complex patterns, and like I said before, you can use them with any commands that accept file names as arguments.

The following lists all files or directories whose name starts with either hd or sd, followed by any single character between a and c:
ls {hd,sd}[a-c]

This command copies all files, that begin with an uppercase letter, to directory dir2:
cp [A-Z]* dir2

This deletes all files that don't end with ceh or g:
rm *[!cehg]

I could continue on and on with these examples, but you get the idea. You can use simple patterns or combine different wildcards and construct very complex patterns, and like I said before, you can use them with any commands that accept file names as arguments.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值