Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- folder="$(pwd)"
- # Check if a folder argument is provided
- if [ $# -gt 0 ]; then
- # Assign the full path to a variable and remove trailing slash
- folder="$(readlink -f -- "${1%/}")"
- fi
- # Check if the folder exists
- if [ ! -d "$folder" ]; then
- echo "Error: The specified folder '$folder' does not exist."
- exit 1
- fi
- # Ask for confirmation
- read -r -p "Are you sure you want to apply the +F flag to all directories in '$folder' and its subdirectories? (yes/no): " confirmation
- if [[ ! "$confirmation" =~ ^([yY][eE][sS]|[yY])$ ]]; then
- echo "Operation cancelled."
- exit 1
- fi
- # Check if the filesystem supports case-folding
- filesystem=$(df -P "$folder" | awk 'NR==2 {print $1}')
- if ! sudo tune2fs -l "$filesystem" | grep -q "casefold"; then
- echo "Error: The filesystem containing '$folder' does not support case-folding."
- exit 1
- fi
- # Parent directory of the target folder
- parent_dir="$(dirname -- "$folder")"
- # Create a temporary working directory next to the target
- if ! temp_dir=$(mktemp -d -p "$parent_dir" 2>/dev/null); then
- echo "Error: Failed to create temporary directory in '$parent_dir'!"
- exit 1
- fi
- # Move the original folder into the temporary location
- mv "$folder" "$temp_dir/" 2>/dev/null
- # Recreate directory tree and apply +F to each directory
- while IFS= read -r -d '' dir; do
- rel_path="${dir#"$temp_dir/"}"
- target_path="$parent_dir/$rel_path"
- mkdir -p -- "$target_path"
- sudo chattr +F -- "$target_path"
- done < <(find "$temp_dir" -mindepth 1 -type d -print0)
- # Root path of the moved original tree
- moved_root="$temp_dir/$(basename -- "$folder")"
- # Copy files back into the recreated directories
- rsync -a -- "$moved_root/" "$folder/"
- # Clean up temporary directory
- rm -rf -- "$temp_dir"
- echo "Operation completed."
Advertisement