CIS120Linux Fundementals
Linux Expansion
In Linux, expansion is the process where the shell takes certain special characters or patterns in commands and translates them into a more concrete form before executing the command. This allows you to use shortcuts or patterns to save time and effort when interacting with the system. There are several types of expansions in Linux that you’ll use often, including pathname expansion, tilde expansion, arithmetic expansion, brace expansion, parameter expansion, and command substitution. Let's explore these one by one.
Pathname Expansion (Wildcard Expansion)
Pathname expansion, or wildcard expansion, is when the shell interprets special characters like *
and ?
to match files and directories.
*
matches any number of characters.?
matches exactly one character.[abc]
matches any character from the seta
,b
, orc
.
Examples:
This will list all files and directories in the current working directory.
echo *
This will list all files in the current directory with a .txt
extension.
echo *.txt
This will match files like file1.txt
, fileA.txt
, but not file10.txt
(because ?
only matches one character).
ls file?.txt
This will match all .jpg
files whose names start with any letter between a
and c
.
ls [a-c]*.jpg
Tilde Expansion
Tilde expansion uses the ~
symbol to represent the current user’s home directory, or another user’s home directory.
Examples:
Changes to your home directory.
cd ~
Lists the contents of your Documents
folder within your home directory.
ls ~/Documents
Expands to the home directory of another user named username
. This will just print the expanded path to the terminal
echo ~username
Copies a file from your Downloads
directory to your Documents
directory.
cp ~/Downloads/file.txt ~/Documents/
Arithmetic Expansion
Arithmetic expansion lets you perform mathematical operations directly in your commands. It uses the format $((expression))
, where expression
is a mathematical formula.
Examples:
Outputs 8
by adding 5 and 3.
echo $((5 + 3))
Outputs 7
by subtracting 2 from 9.
echo $((9 - 2))
Outputs 12
by multiplying 2 and 6.
echo $((2 * 6))
Outputs 5
by dividing 10 by 2.
echo $((10 / 2))
Follows the correct order of operations and outputs 13
(5 * 2 = 10, then 3 + 10 = 13).
echo $((3 + 5 * 2))
The parentheses ensure that 5 + 3 is evaluated first (resulting in 8), then multiplied by 2 to get 16.
echo $(( (5 + 3) * 2 ))
Brace Expansion
Brace expansion generates arbitrary strings by expanding text enclosed in braces {}
. This is very useful for creating sequences of similar file or directory names.
Examples:
Expands to fileA.txt
, fileB.txt
, fileC.txt
.
echo file{A,B,C}.txt
Creates directories folder_1
, folder_2
, ..., folder_5
.
mkdir folder_{1..5}
Creates files image_001.png
, image_002.png
, ..., image_005.png
.
touch image_{001..005}.png
Expands to project_alpha_test
, project_beta_test
, and project_gamma_test
.
echo project_{alpha,beta,gamma}_test
Parameter Expansion
Parameter expansion retrieves the value of variables and allows for operations on those variables. Variables in Linux can store data like strings or numbers and are often used in scripts.
Examples:
Displays the value of the HOME
variable, which is your home directory path.
echo $HOME
Displays the value of the USER
variable, which is your current username.
echo $USER
Displays the list of directories where the system searches for executable files.
echo ${PATH}
You can also modify variables using parameter expansion. For instance, setting a default value if a variable isn’t set:
echo ${UNSET_VARIABLE:-default_value}
This outputs default_value
if UNSET_VARIABLE
is not defined.
Command Substitution
Command substitution allows the output of one command to be used as an argument in another command. The syntax is either $(command)
or `command`
.
Examples:
This will print the current date by substituting the output of the date
command.
echo "The current date is: $(date)"
This prints a list of files in the current directory.
echo "Files in the directory: $(ls)"
This counts and prints the number of files in the current directory by combining ls
and wc -l
(which counts lines).
echo "Number of files: $(ls | wc -l)"
More Examples of Combining Expansions
You can also combine different types of expansions to perform more complex tasks. Here are some useful Examples:
Combining Brace Expansion and Tilde Expansion:
mkdir ~/projects/{project1,project2,project3}
This creates three directories (project1
, project2
, and project3
) inside the projects
folder in your home directory.
Combining Arithmetic Expansion and Command Substitution:
echo "The result of 5 + 10 is $((5 + 10))"
Outputs The result of 5 + 10 is 15
.
Combining Command Substitution with Pathname Expansion:
echo "Today's log file: log_$(date +%Y%m%d).txt"
Creates a dynamic filename for the log based on today’s date, for example, log_20240925.txt
.
Conclusion
Linux expansions allow you to write more powerful and efficient commands by automating common tasks like listing files, performing calculations, creating multiple directories or files, and substituting commands within other commands. As you become familiar with expansions like pathname, tilde, arithmetic, brace, parameter expansion, and command substitution, you will find yourself saving time and effort when working on the command line. This flexibility is one of the reasons why Linux is such a powerful system for both casual users and professionals.