The asterisk (*
) is one of the most powerful and frequently used wildcard characters in Bash. Whether you're listing files, moving directories, or searching for patterns, mastering the use of *
can significantly boost your efficiency in the terminal. This article explores various ways to use *
in Bash, with practical examples to help you get the most out of this wildcard.
1. Wildcard for File and Directory Matching
-
The
*
wildcard matches zero or more characters in filenames. -
Example:
ls *.txt # Lists all files ending with .txt ls file* # Lists all files starting with "file"
2. Using *
in Commands
-
The
*
wildcard works with commands likecp
,mv
,rm
, etc. -
Example:
cp *.jpg backup/ # Copies all .jpg files to the backup directory rm * # Deletes all files in the current directory
3. Wildcard in Recursive Search
-
Used with
find
orgrep
to search files recursively. -
Example:
find . -name "*.sh" # Finds all .sh files in current directory and subdirectories grep "error" *.log # Searches for "error" in all .log files
4. Repeating the Last Argument (!*
)
-
Expands to the last argument of the previous command.
-
Example:
echo Hello World echo !* # Expands to "World"
5. Arithmetic Expansion (*
as a Multiplication Operator)
-
Used in arithmetic operations within
$(( ))
. -
Example:
echo $((5 * 3)) # Prints 15
6. Globbing with Extended Patterns
-
Works with
shopt -s extglob
to enable advanced patterns. -
Example:
shopt -s extglob ls !(file1.txt) # Lists all files except file1.txt
7. Using *
in Variable Expansion
-
Expands filenames matching a pattern into a variable.
-
Example:
FILES=(*.txt) echo "${FILES[@]}" # Prints all .txt files
8. Escaping *
to Treat as a Literal Character
-
Use
\*
or quotes to prevent wildcard expansion. -
Example:
echo "*" # Prints *
9. Using *
in Pathname Expansion
-
Works in paths to match files and directories.
-
Example:
cp ~/Documents/*.pdf ~/Backup/ # Copies all PDFs from Documents to Backup
Advanced Uses of *
in Bash
10. Using *
with apt
Commands
apt list --installed alsa*
-
Lists all installed packages whose names start with
alsa
.
apt install alsa*
-
Installs all packages that start with
alsa
.
11. File Removal Patterns
rm *.exe
-
Deletes all
.exe
files in the current directory.
rm he*.deb
-
Deletes all
.deb
files that start with "he".
12. Excluding Files (rm !(*.c)
)
rm !(*.c)
-
Deletes all files except those ending with
.c
. -
Requires enabling extended globbing:
shopt -s extglob
13. Moving and Copying Files
mv *.txt backup/
cp *.jpg images/
-
Moves all
.txt
files into thebackup/
directory. -
Copies all
.jpg
files into theimages/
directory.
14. Removing Empty Directories (rmdir */
)
rmdir */
-
Removes all empty directories in the current location.
15. Counting Matching Files (ls *.log | wc -l
)
ls *.log | wc -l
-
Counts the number of
.log
files in the current directory.
16. Using *
in tar
Commands
-
Archive all
.log
files:tar -cvf logs.tar *.log
17. Wildcard in rsync
-
Sync all
.jpg
files to a backup folder:rsync -av *.jpg backup/
18. Using *
in echo
for Expansion
echo *.txt
-
Prints a list of all
.txt
files in the current directory.
Final Thoughts
Understanding how the asterisk (*
) works in Bash can make your command-line experience much more efficient. From simple file management to advanced pattern matching, *
is an essential tool for any Linux user. Experiment with these commands and see how they can streamline your workflow!
Comments
Post a Comment