Advanced file expansions and quoting

File name expansions in Linux

In Linux, file name expansion (also known as globbing) is the process of generating a list of file names that match a given pattern or criteria. File name expansion is often used in combination with commands such as ls and rm to operate on a group of files that share a common pattern in their names.

There are several wildcard characters that you can use to specify patterns in file names:

  • *: Matches zero or more characters
  • ?: Matches any single character
  • [characters]: Matches any single character that is listed between the brackets
  • [!characters]: Matches any single character that is not listed between the brackets

Here are some examples of using file name expansion:

  • ls *: Lists all files in the current directory
  • ls *.txt: Lists all files in the current directory with the .txt extension
  • rm *~: Deletes all files in the current directory that end with a ~ character
  • ls file[123].txt: Lists the files file1.txt, file2.txt, and file3.txt in the current directory

You can also use {} to specify a list of patterns to match. For example:

ls file{1,2,3}.txt: Lists the files file1.txt, file2.txt, and file3.txt in the current directory

ls *.{jpg,png,gif}: Lists all files in the current directory with the .jpg, .png, or .gif extension

mv *.{txt,doc} /new/location: Moves all files in the current directory with the .txt or .doc extension to the /new/location directory

History command in bash CLI

The command history is a record of the commands that you have entered at the command prompt. The command history is stored in a file, usually ~/.bash_history, and can be accessed and used to repeat previous commands or to quickly enter frequently used commands.

To view the command history, you can use the history command:

history

This will display a list of the most recently entered commands, along with their line numbers.

To execute a command from the history, you can use the ! followed by the line number of the command:

!42

This will execute the command that is on line 42 of the history.

You can also use the ! followed by a string to execute the most recent command that begins with the string:

!ls

!grep: Executes the most recent grep command from the history

!42: Executes the command that is on line 42 of the history

This will execute the most recent ls command from the history.

You can use the !! command to execute the most recent command:

!!

Quoting in Linux

