As the shell reads each line, it "handles" any special characters. This includes variable evaluation (variables start with a "$)," and filename expansion. Expansion of filenames occurs when the characters "*," "?," or "[" occur in a word. A question mark matches a single character. An asterisk matches any number of characters, including none. Square brackets are used to specify a range or particular combination of characters. Inside square brackets, a hyphen is used to specify a range or characters. Also, if the first character inside the square brackets is an exclamation point, the complement of the range is used. Let me give some examples:

Table 1
Examples of Bourne shell filename expansions
Pattern Matches
* Every file in the current directory
? Files consisting of one character
?? Files consisting of two characters
??* Files consisting of two or more characters
[abcdefg] Files consisting of a single letter from a to g.
[gfedcba] Same as above
[a-g] Same as above
[a-cd-g] Same as above
[a-zA-Z0-9] Files that consist of a single letter or number
[!a-zA-Z0-9] Files that consist of a single character not a letter or number
[a-zA-Z]* Files that start with a letter
?[a-zA-Z]* Files whose second character matches a letter.
*[0-9] Files that end with a number
?[0-9] Two character filename that end with a number
*.[0-9] Files that end with a dot and a number

As you can see, the dot is not a special character. Filenames may or may not have a dot. UNIX Programers use the dot to standardize on the type of source code of each file, but that is just a convention. There is another convention, which concerns the shell:

正如你所看到的,dot不是特殊字符,文件名可有可无dot。unix程序员习惯使用dot来标准化源代码的文件

Files whose name starts with a dot are not normally listed.
以dot开头的文件不列出。

Again, it is a convention, but lsfind and the various shells follow this convention. This allows some files to be "secret," or perhaps invisible, by default. You must explicitly ask for these files, by including the dot as part of the filename. The pattern ".*" matches all hidden files.

我使用ls .* 效果:

/home/sumng/Documents/sh    Documents下面有一个.secret文件。

ms@sumng-VirtualBox:~/Documents$ ls .*
.secret .:
file1 find sh test1 () test1zip test2.txt test3.txt
file1~ new3 test~ test1 () test1.zip test2.zip
file1.zip rm test1 test1.txt test2 test3 ..:
Desktop examples.desktop Pictures suming.txt Videos
Documents hello.txt printf.sh Templates
Downloads Music Public Ubuntu One

可有看到除了把.secret显示除了,也把Documents下其他文件显示了,因为其他文件是在当前目录,也是以dot开头,也把上级目录显示

出来了。)

Remember that two hidden files are always in every directory, ".," which indicate the present directory, and "..," which indicates the directory above the current directory. If you want to match all hidden files except these two directories, there is no easy way to specify a pattern that will always match all files except the two directories. I use

.??

or

.[a-zA-Z]*

As I said, this does not match all combinations, but works most of the time. Hackers (or crackers, if you prefer) break into computers and often use strange filenames, like ". " or ".. " to hide their traces. You may not have noticed, but there was a space in these filenames. Refering to files with spaces in the names require quoting, which I will cover later. Personally, all of my hidden files are matched by ".[a-z]*" and all of my hidden directories are matched by ".[A-Z]*." This works because I made up my own convention, and always follow it.

The slash is also special, as it is used to indicate a directory path. Filename expansion does not expand to match a slash, because a slash can never be part of the filename. Also, the same rules for filename expansion of hidden files applies if the pattern follows a slash. If you want to match hidden files in a subdirectory, you must specify the explicit pattern. Table 2 lists some examples.

斜线也是特殊的,用来预示一个目录,文件匹配不会匹配一个slash。因为slash永远不是filename 的一部分。

Table 2
Filename Expansion with directories
Pattern Matches
* All non-invisible files
abc/* All non-invisible files in directory abc
abc/.* All invisible files in directory abc
*/* All non-invisible files in all subdirectories below
*/.* All invisible files in all subdirectories below

Filename expansions are based on the current directory, unless the filename starts with a slash.

The Bourne shell differs from the C shell if the meta-characters do not match any file. If this happens, the pattern is passed to the program unchanged. (The C shell will either do this, or generate an error, depending on a variable).

If you are not sure how something will expand, use the echo command to check. It generates output more compact than ls, and it will not list contents of directories like ls will. You will also notice the output is sorted alphabetically. The shell not only expans filenames, but sorts them, for all applications.

转自:http://www.grymoire.com/Unix/Sh.html

