Certainly! Below are the solutions for each task in your practice exercise on shell expansions in the Linux command line.
Objective: Use simple expansion to echo the current user and home directory.
Solution:
echo "Current user: $USER, Home directory: $HOME"
This command uses variable expansion to print the current username and home directory.
Objective: Use pathname expansion to list all .txt
files in the current directory.
Solution:
ls *.txt
Using the *
wildcard, this command lists all .txt
files in the current directory.
Objective: Use tilde expansion to navigate to the home directory and then to a sub-directory Documents
within it, all in a single command.
Solution:
cd ~/Documents
The tilde (~
) expands to the home directory, so this command changes the current working directory to Documents
within the home directory.
Objective: Create directories for each week in a course that lasts 12 weeks. The directories should be named Week1
, Week2
, ..., Week12
.
Solution:
mkdir Week{1..12}
Brace expansion makes it easy to create multiple directories with a single command. This creates 12 directories, from Week1
to Week12
.
Objective: List all .txt
and .pdf
files in the current directory using pathname expansion.
Solution:
ls *.{txt,pdf}
This command uses brace expansion within pathname expansion to list all files that have either a .txt
or .pdf
extension.
Objective: Create a variable named greeting
with the value Hello
. Use parameter expansion to echo the value of this variable.
Solution:
greeting="Hello"
echo $greeting
This sets a variable greeting
with the value "Hello" and then uses parameter expansion to echo its value.