quotes are used to enclose a string or group of characters, and they are used to preserve the literal value of the characters within the quotes. There are three types of quotes that are commonly used in Linux: single quotes (), double quotes (), and backticks (`). Each type of quote serves a specific purpose, and it is important to understand the differences between them to use them effectively.

Single quotes are used to enclose a string that should be treated literally, without any special interpretation. This means that any variables or special characters within the single quotes will be treated as plain text, and will not be expanded or interpreted in any way. For example:

echo 'This string will be treated literally, $foo will not be expanded'

Double quotes are used to enclose a string that should be treated literally, but with some exceptions. Within double quotes, variables and certain special characters will be expanded or interpreted. For example:

echo "This string will be treated literally, but $foo will be expanded"

Backticks are used to enclose a command that should be executed and replaced with its output. This is known as command substitution. For example:

echo The current date is `date`

This command will execute the date command and replace it with the output of the command, so the final string that is printed will be something like “The current date is Sat Dec 18 2022”.

It is important to use quotes correctly in Linux, as they can affect the way a command is interpreted and how its output is displayed. Using the wrong type of quote can lead to syntax errors or unexpected results.

special characters are characters that have a specific meaning or function within the operating system or shell. These characters include:

  • $: Indicates the beginning of a variable name
  • *: Matches any number of characters
  • ?: Matches any single character
  • [ ]: Matches any single character within the brackets
  • !: Negates the character class if used as the first character within the brackets
  • : Escapes the special meaning of the character that follows it

To quote a special character in Linux, you can enclose it in quotes or use the backslash () to escape its special meaning. For example:

echo 'The * character will be treated literally'
echo "The * character will be treated literally"
echo The * character will be treated literally

In these examples, the * character will be treated as a literal character and will not be interpreted as a wildcard.

It is important to quote special characters correctly, as they can affect the way a command is interpreted and how its output is displayed. Using the wrong type of quote or failing to escape a special character can lead to syntax errors or unexpected results.

Escape characters

An escape character is a special character that is used to indicate that the character that follows it should be treated literally, rather than being interpreted as a special character or command. The backslash () is the most commonly used escape character in Linux.

Here are some examples of how escape characters can be used in Linux:

To escape a special character:

echo "This string contains a * wildcard character"

In this example, the * character will be treated as a literal character and will not be interpreted as a wildcard.

To escape a space character:

cp file name new file name

In this example, the space characters between the words “file”, “name”, “new”, “file”, and “name” will be treated as literal space characters and will not be interpreted as separators between arguments.

To escape a newline character:

echo "This string will span multiple lines"

In this example, the newline character after “This string” will be treated as a literal newline character and will not create a new line in the output.

Escape characters can be used in various contexts in Linux, such as in shell scripts, commands, and configuration files. They are useful for preserving the literal value of special characters and for specifying characters that would otherwise be interpreted in a special way.

Brace expansion

brace expansion is a feature of the shell that allows you to generate a list of strings by specifying a pattern using braces ({}). The shell will then expand the pattern to create a list of strings that match the pattern.

Here are some examples of brace expansion:

  • {1..5}: Expands to a list of the numbers 1 through 5.
  • {a..e}: Expands to a list of the letters a through e.
  • {a..e..2}: Expands to a list of the letters a, c, and e.
  • {a,b,c}: Expands to a list of the strings a, b, and c.

You can use brace expansion to generate lists of strings that you can use in your scripts or on the command line.

For example, you might use brace expansion to generate a list of filenames:

# Generate a list of filenames
touch file{1..5}.txt

This would create five files named file1.txt, file2.txt, file3.txt, file4.txt, and file5.txt.

You can also use brace expansion in combination with other shell features, such as wildcards and command substitution, to create more complex patterns.

For example, you might use brace expansion to generate a list of directories and then use find to search for files within those directories:

# Generate a list of directories
dirs=$(echo dir{1..5})
# Search for files within the directories
find $dirs -name "*.txt"

Brace expansion can be a useful tool for generating lists of strings in your scripts and on the command line.

brace expansion is a feature of the shell that allows you to generate a list of strings by specifying a pattern using braces ({}). The shell will then expand the pattern to create a list of strings that match the pattern.

Here are some examples of brace expansion:

  • {1..5}: Expands to a list of the numbers 1 through 5.
  • {a..e}: Expands to a list of the letters a through e.
  • {a..e..2}: Expands to a list of the letters a, c, and e.
  • {a,b,c}: Expands to a list of the strings a, b, and c.

You can use brace expansion to generate lists of strings that you can use in your scripts or on the command line.

For example, you might use brace expansion to generate a list of filenames:

# Generate a list of filenames
touch file{1..5}.txt

This would create five files named file1.txt, file2.txt, file3.txt, file4.txt, and file5.txt.

You can also use brace expansion in combination with other shell features, such as wildcards and command substitution, to create more complex patterns.

For example, you might use brace expansion to generate a list of directories and then use find to search for files within those directories:

# Generate a list of directories
dirs=$(echo dir{1..5})
# Search for files within the directories
find $dirs -name "*.txt"

Brace expansion can be a useful tool for generating lists of strings in your scripts and on the command line.


Summary of Advanced file expansions and quoting:
File name expansion in Linux is a process that generates a list of file names based on a given pattern or criteria. This is often used in conjunction with commands such as ls and rm to operate on a group of files that share a common pattern in their names. Wildcard characters, such as *, ?, and [], can be used to specify patterns in file names. The history command in Bash is a record of commands that have been entered at the command prompt, and it is stored in a file (usually ~/.bash_history). The history command can be used to view and execute previous commands. Quoting in Linux involves enclosing a string or group of characters in quotes to preserve their literal value. There are three types of quotes commonly used in Linux: single quotes ('), double quotes ("), and backticks (`). Single quotes preserve the literal value of a string without any special interpretation, double quotes preserve the literal value with some exceptions, and backticks execute a command and replace it with its output. Special characters in Linux are characters that have a special meaning or purpose in the shell, such as $ and |. They can be used in commands and scripts to perform various tasks and operations.

License

Developers ultimate guide: Linux Bash scripting Copyright © 2022 by Matin Maleki. All Rights Reserved.

Share This Book