Command Line Metacharacters

3 minutes read


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;

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

Installing Packages/Applications/Software The yum (apt-get or apt in Ubuntu) command is used to install new packages and to update/upgrade the operating system, it requires administrative access, so it must be used with sudo. yum [OPTIONS] [COMMAND] apt-get [O...
Before you start this tutorial is better to know how to get help and details about the commands, when you need it. There are several ways to get help about the commands, but I will present only the most important ones: Using –help as argument for the command E...
Tidal subscribers have a new way to listen to the high-fidelity music streaming service while using the Linux desktop. The Spotify rival touts better sound Source: Listen to Tidal Music from the Command Line – OMG! Ubuntu!
grep command is a text filter that searches for the input and returns the text lines by a given pattern. In order to start the example, use the following command to get to the /Documents/ folder: cd ~/Documents
Each Linux/Unix command has 3 communication channels: input, output and error. The output can be filtered and then redirected and by this way parts of it can be captured, depending on the needs. Redirecting the output to a file, using &gt; or &gt;&gt;: &gt; – ...
The ifconfig command is used to view the current network configuration for ethernet cards and iwconfig displays the network configuration for wireless cards. Because this is the output from a Virtual Machine the ethernet is called enp0s3, on hardware machines ...