Linux 文件名匹配的更多相关文章

  1. Linux文件名匹配和输出重定向--2019-4-24

    1.文件名匹配 例:rm *.bak;   删除结尾是.bak的文件 rm *4*.bak; 删除名称中有“4”的.bak文件 ls *.????; 查找结尾是“.”后有四个字母的文件 2.管道 先进 ...

  2. linux文件名匹配——通配符使用

    背景:在linux使用过程中,经常需要查找文件,对命令中的通配符pattern和正则表达式的区分不是很清楚.有必要好好研究一下. 1 扫盲 1.1 通配符和正则表达式 当在使用命令行时,有很多时间都用 ...

  3. linux文件名匹配

    *   匹配文件名中的任何字符串,包括空字符串. ? 匹配文件名中的任何单个字符. [...]   匹配[ ]中所包含的任何字符. [!...]   匹配[ ]中非感叹号!之后的字符. 如: s*   ...

  4. Linux文件名小写的好处(转)

    说明:来自老阮的<为什么文件名要小写>的文章,其实我觉得应该说是<Linux文件名为什么要小写>会更合适些. 一.可移植性 Linux 系统是大小写敏感的,而 Windows ...

  5. linux文件名乱码时删除或改名的方式(转载)

    转自:http://www.linuxsa.cn/when-linux-file-name-topsy-turvy-deleted-or-renamed.html linux文件名乱码时删除或改名的方 ...

  6. iconv内容,convmv文件名,unix2dos,dos2unix文件格式转换,od/cut/wc/dd/diff/uniq/nice/du等命令,linux文件名乱码,文件名,文件内容,vim编码设置

    1.enconv文件名编码转换,比如要将一个GBK编码的文件转换成UTF-8编码,操作如下 enconv -L zh_CN -x UTF-8 filename enconv -L GB2312 -x  ...

  7. linux文件名乱码解决办法

    1.linux解压压缩文件乱码 unzip -O CP936 xxx.zip 2.一般文件用convmv sudo convmv -f gbk -t utf-8 -r --notest /your_d ...

  8. 正则表达式-linux路径匹配

    转载请注明出处:https://www.cnblogs.com/wenjunwei/p/9860994.html 需求 注:此正则只用于规范linux路径格式. linux路径支持很多的特殊字符,但是 ...

  9. linux查看匹配内容的前后几行(转)

    linux系统中,利用grep打印匹配的上下几行   如果在只是想匹配模式的上下几行,grep可以实现.   $grep -5 'parttern' inputfile //打印匹配行的前后5行   ...

随机推荐

  1. Android中ListView通过BaseAdapter实现数据的绑定

    1. public class ListFiles extends Activity { ListView Listview=null; protected void onCreate(Bundle ...

  2. eclipse 解决插件失效

    昨天系统崩溃,重装系统后eclipse突然对links方式加载插件失效.用尽了网上各种解决方法,始终不行.在%eclispe_dir%/configration/org.eclipse.update/ ...

  3. javascript 实现分享功能

    1.面向过程分享 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  4. json对象转字符串与json字符串转对象

    1.概述: 我们在编程时进场会遇到json对象转字符串,或者字符串转对象的情况. 2.解决办法: json.parse()方法是将json字符串转成json对象. json.stringfy()方法是 ...

  5. Python 线程池的原理和实现及subprocess模块

    最近由于项目需要一个与linux shell交互的多线程程序,需要用python实现,之前从没接触过python,这次匆匆忙忙的使用python,发现python确实语法非常简单,功能非常强大,因为自 ...

  6. 浙江大学PAT上机题解析之5-05. QQ帐户的申请与登陆

    实现QQ新帐户申请和老帐户登陆的简化版功能.最大挑战是:据说现在的QQ号码已经有10位数了. 输入格式说明: 输入首先给出一个正整数N(<=105),随后给出N行指令.每行指令的格式为:“命令符 ...

  7. HID Keyboard & Mouse descriptor.

    在USB中,USB HOST是通过各种描述符来识别设备的,有设备描述符,配置描述符,接口描述符,端点描述符,字符串描述符,报告描述符等等.USB报告描述符(Report Descriptor)是HID ...

  8. rpm包安装

    RPM全称是“RedHatPackageManager”是由RedHat公司发发展起来的,本质是将软件源码包经过编译并且打包成rpm的格式,rpm文件包含的有二进制文件,配置文件,库文件等,同时RPM ...

  9. 部署hibernate框架项目时出现问题:The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files.

    基本情况: (这些其实关系不大)我是直接impor导入HibernateDemo项目到eclipse中的,该项目的hibernate版本是3.6.7.Final版,使用了Hibernate Tools ...

  10. 使用VS Code开发Angular 2应用程序所需配置文件的解析

    目录 package.json typings.json tsconfig.json launch.json settings.json tasks.json package.json: 这是项目的基 ...