Managing filenames with path expansion

May 17, 2021

In this tutorial, we will see the meta-characters and expansion technique that can improve file management efficiency.

File globbing

The bash shell can match a pathname based on some meta-characters. This feature is known as file globbing. The pathname matching capability was historically called globbing (also known as pattern-matching or wildcard matching) for managing a large number of files. It expands the meta-characters used in filenames and pathnames.

When using file globbing for matching filenames or pathnames, the meta-characters are replaced by the list of matching pathnames prior to command execution. The following table lists the pattern and its corresponding result:

Pattern

Result

?

Matches any single character

*

Matches any string of characters (0 or more)

[set]

Matches any character in the set; for example, [akl] will match any single occurrence of a, k, l

[!set] or [^set]

Matches any character not specified in the set of characters

~

Matches the current user’s home directory (known as tilde expansion)

~username

Matches username’s home directory

Wildcard expansion

To search for a file using the ?wildcard, replace each unknown character with ?, as shown in this example:

  • $ ls /etc/???.conf will listthe conf files with names that consist only of three characters

To search for files using the *wildcard, replace the unknown string with *, as shown in this example:

  • $ ls /etc/*.conf will list all conf files inside the /etc/ directory

The result of the preceding command will look as follows:

filenames

Tilde expansion

The tilde character, ~, if immediately followed by a forward slash,/, matches the current user’s home directory, as shown in the screenshot:

filenames

The tilde character, ~, if immediately followed by a string and then a forward slash, /, matches the username as specified in the string, as shown in the following screenshot:

filenames

Brace expansion

Brace expansion generates a set of strings of characters. Inside braces, we specify a comma-separated list of strings or a sequence expression, which is either preceded or followed by text to append in the string supplied between braces, as shownin the following screenshot:

filenames

Command substitution

Command substitution allows the output of a command to be stored in a variable; the command can be replaced by itself. There are two methods for performing command substitution:

  • Enclosing the command within backticks, as in`command`. Use of backticks is an oldermethod, and has two disadvantages:
  • Backticks are sometimes confusedwithsingle quotation marks
  • Backticks cannot be nested inside other backticks, so nested command substitution is not possible
  • Enclosing the command with an initial dollar sign and parentheses, as in $(command). This overcomes the disadvantages of backticks when used in command substitution as shown in the following screenshot:
filenames

Quoting and escaping

There are certain characters in bash shell that have special meaning, they are also known as meta-characters. Bash meta-characters enhance the flexibility, power and usage of bash. Examples of meta-characters are asterisk (*), question mark (?), hyphen (-), exclamation (!), and so on. 

  • Escaping: The backslash (\) is an escape character that protects the special interpretation of the character immediately followed
  • Quoting: To protect longer character strings, enclose them inside single (') or double (") quotes
  • Double quotes (): Using double quotes suppresses globbing and shell expansion; however, it allows command and variable substitution, as shown in the following screenshot:
filenames
  • Single quotes (‘): Using single quotes suppresses all kinds of expansion, and everything within single quotes is treated as literal characters without any special meaning, as shown in the following screenshot:

filenames

File naming conventions

A file or folder name is a string used to identify a file. It can consist of 255 characters, including alphabetical letters, numbers, and special characters, excluding (/), which is used as the directory separator. You can include special characters (meta-characters); however, use of certain characters in filenames is not advisable, including '"$#!, and so on, because of their special interpretation by the shell.

Linux is case-sensitive as well as space-sensitive, so filenames are also case-sensitive. This means if we create files with the name data.txt and Data.txt in the same directory, then it results in two unique files.

Note:

The space is an acceptable character in Linux filenames; however, a space is also used as a delimiter by the command shell for command-line syntax interpretation, so it is generally not advised touse spaces in filenames in case this leads to ambiguity when executing commands.

Related Articles

How to Install MongoDB on CentOS 8 Linux

How to Install MongoDB on CentOS 8 Linux

MongoDB is an open-source and free NoSQL document based database system. Nowadays, it is prevalent as most application developers are using MongoDB, which can handle big data. As a MongoDB is a NoSQL database, It stores data in JSON-like documents where fields can...

read more
How to Install OpenCV on CentOS 8 Linux

How to Install OpenCV on CentOS 8 Linux

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning software library. OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in commercial...

read more

Lorem ipsum dolor sit amet consectetur

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

20 − six =