我对执行文件操作的脚本有疑问

我对执行文件操作的脚本有疑问

我一直在开发一个脚本,该脚本可以立即检测在下载目录中创建的文件,然后根据文件扩展名对它们进行排序。然而,这样做的问题是,当从互联网下载文件时,它会移动文件,同时下载会损坏文件。

有没有办法可以停止脚本的操作直到完成?例如,我有一个想法,我注意到,当从互联网下载文件时,我通常会看到一个带有 .part 文件扩展名的文件,我可以添加一些内容,当它检测到此文件扩展名时,会循环直到它消失,然后继续执行其余部分剧本?

如果我不乐意接受任何建议,如果您有疑问,请直接询问。 -谢谢

#!/bin/bash

# Requires inotify-tools package
#
# Authors: oddstap && yetanothergeek
#
# This simple tool takes newly created files in the Downloads directory
# and then organizes them based on file extension.

TARGET=$HOME/Downloads
inotifywait -m -e close_write -e moved_to --format "%f" "$TARGET" \
| while read FILENAME; do

  EXT=${FILENAME##*.} # Extract file extension
  EXT=${EXT,,} # Convert to lowercase
  DEST_DIR=''

  case "$EXT" in

    # Word processor and text files
    doc|docx|odt|pdf|rtf|tex|txt|wks|wps|wpd)
      DEST_DIR="$HOME/Documents/Word_Processor_And_Text_files"
    ;;

    # Audio files
    mp3|wav|wma|mid|midi|aif|cda|mpa|ogg|wpl)
      DEST_DIR="$HOME/Music"
    ;;

    # Image files
    jpg|jpeg|png|ai|bmp|gif|ico|ps|svg|tif|tiff|psd)
      DEST_DIR="$HOME/Pictures"
    ;;

    # Video files
    avi|wmv|3g2|3gp|flv|h264|m4v|mkv|mov|mp4|mpg|mpeg|rm|swf|vob|wmv)
      DEST_DIR="$HOME/Videos"
    ;;

    # Compressed files
    7z|arj|deb|pkg|rar|rpm|gz|z|zip)
      DEST_DIR="$HOME/Documents/Compressed_Files"
    ;;

    # Disc and media files
    bin|dmg|iso|toast|vcd)
      DEST_DIR="$HOME/Documents/Disk_Images"
    ;;

    # Data and database files
    csv|dat|db|dbf|log|mdb|sav|sql|tar|xml)
      DEST_DIR="$HOME/Documents/Data_Database"
    ;;

    # Executable files
    apk|bat|cgi|pl|com|exe|gadget|jar|py|wsf)
      DEST_DIR="$HOME/Documents/Executable_File"
    ;;

    # Font files
    fnt|fon|otf|ttf)
      DEST_DIR="$HOME/Documents/Fonts"
    ;;

    # Internet related files
    asp|cer|cfm|css|htm|html|js|jsp|php|rss|xhtml)
      DEST_DIR="$HOME/Documents/Internet_files"
    ;;

    # Presentation files
    key|odp|pps|ppt|pptx)
      DEST_DIR="$HOME/Documents/Presentation"
    ;;

    # Programming files
    c|class|cpp|cs|h|java|sh|swift|vb)
      DEST_DIR="$HOME/Documents/Programming_Files"
    ;;

    # Spreadsheet files
    ods|xlr|xls|xlsx)
      DEST_DIR="$HOME/Documents/Spreadsheets"
    ;;

    # Anything else
    *)
      # TODO: handle any unrecognized files here
    ;;
  esac
  if [ "$DEST_DIR" = "" ] ; then
    # If we didn't find a place for this file, just skip it.
    continue
  fi
  # Now we should have our filename and our destination directory
  # So let's do it!
  mkdir -p "$DEST_DIR"
  chmod +w "$TARGET/$FILENAME"
  if ! [ -e "$DEST_DIR/$FILENAME" ] ; then
    mv "$TARGET/$FILENAME" "$DEST_DIR"
  else
    # Don't clobber existing files!
    # If we already have a "foo.txt", try "foo.txt.1.txt",
    # "foo.txt.2.txt", etc. If we can't find a unique name
    # after "foo.txt.99.txt" just give up -- the user can
    # deal with it later.
    N=0
    while [ $N -le 99 ] ; do
      if ! [ -e "$DEST_DIR/$FILENAME.$N.$EXT" ] ; then
        mv "$TARGET/$FILENAME" "$DEST_DIR/$FILENAME.$N.$EXT"
        break # Success!
      fi
      N=$((N+1))
    done
  fi
