Wildcards (globbing)
| Wildcard | Meaning | 示例 |
|---|---|---|
* |
匹配任意0或多个字符 | find -name *.txt 匹配任意 x.txt 文件,x 不为空find -name .[!.]* 匹配任意隐藏文件 |
? |
匹配任意1个字符 | a?c.txt 匹配 a1c.txt, abc.txt, a-c.txt 等 |
[characters] |
匹配范围里的任一字符 | [abc] , [0-9] , [a-z] , [A-Z] , [0-9a-zA-Z] |
[!characters] |
匹配不在范围里的任一字符 | |
[[:class:]] |
匹配属于 class 的任一字符 | |
\\ |
转义字符,使 Wildcard 失效 | a\\?c.txt 只匹配 a?c.txt |
| Class | Meaning | 示例 |
|---|---|---|
[:alnum:] |
Matches any alphanumeric character | [[:alnum:]]*.txt Any txt file beginning with a alphabetic or numeral character |
[:alpha:] |
Matches any alphabetic character | |
[:digit:] |
Matches any numeral | [![:digit:]]*Any file not beginning with a numeral |
[:lower:] |
Matches any lowercase letter | *[[:lower:]123]Any file ending with a lowercase letter or the numerals 1, 2, or 3 |
[:upper:] |
Matches any uppercase letter |
mkdir
mkdir mydir1 mydir2mkdir -p mydir/dir1/dir2 # 如果 parent 文件夹不存在,则自动创建
cp
Options & Arguments
cp [OPTION]... [-T] SOURCE DEST cp [OPTION]... SOURCE... DIRECTORY cp [OPTION]... -t DIRECTORY SOURCE...
| Option | Long option | Description |
| —- | —- | —- |
| -a | —archive | Copy the files and directories and all of their attributes, including ownerships and permissions. Normally, copies take on the default attributes of the user performing the copy. |
| -i | —interactive | Before overwriting an existing file, prompt the user for confirmation. If this option is not specified, cp will silently (meaning there will be no warning) overwrite files. |
| -r | —recursive | Recursively copy directories and their contents. This option (or the -a option) is required when copying directories. |
| -u | —update | When copying files from one directory to another, only copy
files that either don’t exist or are newer than the existing corresponding files in the destination directory. This is useful whe copying large numbers of files as it skips files that don’t need to be copied. |
| | —parents | use full source file name under DIRECTORY |
| -v | —verbose | Display informative messages as the copy is performed. |
Examples
| Command | Results |
|---|---|
cp file1 file2 |
Copy file1 to file2. - If file2 exists, it is overwritten with the contents of file1. - If file2 does not exist, it is created. |
cp -i file1 file2 |
Same as previous command, except that if file2 exists, the user is prompted before it is overwritten. |
cp file1 file2 dir1 |
Copy file1 and file2 into directory dir1. The directory dir1 must already exist. |
cp dir1/* dir2 |
Using a wildcard, copy all the files in dir1 into dir2. The directory dir2 must already exist. |
cp -r dir1 dir2 |
Copy the contents of directory dir1 to directory dir2. - If directory dir2 does not exist, it is created and, after the copy, will contain the same contents as directory dir1. - If directory dir2 does exist, then directory dir1 (and its contents) will be copied into dir2. |
cp --parents /B/1.txt /A/ |
结果为 /A/B/1.txt |
mv
Options & Arguments
mv [OPTION]... [-T] SOURCE DEST mv [OPTION]... SOURCE... DIRECTORY mv [OPTION]... -t DIRECTORY SOURCE...
| Option | Long option | Description |
| —- | —- | —- |
| -i | —interactive | Before overwriting an existing file, prompt the user for confirmation.
If this option is not specified, mv will silently overwrite files. |
| -u | —update | When moving files from one directory to another, only move files that either don’t exist or are newer than the existing corresponding files in the destination directory. |
| -v | —verbose | Display informative messages as the move is performed. |
Examples
| Command | Results |
|---|---|
mv file1 file2 |
Move file1 to file2. - If file2 exists, it is overwritten with the contents of file1. - If file2 does not exist, it is created. |
In either case, file1 ceases to exist. |
| mv -i file1 file2 | Same as previous command, except that if file2 exists, the user is prompted before it is overwritten. |
| mv file1 file2 dir1 | Move file1 and file2 into directory dir1.
The directory dir1 must already exist. |
| mv dir1 dir2 |
- If directory dir2 does not exist, create directory dir2 and move the contents of directory dir1 into dir2 and delete directory dir1.
- If directory dir2 does exist, move directory dir1 (and its contents) into directory dir2.
|
rename (Perl Version)
Options & Arguments
rename [ -h|-m|-V ] [ -v ] [ -n ] [ -f ] [ -e|-E perlexpr]*|perlexpr [ files ]
| Option | Long option | Description |
| —- | —- | —- |
| -n | —nono | No action: print names of files to be renamed, but don’t rename. |
| -f | —force | Over write: allow existing files to be over-written. |
| -v | —verbose | Display informative messages as the rename is performed. |
Examples
| Command | Results |
|---|---|
rename's/.htm$/.html/' *.htm |
Rename all .htm files to .html. |
rename'y/a-z/A-Z/' * |
Translate all lowercase names to uppercase. |
rm
Options & Arguments
rm [OPTION]... [FILE]...
| Option | Long option | Description |
| —- | —- | —- |
| -i | —interactive | Before deleting an existing file, prompt the user for confirmation. If this option is not specified, rm will silently delete files. |
| -r | —recursive | Recursively delete directories. This means that if a directory being deleted has subdirectories, delete them too. To delete a directory, this option must be specified. |
| -f | —force | Ignore nonexistent files and do not prompt. This overrides the
--interactive option. |
| -v | —verbose | Display informative messages as the deletion is performed. |
Examples
| Command | Results |
|---|---|
rm file1 |
Delete file1 silently |
rm -i file1 |
Same as the previous command, except that the user is prompted for confirmation before the deletion is performed. |
rm -r file1 dir1 |
Delete file1 and dir1 and its contents. |
rm -rf file1 dir1 |
Same as the previous command, except that if either file1 or dir1 does not exist, rm will continue silently. |
ln
Hard Links
Hard Links 类似于智能指针~,只有当所有智能指针销毁后,文件才会真正被删除。
Limitations:
- A hard link cannot reference a file outside its own file system. This means a link cannot reference a file that is not on the same disk partition as the link itself.
- A hard link may not reference a directory.
目标文件为 file1.txt, Hard Link 为 file1-hl.txt, 特点:
- file1.txt 和 file1-hl.txt 拥有相同的属性,因为是对同一真实文件的引用
- 删除任一文件,对另一文件没有影响
- 修改任一文件内容,会对另一文件产生同样的修改
Symbolic Links
Symbolic Links 类似于 Windows 里的快捷方式。
目标文件为 file1.txt, Symbolic Link 为 file1-sl.txt, 特点:
- file1-sl.txt 只有 5 字节左右的大小
- 删除 file1.txt 会导致 file1-sl.txt 失效,删除 file1-sl.txt 不会影响 file1.txt
-
Options & Arguments
ln [OPTION]... [-T] TARGET LINK_NAME- Create a link to TARGET with the name LINK_NAME.
ln file1.txt file1-hl.txtln -s file1.txt file1-sl.txt
ln [OPTION]... TARGET- Create a link to TARGET in the current directory.
ln ../1.txt在当前目录里创建一个了 ../1.txt 的硬链接
ln [OPTION]... TARGET... DIRECTORY,ln [OPTION]... -t DIRECTORY TARGET...- Create links to each TARGET in DIRECTORY.
ln -s 1.txt 2.txt ./dir在 ./dir 目录里创建了 1.txt 和 2.txt 的符号链接 | Option | Long option | Description | | —- | —- | —- | | -s | —symbolic | make symbolic links instead of hard links |
file
touch
touch - change file timestamps
