The most powerful feature of the Linux Bash shell is its ability to work with files and redirect their input and output extremely efficiently. Linux uses special characters or symbols called metacharacters that complement shell commands related to file searches and command connections.
Join the channel Telegram belong to AnonyViet π Link π |
Superscripts are useful in listing, deleting, and copying files on Linux. However, the function of each superscript differs depending on the command you are using.
This article provides an in-depth understanding of the different types of superscripts in Linux. And I will explain how these special characters help connect and expand commands.
Handle files with superscripts
The Linux shell allows you to save keystrokes when entering commands by using superscripts between file or directory names. These characters help you refer to a group of files or a folder to list, move, or perform other actions.
Here are some superscripts in the Linux Shell:
- * (Asterisk): Matches one or more occurrences of a character
- ? (Question mark): Matches a single character or a pattern occurrence
- [] (Square brackets): Match any numbers, symbols, or hyphen-separated alphabets specified inside square brackets
An ideal way to practice superscript in Linux is to create a new empty folder inside the folder /tmp.
sudo mkdir /tmp/meta
Now access the folder /tmp/meta by command CD and create new empty files using command touch:
touch apple.txt cider.sh vinegar.php cat.txt lemon.txt juice.sh catfish.sh
Use the following commands to test the super character β*β and display the output:
ls c* Output: catfish.sh cat.txt cider.sh
ls c*h Output: catfish.sh cider.sh
ls *r* Output: cider.sh vinegar.php
sudo rm *p*
The above command will delete all files containing the character βpβ in its name. You can confirm the change with the command ls:
ls Output: catfish.sh cat.txt cider.sh juice.sh lemon.txt
Here are some examples of the β?β sign:
ls a?* Output: apple.txt
ls c?t* Output: catfish.sh cat.txt
The last command matches any file that begins with a letter c and there are words t is the third letter (cat.txt, catfish.sh,β¦). Now use options [av]* with ls command to list all files starting with a or v:
ls [av]* Output: apple.txt vinegar.sh
You can modify the above command to list only files that end with letters t:
ls [ac]*[t] Output: apple.txt catfish.txt cat.txt
Similarly, you can use letters separated by hyphens to scope and list files like this:
ls [a-j]* Output: apple.txt catfish.sh cat.txt cider.sh juice.sh
Superscript file redirection
To better understand redirection in Bash, every process in Linux has file descriptors, called standard input (stdin/0), standard output (stdout/1), and standard error ( stderr/2). They determine the origin of command input and decide where to send output and error messages.
Superscripts help you modify these actions by I/O content stream redirection. In general, the Linux shell reads command input from the keyboard and writes the results to the screen. Input redirection allows the command to read content from a file instead of the keyboard, and output redirection saves the output to the file.
In other words, the Linux file redirection superscript allows you to redirect content to (>) and from (<) files. The three main navigation superscripts are:
first. <: Convert file content to command. For example: command output for less .bashrc like less < .bashrc.
2. >: Pipe command output to file. Command ls /etc > lists.txt will save the output to file lists.txt.
3. >>: Add command output to file content.
wc stands for word count and you can use it to show the difference between the file before and after attaching it to the output.
The superscript expands curly braces
The curly brace expansion metacharacter allows you to expand characters across directories, filenames, or other command parameters. For example, you can create a new folder inside the folder /tmp and create a set of files using the touch command as follows:
sudo mkdir /tmp/brace; cd /tmp/brace touch test{1,2,3,4,5}
Now you can check whether touch has created the file or not using the ls command.
ls Output: test1 test2 test3 test4 test5
You can specify multiple lists to generate file names based on a combination of elements in the list. For example:
touch {apple,cider,vinegar}.{fruit,liquid,sour} touch {a,b,c}.{1,2,3}
The last command will create the following files in the current directory:
The first command uses two sets of curly braces to link the filenames in each set to the other. You can also write the final command by touch {a..c}. {1..3} to specify the range between a and c and 1 and 3.
In addition to creating files, you can also use curly braces to delete or copy files to other locations.
Some other Linux superscripts
Below is a table of some superscripts you must know to connect commands with their names, descriptions and examples for practice:
Name | Describe | For example |
---|---|---|
Pipe (|) | Connect command output as input to another command. | cat /etc/passwd | grep root |
Semicolon (;) | Allows executing commands sequentially, one after another. | cd /etc ; ls -la ; chmod +x /tmp/script.php |
Ampersand (&) | Run processes or commands in the background. | find / -perm -u=s -type f & |
Dollar ($) | Expand the arithmetic expression and pass it to the shell | echo βtotal files in this directory are: $(ls | wc -l)β |
Null Redirection (2>) | Direct standard error messages to the file /dev/null | your_command 2>/dev/null |
Circumflex (^) | Matches any pattern that begins with an expression followed by ^ | cd /etc/ssh ; ls | grep ^s |
Supercharacters on Linux are also known as wildcards that add special meaning to commands and control their behavior. Superscripts optimize user's work performance while working with files/folders and connecting/extending Linux shell commands.
Besides, superscripts are also the building blocks of regular expressions. Additionally, learning about superscripts and how to use them is an important skill to have if you want to become a professional Linux user.
Additionally, you can also see how to write Shell Script in Linux here.