$ ls /dev/ram?? ← List "/dev/ram" followed by any two characters of files /dev/ram10 /dev/ram11 /dev/ram12 /dev/ram13 /dev/ram14 /dev/ram15 $ cd /bin ← Enter the directory /bin $ ls l? ← List the files whose first word is "l" (lowercase L) and the second word is any single character ln ls $ ls ?o?? ← List the 1st, 3rd, and 4th characters as any character, and the 2nd word is "o" files more sort $ ls ??? ← List the files with length=3 awk cat cut env pwd red rpm rvi sed tar $ ls ?ed ← list the first word as any character, followed by "ed" file red sed |
$ mv l? /tmp $ cp ch?.html /backup $ rm ???.txt |
$ ls /dev/ram* ← List "/dev/ram" followed by a string of any length /dev/ram /dev/ram10 /dev/ram13 /dev/ram2 /dev/ram5 /dev/ram8 /dev/ram0 /dev/ram11 /dev/ram14 /dev/ram3 /dev/ram6 /dev/ram9 /dev/ram1 /dev/ram12 /dev/ram15 /dev/ram4 /dev/ram7 /dev/ramdisk $ cd /etc ← Enter the directory /etc $ ls -d c*/ ←list the directories starting with "c" and followed by any name directory cron.d/ cron.daily/ cron.hourly/ cron.monthly/ cron.weekly/ cups/ $ ls *.cfg ←List all files with the extension name "cfg" (the file name after "." is the extension name) a2ps.cfg a2ps-site.cfg enscript.cfg $ ls -d *-* ←List all files with "-" in the file name(the first or last character of the file name is "-" will also be established) a2ps-site.cfg gnome-vfs-mime-magic jvm-commmon shadow- bonobo-activation gpm-root.conf lsb-release.d system-release dbus-1 group- passwd- fedora-release gshadow- redhat-lsb gnome-vfs-2.0 gtk-2.0 redhat-release $ ls s*s ← list "s "All files beginning and ending with services shells sudoers |
wildcards | matching profile | example |
* | Any file. Except hidden files at the beginning of "." | ls * |
*.* | Any file name with ".", such as "readme.txt", "abc.", etc. but not including the file at the beginning of ".", such as ".Trash" | cp *.* /tmp |
.* | Any hidden file | rm .* |
.*/ | Any hidden directory ("/" is the directory at the end of the file name, and it will appear with ls -p or ls -F ) | ls .*/ |
*/ | Any directory, but does not include hidden directories starting with "." | mv */ /tmp |
$ ls -d .*/ ←List only hidden directories $ ls -d .* ←List only hidden files $ rm *. ←Delete only hidden files $ rm -rf * ←Delete all files and directories in the working directory, But does not include hidden files $ rm -r */ ← Delete all directories in the working directory, but does not include files and hidden directories |
$ echo iphone{3g,5,5c}, ← expand to list "3g", "5", and "5c" iphone3g, iphone5, iphone5c, $ echo iphone {3g,5,5c} , ← before and after "{ }" There is a difference in the output of characters with or without spaces. When there are spaces, the characters before and after the characters will not be expanded iphone 3g 5 5c , $ ls -d {.*,.*/} ← only list hidden files and hidden directories $ rm -fr {.*/,.*,*} ←Delete all files in the working directory, including hidden files and hidden directories (dangerous action) $ cp -v /bin/{*ee*,*dd*} /tmp Copy files matching wildcard patterns "*ee*" and "*dd*" `/bin/sleep' -> `/tmp/sleep' `/bin/usleep' -> `/tmp/usleep' `/bin/dd' -> `/tmp/dd' |
$ ls /dev/tty[14] ←list the files of tty1 or tty4 in the "/dev" directory /dev/tty1 /dev/tty4 $ ls /dev/tty[123][45] /dev/tty14 /dev/tty15 /dev/tty24 /dev/tty25 /dev/tty34 /dev/tty35 $ ls /dev/l[op]? /dev/log /dev/lp0 /dev/lp1 /dev/lp2 /dev/lp3 |
$ LANG=en_US.UTF-8 ←Set the encoding of the locales to "en_US.UTF-8" (different locales results may be different) $ ls /usr/bin/t[b-Y]e ←list "t" and "e", the middle is the file between "b to Y" /usr/bin/tee /usr/bin/tie /usr/bin/toe $ ls /usr/bin/t[Y-b]e ← invalid representation, because The matching filter range must be from small to large ls: cannot access t[Y-b]e: No such file or directory $ ls /usr/bin/icc[7-9i-t1-2]* ←the 4th character, range From "7~9" or "i~t" or "1~2" as long as one of them is established /usr/bin/icc2ps /usr/bin/icclink /usr/bin/icctrans |
$ ls /dev/tty[!14] dev/tty0 /devtty3 /devtty6 /devtty8 dev/tty2 /devtty5 /devtty7 /devtty9 $ ls /usr/bin/t[!o-p]e /usr/bin/tee /usr/bin/tie $ ls /usr/bin/icc[^2]* /usr/bin/icclink /usr/bin/icctrans |
POSIX Character matches characters | ||
POSIX | ASCII | Illustrate |
[:alnum:] | [A-Z,a-z,0-9] | English letters and numbers |
[:alpha:] | [A-Z,a-z] | Alphabet |
[:blank:] | Space(ASCII = 20H) and TAB(ASCII = 9H) | |
[:cntrl:] | [0H-1FH,7FH] | Control character |
[:digit:] | [0-9] | Mumber |
[:graph:] | [21H-7EH] | Characters that will be displayed |
[:upper:] | [A-Z] | Uppercase letter |
[:lower:] | [a-z] | Lower case letters |
[:print:] | [20H-7EH] | Characters that will be displayed + spaces |
[:punct:] | [\]\[!"#$%&'()*+,./:;<=>?@\^_`{|}~-] | Punctuation and symbols |
[:space:] | [ \t\r\n\v\f] | Whitespace (characters not displayed) |
[:xdigit:] | [A-Fa-f0-9] | Character in hexadecimal |
$ ls -d /etc/[[:upper:]]* ← List files whose first character is uppercase /etc/ConsoleKit /etc/DIR_COLORS.xterm /etc/Muttrc.local /etc/PolicyKit /etc/DIR_COLORS /etc/Muttrc /etc/NetworkManager /etc/X11 |
$ echo > abc ← Create a file with a length of three characters $ ls ??? ← Verify abc $ echo > -12 ← Create a file "-12" (also three characters in length) $ ls ??? ← Verify ls: invalid option -- 2 ←??? Invalid Try `ls --help' for more information. $ ls *←List all files ls: invalid option -- 2 Bizarre incident? wildcards completely fail. $ rm -f ./-12 ←Kill this ghost file $ ls ??? ←Verify abc ←Normal |
$ rename IMAGE keelung IMAGE* ←Rename the file IMAGE0001.jpg ~ IMAGE0050.jpg to keelung0001.jpg ~ keelung0050.jpg |
$ rename IMAGE00 keelung00 IMAGE00[0-2]?.* ←Rename all files IMAGE0001.jpg ~IMAGE0029.jpg to keelung0001.jpg~keelung0029.jpg, and keep the rest unchanged |
$ rename .config .cfg *.config ←Change the extension ".config" to ".cfg" in all working directories $ rename - _ *-* ←Change the minus sign "-" in the file name of all files Change to underscore"_" |
$ rename ~/catlog ~/list ~/catlog?? ←in the home directory,Rename all directories from "catlog00"~"catlog99" rename to "list00"~"list99" |
$ rename abc wxyz * ← Rename "abc" to "wxyz" |
$ LANG= ←To clear all configured locales (equivalent to "LANG=C" or "LANG=POSIX") $ LANG=en_US.UTF-8←Set locales to "en_US.UTF-8" |