sistema_progs

Programas para customizar o meu entorno de traballo nos meus equipos persoais
Log | Files | Refs

organize (1664B)


      1 #!/usr/bin/env sh
      2 
      3 # Description: Organize files in directories by category
      4 #
      5 # Note: This plugin clears the selection as it changes the contents of the current dir
      6 #
      7 # Shell: POSIX compliant
      8 # Author: th3lusive
      9 
     10 sel=${NNN_SEL:-${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.selection}
     11 
     12 organize() {
     13     case "$(file -biL "$1")" in
     14         *video*)
     15             [ ! -d "Videos" ] && mkdir "Videos"
     16             mv "$1" "Videos/$1"
     17             printf "Moved %s to Videos\n" "$1" ;;
     18 
     19         *audio*) [ ! -d "Audio" ] && mkdir "Audio"
     20             mv "$1" "Audio/$1"
     21             printf "Moved %s to Audio\n" "$1" ;;
     22 
     23         *image*)
     24             [ ! -d "Images" ] && mkdir "Images"
     25             mv "$1" "Images/$1"
     26             printf "Moved %s to Images\n" "$1" ;;
     27 
     28         *pdf*|*document*|*epub*|*djvu*|*cb*)
     29             [ ! -d "Documents" ] && mkdir "Documents"
     30             mv "$1" "Documents/$1"
     31             printf "Moved %s to Documents\n" "$1" ;;
     32 
     33         *text*)
     34             [ ! -d "Plaintext" ] && mkdir "Plaintext"
     35             mv "$1" "Plaintext/$1"
     36             printf "Moved %s to Plaintext\n" "$1" ;;
     37 
     38         *tar*|*xz*|*compress*|*7z*|*rar*|*zip*)
     39             [ ! -d "Archives" ] && mkdir "Archives"
     40             mv "$1" "Archives/$1"
     41             printf "Moved %s to Archives\n" "$1" ;;
     42 
     43         *binary*)
     44             [ ! -d "Binaries" ] && mkdir "Binaries"
     45             mv "$1" "Binaries/$1"
     46             printf "Moved %s to Binaries\n" "$1" ;;
     47     esac
     48 }
     49 
     50 main() {
     51     for file in *
     52     do
     53        [ -f "$file" ] && organize "$file"
     54     done
     55 
     56     # Clear selection
     57     if [ -s "$sel" ] && [ -p "$NNN_PIPE" ]; then
     58         printf "-" > "$NNN_PIPE"
     59     fi
     60 }
     61 
     62 main "$@"