Here's a shell script that copies extended attributes from one file to another. This script works on systems that support extended attributes, such as Linux (with getfattr and setfattr) or macOS (with xattr).
Since the implementation differs between operating systems, I'll provide versions for both Linux and macOS, and the script will automatically detect the system and use the appropriate method.
#!/bin/bash
# Function to display usage
usage() {
echo "Usage: $0 source_file destination_file"
echo "Copies extended attributes from source_file to destination_file"
exit 1
}
# Check if correct number of arguments is provided
if [ $# -ne 2 ]; then
usage
fi
SOURCE_FILE="$1"
DEST_FILE="$2"
# Check if source file exists
if [ ! -f "$SOURCE_FILE" ]; then
echo "Error: Source file '$SOURCE_FILE' does not exist"
exit 1
fi
# Check if destination file exists
if [ ! -f "$DEST_FILE" ]; then
echo "Error: Destination file '$DEST_FILE' does not exist"
exit 1
fi
# Function to copy extended attributes on Linux
copy_xattr_linux() {
# Check if getfattr and setfattr are available
if ! command -v getfattr >/dev/null 2>&1 || ! command -v setfattr >/dev/null 2>&1; then
echo "Error: This script requires getfattr and setfattr (install attr package)"
exit 1
}
# Create a temporary file to store attributes
TEMP_FILE=$(mktemp)
# Get all extended attributes from source file
if ! getfattr -d -m - "$SOURCE_FILE" > "$TEMP_FILE" 2>/dev/null; then
echo "Error: Failed to get extended attributes from source file"
rm -f "$TEMP_FILE"
exit 1
}
# Check if there are any attributes to copy
if ! grep -q "^user\." "$TEMP_FILE"; then
echo "No extended attributes found on source file"
rm -f "$TEMP_FILE"
return 0
}
# Apply the attributes to destination file
if ! setfattr --restore="$TEMP_FILE" "$DEST_FILE" 2>/dev/null; then
echo "Error: Failed to set extended attributes on destination file"
rm -f "$TEMP_FILE"
exit 1
}
echo "Successfully copied extended attributes"
rm -f "$TEMP_FILE"
}
# Function to copy extended attributes on macOS
copy_xattr_macos() {
# Check if xattr is available
if ! command -v xattr >/dev/null 2>&1; then
echo "Error: This script requires xattr command"
exit 1
}
# Get list of extended attributes
ATTRS=$(xattr "$SOURCE_FILE" 2>/dev/null)
if [ -z "$ATTRS" ]; then
echo "No extended attributes found on source file"
return 0
}
# Copy each attribute
while IFS= read -r attr; do
# Get the attribute value (in hex format)
VALUE=$(xattr -p -x "$attr" "$SOURCE_FILE" 2>/dev/null)
if [ $? -eq 0 ] && [ -n "$VALUE" ]; then
# Set the attribute on destination file
if ! echo "$VALUE" | xattr -w -x "$attr" "$DEST_FILE" 2>/dev/null; then
echo "Error: Failed to set attribute '$attr' on destination file"
exit 1
fi
fi
done <<< "$ATTRS"
echo "Successfully copied extended attributes"
}
# Detect operating system and call appropriate function
OS_TYPE=$(uname -s)
case "$OS_TYPE" in
Linux*)
copy_xattr_linux
;;
Darwin*)
copy_xattr_macos
;;
*)
echo "Error: Unsupported operating system: $OS_TYPE"
echo "This script supports Linux and macOS only"
exit 1
;;
esac
How to Use the Script
- Save the script to a file, e.g.,
copy_xattr.sh
- Make it executable:
chmod +x copy_xattr.sh
- Run it with two arguments (source file and destination file):
./copy_xattr.sh source_file.txt destination_file.txt
Features of the Script
- Cross-Platform Support: Works on both Linux and macOS, automatically detecting the operating system.
- Error Handling: Checks for:
- Correct number of arguments
- Existence of source and destination files
- Availability of required commands (
getfattr/setfattr on Linux, xattr on macOS)
- Success of attribute copying operations
- Clean Temporary Files: Removes temporary files used during the process (on Linux).
- Informative Messages: Provides feedback about the success or failure of the operation.
Requirements
Notes
- Extended attributes must be supported by the filesystem. Common filesystems that support extended attributes include ext4, XFS, and Btrfs on Linux, and APFS and HFS+ on macOS.
- The script copies user-defined extended attributes (on Linux, those in the
user. namespace). System attributes (e.g., system. or security.) may require root privileges and are not copied by this script.
- On macOS, the script copies all extended attributes it can access (some may require elevated privileges).
Example Usage
If you have a file source.txt with extended attributes, you can copy them to dest.txt:
./copy_xattr.sh source.txt dest.txt
To verify the attributes were copied:
- On Linux: Use
getfattr -d source.txt and getfattr -d dest.txt
- On macOS: Use
xattr -l source.txt and xattr -l dest.txt