Introduction to Bash Globbing on Linux


Bash globbing is the process of using wildcard characters to match multiple filenames or paths. Bash provides several special characters that can be used for globbing, such as *, ?, and [].

The * character is a wildcard that can match zero or more characters in a filename or path. For example, the command ls * would list all files in the current directory, while the command ls *.txt would list all files with the ".txt" extension in the current directory.

The ? character is similar to the * character, but it only matches a single character. For example, the command ls ?.txt would match filenames like "a.txt" or "b.txt", but not "abc.txt"

[] character is used to specify a character class, which can be used to match any single character that is a member of the class. For example, the command ls [abc]*.txt would match filenames like "a.txt", "b.txt" or "c.txt" but not "d.txt"

Matching Any String

In Bash, the wildcard character * can be used to match any string. The * character is a special character that is used to match any number of characters (including zero characters) in a filename or path.

For example, the command ls * will list all files in the current directory, regardless of their names. The command ls *file* will list all files in the current directory that have the string "file" anywhere in their names.

You can also use the * character to match any file with specific extension, like ls *.txt will match all the files with .txt extension in the current directory.

It's important to note that Bash's globbing only works for files in the file system and not for directories or command outputs. You can use other commands like find or grep to match strings in files or command output.

Matching a Single Character

In Bash, the wildcard character ? can be used to match a single character. The ? character is similar to the * character, but it only matches a single character. This is useful when you need to match a specific pattern that includes a single unknown character.

For example, the command ls file?.txt will match filenames like "filea.txt" or "fileb.txt", but not "fileabc.txt" or "file.txt". This is useful if you want to match all files that starts with "file" and ends with ".txt" but have one additional letter in the middle

You can also use the ? character in combination with other wildcard characters. For example, the command ls file?* will match filenames like "filea.txt" or "fileabcd" or "fileabc"

You can use [] to match specific set of characters. Like ls file[abc]*.txt will match "filea.txt" , "fileb.txt", "filec.txt" but not "filed.txt"

Matching a Range of Characters

In Bash, you can use square brackets [] to specify a range of characters, called a character class, which can be used to match any single character that is a member of the class.

For example, the command ls file[a-z].txt will match filenames like "filea.txt" or "fileb.txt" or "filec.txt" through "filez.txt" but not "fileA.txt" or "file1.txt"

You can also use an exclamation point ! inside the brackets to negate the class, to match any character that is not in the class. For example, ls file[!a-z].txt would match filenames like "fileA.txt" or "file1.txt" but not "filea.txt" or "fileb.txt

You can also specify multiple ranges like [a-zA-Z] will match any upper or lowercase alphabetical characters. This can be useful for matching multiple variations of a specific pattern in a filename.

It's important to mention that the range of characters is case sensitive and the globbing match the file names based on the characters in the range accordingly.

Hidden Files

In Unix-based systems, including Linux and macOS, files and directories that start with a dot . are considered hidden files. These files and directories are not typically displayed by default when using commands like ls or when viewing the contents of a directory in a file browser.

To display hidden files and directories in the terminal, you can use the -a or --all option with the ls command. For example, the command ls -a will list all files and directories, including hidden ones, in the current directory.

Alternatively, you can use ls -A to show all files and directories, also including hidden files but ignore "." and ".." files which are considered as current and parent directory respectively.

You can also use ls -la to show both files and directories, including hidden files in detailed format like permissions, owner, and timestamps.

Hidden files can also be matched using wildcard characters in bash. For example, the command ls .* will match all hidden files and directories in the current directory.

Conclusion

In conclusion, Bash provides several special characters, such as *, ?, and [] that can be used for globbing, which is the process of using wildcards to match multiple filenames or paths. The * character can match any string, the ? character matches a single character, and the [] character allows you to specify a range of characters called a character class.

Hidden files and directories in Linux and macOS are files and directories that start with a dot . which are not typically displayed by default when using commands like ls or when viewing the contents of a directory in a file browser. To display hidden files and directories you can use the -a or -A option with the ls command.

Updated on: 24-Jan-2023

849 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements