The xargs utility serves as both a parameter processor and a command composition tool, commonly used in conjunction with pipelines to construct complex command sequences.
Key Parameters
-a file: Reads input from specified file instead of stdin-e flagor-E flag: Stops processing when encountering the specified delimiter flag-p: Prompts user confirmation before executing each argument-n num: Limits the number of arguments passed per command execution-t: Displays commands before execution-I {}: Replaces placeholder with each input item (distribution dependent)-r: Prevents command execution when input is empty-s num: Sets maximum command line length-L num: Processes specified number of input lines per command-d delim: Customizes input delimiter character-x: Terminates execution when combined with size limits-P: Controls concurrent process count
Practical Applications
Moving text files from source to destination:
find /oldboy -type f -name "*.txt" | xargs -I {} cp {} /oldgirl
find /oldboy -type f -name "*.txt" | xargs cp -t /oldgirl
find /oldboy -type f -name "*.txt" | xargs -I {} mv {} /oldgirl
find /oldboy -type f -name "*.txt" | xargs mv -t /oldgirl
Removing aged files:
find /backup -type f -mtime +7 | xargs rm
Brace Expansion Patterns
Generating sequential combinations:
echo {A,B}{01,02} # Output: A01 A02 B01 B02
echo A{01,02} # Output: A01 A02
echo A{,02} # Output: A A02
File backup techniques:
cp /oldboy/oldboy.txt{,.bak}
Restoring backup files:
rm /oldboy/alex.txt -f
mv /oldboy/alex.txt{.ori,}
Filename Globbing Patterns
Wildcard matching syntax:
*: Matches zero or more characters?: Matches exact one character[range]: Matches any character within brackets
Character class expressions:
[a-z][A-Z][0-9]: Alphanumeric ranges[[:upper:]]: Uppercase letters[[:lower:]]: Lowercase letters[[:alpha:]]: All alphabetic characters[[:digit:]]: Numeric digits[[:alnum:]]: Alphanumeric characters[[:space:]]: Whitespace characters[[:punct:]]: Punctuation symbols[^pattern]: Negation of specified pattern
Pattern Matching Exercises
Locating specific directory entries:
ls -d /var/l?[[:lower:]]
Selective file copying operations:
cp -r /etc/m*[^0-9] /tmp/magedu.com/
cp -r /usr/share/man/man[0-9] /tmp/man/
cp -r /etc/[mnrp]*.conf /tmp/conf.d/
Input/Output Redirection and Pipelines
Process data streams:
- Standard enput (stdin): File descriptor 0
- Standard output (stdout): File descriptor 1
- Standard error (stderr): File descriptor 2
Redirection operators:
>: Overwrite output>>: Append output2>: Redirect errors&>: Combine stdout and stderr
Preventing accidental overwrites:
set -C # Disable file overwriting
set +C # Re-enable file overwriting
Input redirection example:
echo oldboy >/oldboy/info.log 2>&1
Text Transformation with tr
Character substitution without modifying source files:
tr 'a-z' 'A-Z' < /etc/passwd
tr -d 'aeiou' < /etc/passwd
Pipeline usage:
head -n 6 /etc/passwd | tr 'a-z' 'A-Z'
Additional Utilities
Here documents for user input:
cat << EOF
Your content here
EOF
Splitting data streams with tee:
command | tee /path/to/file
Discarding unwanted output:
command > /dev/null