done

答案1

我相信我已经解决了这个问题,幸运的是,我能够遇到一个比我更有才华的 Linux 用户,他也是该脚本的共同作者,不会透露姓名,因为这对我来说很粗鲁,但他出现了在“| while read FILENAME; do”末尾添加此行“[ -s“$TARGET/$FILENAME”] || continue”不知道它会做什么,因为我是新手,但现在脚本将等待直到文件下载完成后再移动它们。

#!/bin/bash

# Requires inotify-tools package
#
# Authors: oddstap && yetanothergeek
#
# This simple tool takes newly created files in the Downloads directory
# and then organizes them based on file extension.

TARGET=$HOME/Downloads
inotifywait -m -e close_write -e moved_to --format "%f" "$TARGET" \
| while read FILENAME; do

  [ -s "$TARGET/$FILENAME" ] || continue

  EXT=${FILENAME##*.} # Extract file extension
  EXT=${EXT,,} # Convert to lowercase
  DEST_DIR=''

  case "$EXT" in

    # Word processor and text files
    doc|docx|odt|pdf|rtf|tex|txt|wks|wps|wpd)
      DEST_DIR="$HOME/Documents/Word_Processor_And_Text_files"
    ;;

    # Audio files
    mp3|wav|wma|mid|midi|aif|cda|mpa|ogg|wpl)
      DEST_DIR="$HOME/Music"
    ;;

    # Image files
    jpg|jpeg|png|ai|bmp|gif|ico|ps|svg|tif|tiff|psd)
      DEST_DIR="$HOME/Pictures"
    ;;

    # Video files
    avi|wmv|3g2|3gp|flv|h264|m4v|mkv|mov|mp4|mpg|mpeg|rm|swf|vob|wmv)
      DEST_DIR="$HOME/Videos"
    ;;

    # Compressed files
    7z|arj|deb|pkg|rar|rpm|gz|z|zip)
      DEST_DIR="$HOME/Documents/Compressed_Files"
    ;;

    # Disc and media files
    bin|dmg|iso|toast|vcd)
      DEST_DIR="$HOME/Documents/Disk_Images"
    ;;

    # Data and database files
    csv|dat|db|dbf|log|mdb|sav|sql|tar|xml)
      DEST_DIR="$HOME/Documents/Data_Database"
    ;;

    # Executable files
    apk|bat|cgi|pl|com|exe|gadget|jar|py|wsf)
      DEST_DIR="$HOME/Documents/Executable_File"
    ;;

    # Font files
    fnt|fon|otf|ttf)
      DEST_DIR="$HOME/Documents/Fonts"
    ;;

    # Internet related files
    asp|cer|cfm|css|htm|html|js|jsp|php|rss|xhtml)
      DEST_DIR="$HOME/Documents/Internet_files"
    ;;

    # Presentation files
    key|odp|pps|ppt|pptx)
      DEST_DIR="$HOME/Documents/Presentation"
    ;;

    # Programming files
    c|class|cpp|cs|h|java|sh|swift|vb)
      DEST_DIR="$HOME/Documents/Programming_Files"
    ;;

    # Spreadsheet files
    ods|xlr|xls|xlsx)
      DEST_DIR="$HOME/Documents/Spreadsheets"
    ;;

    # Anything else
    *)
      # TODO: handle any unrecognized files here
    ;;
  esac
  if [ "$DEST_DIR" = "" ] ; then
    # If we didn't find a place for this file, just skip it.
    continue
  fi
  # Now we should have our filename and our destination directory
  # So let's do it!
  mkdir -p "$DEST_DIR"
  chmod +w "$TARGET/$FILENAME"
  if ! [ -e "$DEST_DIR/$FILENAME" ] ; then
    mv "$TARGET/$FILENAME" "$DEST_DIR"
  else
    # Don't clobber existing files!
    # If we already have a "foo.txt", try "foo.txt.1.txt",
    # "foo.txt.2.txt", etc. If we can't find a unique name
    # after "foo.txt.99.txt" just give up -- the user can
    # deal with it later.
    N=0
    while [ $N -le 99 ] ; do
      if ! [ -e "$DEST_DIR/$FILENAME.$N.$EXT" ] ; then
        mv "$TARGET/$FILENAME" "$DEST_DIR/$FILENAME.$N.$EXT"
        break # Success!
      fi
      N=$((N+1))
    done
  fi
done

相关内容