Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Directories to monitor (add your directories here)
- WATCH_DIRS=(
- "/path/to/dir1"
- "/path/to/dir2"
- )
- # Subdirectory for extracted files (optional)
- EXTRACT_DIR="extracted"
- # Function to extract RAR files
- extract_rar() {
- local rar_file="$1"
- local dest_dir="$(dirname "$rar_file")/$EXTRACT_DIR"
- # Create the extraction directory if it doesn't exist
- mkdir -p "$dest_dir"
- # Extract the RAR file
- unrar x -o+ "$rar_file" "$dest_dir"
- if [[ $? -eq 0 ]]; then
- echo "Extracted: $rar_file -> $dest_dir"
- else
- echo "Failed to extract: $rar_file"
- fi
- }
- # Monitor directories for new .rar files
- echo "Monitoring directories for new .rar files..."
- inotifywait -m -e create --format '%w%f' "${WATCH_DIRS[@]}" | while read new_file; do
- # Check if the new file is a .rar
- if [[ "$new_file" =~ \.rar$ ]]; then
- echo "Detected new RAR file: $new_file"
- extract_rar "$new_file"
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement