Unix Globbing and noclobber - unix1998/technical_notes GitHub Wiki

In Unix and Unix-like operating systems, there are several concepts and settings related to file matching and redirection behavior, including globbing and controlling overwrite behavior with redirections.

Globbing

Globbing refers to the process of pattern matching filenames using wildcards. Common globbing patterns include:

  • * matches any number of characters (including zero).
  • ? matches exactly one character.
  • [abc] matches any one character enclosed in the brackets (a, b, or c).
  • {xx,yy,zz} matches any of the strings enclosed in the braces (xx, yy, or zz).

For example:

  • xx* matches any file starting with "xx".
  • file?.txt matches file1.txt, file2.txt, etc.

Redirection Behavior

In Unix, you can control how redirection works, especially whether redirections like > will overwrite existing files or not. Here are some ways to control this behavior:

noclobber Option

The noclobber option in Bash prevents the shell from overwriting existing files with the > redirection operator. You can set or unset this option using the set built-in command.

  • To enable noclobber:

    set -o noclobber
    

    This will cause commands like echo "Hello" > file.txt to fail if file.txt already exists.

  • To disable noclobber:

    set +o noclobber
    

    This will allow the > operator to overwrite existing files.

Using >| to Force Overwrite

If noclobber is enabled and you still want to overwrite a file, you can use the >| operator:

echo "Hello" >| file.txt

Example

Here's an example to demonstrate how noclobber works:

#!/bin/bash

# Enable noclobber
set -o noclobber

# Attempt to write to an existing file (will fail if file exists)
echo "Hello" > file.txt

# Force overwrite
echo "Hello" >| file.txt

# Disable noclobber
set +o noclobber

# Overwrite file without error
echo "Hello again" > file.txt

Wildcard Matching (Globbing) Example

Here's an example to demonstrate globbing:

#!/bin/bash

# Create some test files
touch xx1.txt xx2.txt xx3.txt yy1.txt

# List files starting with "xx"
echo "Files starting with 'xx':"
ls xx*

# List files matching "xx?.txt" (one character after "xx")
echo "Files matching 'xx?.txt':"
ls xx?.txt

When you run this script, it will create files and then list them based on the globbing patterns used.

In summary:

  • Globbing allows you to use wildcards to match filenames.
  • The noclobber option prevents accidental overwriting of files with the > operator, and you can override this behavior using >|.