Command Line Metacharacters

3 minutes read

Linux_logo_errorbits.com

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;

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

Related Posts:

To display line numbers in a file in Linux, you can use various commands and methods. Here are a few ways to achieve this:cat command: You can use the cat command along with the -n option to display line numbers before each line in a file. Open the terminal an...
In Bash scripting, command-line arguments allow you to provide input and parameters to a script during execution. Here&#39;s an overview of how to handle command-line arguments in a Bash script:To access the command-line arguments, you can refer to them using ...
To read a specific line from a file in Linux, you can use various commands and techniques. Here&#39;s a textual explanation of the process:Using the sed command: Open the Terminal and navigate to the directory where your file is located. To read a specific lin...
In Bash, you can split a multiple-line output into separate arguments using the read command. Here is how you can accomplish this:Capture the multiple-line output into a variable using command substitution or by assigning the output of a command to a variable....
To run a command in the background in Bash, you can use the following syntax: command &amp; Here, command represents the actual command you want to run. By appending an ampersand (&amp;) to the command, it instructs Bash to run the command in the background.Ru...
To format XML in Linux, you can use various command-line tools. Here are a few commonly used tools:xmllint: xmllint is a command-line tool that is part of the libxml2 library. It can be used to analyze and format XML files. The following command formats an XML...