Skip to main content

Mastering the Asterisk (*) in Bash: A Comprehensive Guide


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 like cp, 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 or grep 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 the backup/ directory.

  • Copies all .jpg files into the images/ 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

Popular posts from this blog

Installing Xray on Ubuntu VPS

Xray is a platform that supports multiple proxy protocols such as VLESS, VMess, Shadowsocks, and Trojan. Below are the steps to install Xray on an Ubuntu VPS and set up a client connection. Installing Xray on Ubuntu VPS Update the System sudo apt update && sudo apt upgrade -y Install Required Dependencies sudo apt install -y curl wget unzip socat Install Xray Using the Installation Script  Run the following script to install the latest version of Xray: bash <(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh) This script will download, install, and configure Xray as a systemd service. Configure Xray  After installation, the configuration file is located at: /usr/local/etc/xray/config.json Edit this file to set up your desired proxy protocol (e.g., VMess, VLESS, or Trojan). Below is an example configuration for VLESS with WebSocket and TLS: { "inbounds" : [ { "port" : 443 , "protocol" : "vless...

Installing .deb Packages Without Root Access in Ubuntu

In modern Linux environments, installing .deb packages typically requires sudo privileges. However, in scenarios such as shared hosting systems, enterprise security restrictions, or developer workstations with limited administrative access, an alternative approach is necessary. This guide provides a systematic method for extracting, configuring, and executing .deb packages within a user directory, bypassing the need for elevated permissions. Manual Extraction and Execution of .deb Packages Step 1: Extracting the .deb Package Use dpkg-deb to extract the package contents into a designated directory within the user's home directory: mkdir -p ~/my_software # Extract package contents dpkg-deb -x software.deb ~/my_software # Extract package metadata (optional but informative) dpkg-deb -e software.deb ~/my_software/DEBIAN The -x flag extracts core package files. The -e flag extracts control metadata, including package configurations and dependency details. Understanding ...

tmux: Ensuring Persistent Terminal Sessions for Reliable Server Administration

  Introduction Maintaining an uninterrupted terminal session is essential for professionals managing remote servers. Unexpected disconnections can disrupt workflows, leading to data loss and inefficiencies. tmux (Terminal Multiplexer) addresses this challenge by ensuring that sessions persist even when a user disconnects. This feature makes tmux indispensable for system administrators, developers, and any technical professional working with remote environments. By leveraging tmux , users can manage multiple terminal windows and panes within a single session, detach and reattach as needed, and automate workflows through scripting. This guide provides an exhaustive exploration of tmux —covering installation, session management, advanced pane manipulation, customization, scripting, and best practices. By the end, readers will have a comprehensive understanding of how to maximize tmux to enhance efficiency and maintain workflow continuity. 1. Getting Started with tmux Installin...