| 1 | #!/bin/sh
|
| 2 | # $ givefilesextensions [DIR]
|
| 3 | # gives the directory's files an extension if missing
|
| 4 | # [bugs]
|
| 5 | # - .jpeg is added to .jpg
|
| 6 |
|
| 7 | if [ "$#" -ne 1 ]; then
|
| 8 | echo "Usage $0 /path/to/dir"
|
| 9 | exit 1
|
| 10 | fi
|
| 11 |
|
| 12 | target_dir="$1"
|
| 13 |
|
| 14 | if [ ! -d "$target_dir" ]; then
|
| 15 | echo "ERROR '$target_dir' is not valid"
|
| 16 | exit 1
|
| 17 | fi
|
| 18 |
|
| 19 | for file in "$target_dir"/*; do
|
| 20 | [ -d "$file" ] && continue
|
| 21 | mime_type=$(file --mime-type -b "$file")
|
| 22 | extension=$(echo "$mime_type" | awk -F'/' '{print $2}')
|
| 23 |
|
| 24 | if [[ ! "$file" == *".$extension" && -n "$extension" ]]; then
|
| 25 | new_file="$file.$extension"
|
| 26 | echo "$file -> $new_file"
|
| 27 | mv "$file" "$new_file"
|
| 28 | fi
|
| 29 | done
|