Metacharacters
We often want to refer to a group of files or directories that have a common characteristic. To do this, there are some small constructs which helps the user to filter the needed files. For example if in a folder we have a lot of jpg, mp3 and doc files and we would like to list only the doc files, in order to do this we will have to filter the files:
The command is: ls -l *.doc
But there are a lot of useful filters that will help us a lot.
* – replaces one or more characters:
ls -d /Desktop/x* = lists all folders from /Desktop whose names is starting with x and have at least one character;
ls -l /home/*/Desktop = lists the contents of the /Desktop directory for all users in the system;
? – replaces only one character:
ls *.??? – lists all files from the current directory that have an extension with 3 characters;
ls -d /Desktop/??x – lists all folders from desktop whose name has 3 characters and ends with x;
[…] – indicates a block/structure/construct. This block only replaces a single character:
ls -l test[123].txt – lists the details of test1.txt, test2.txt and text3.txt, that are in the current directory;
ls -l /Desktop/music1[56].mp3 – lists the details of music15.mp3 and music16.mp3 that are inside or /Desktop directory;
When you want to use these blocks with successive characters like a, b, c, d, e or 1, 2, 3, 4, 5, 6 you can also write them like this: [a-e] and [1-6];
If you want to negate the characters (any other characters but the ones that are inside of the block), you will have to use ! or ^ at the start of the block: [^a-e] and [!1-6];
Predefined character classes:
[[:alnum:]] – Alphanumeric characters: [[:alpha:]] and [[:digit:]]; in the ‘C’ locale and ASCII character encoding, this is the same as ‘[0-9A-Za-z]’;
[[:alpha:]] – Alphabetic characters: [[:lower:]] and [[:upper:]]; in the ‘C’ locale and ASCII character encoding, this is the same as ‘[A-Za-z]’;
[[:blank:]] – Blank characters: space and tab;
[[:cntrl:]] – Control characters. In ASCII, these characters have octal codes 000 through 037, and 177 (DEL). In other character sets, these are the equivalent characters, if any;
[[:digit:]] – Digits: 0 1 2 3 4 5 6 7 8 9;
[[:graph:]] – Graphical characters: [[:alnum:]] and [[:punct:]];
[[:lower:]] – Lower-case letters; in the ‘C’ locale and ASCII character encoding, this is a b c d e f g h i j k l m n o p q r s t u v w x y z;
[[:print:]] – Printable characters: [[:alnum:]], [[:punct:]], and space;
[[:punct:]] – Punctuation characters; in the ‘C’ locale and ASCII character encoding, this is ! ” # $ % & ‘ ( ) * + , – . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~;
[[:space:]] – Space characters: in the ‘C’ locale, this is tab, newline, vertical tab, form feed, carriage return, and space. See Usage, for more discussion of matching newlines;
[[:upper:]] – Upper-case letters: in the ‘C’ locale and ASCII character encoding, this is A B C D E F G H I J K L M N O P Q R S T U V W X Y Z;
[[:xdigit:]] – Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f;