传输脚本

传输脚本

我需要一个用于将文件对传输到文件夹中的脚本。

我有包含.pdf文件的目录。对于 every.pdf是 a .done(名称相同,只是模式不同pdf --> done)。

问题是,有些情况下 a.pdf没有.done,或者 a.done没有.pdf。在这种情况下,脚本应忽略该特定文件对并获取下一个文件。

我想全部搬走配对使用该脚本将文件复制到另一个文件夹中。但对于没有配对的文件则不会移动。

我制作了一个脚本,但我不知道在这种情况下如何进行比较和跳过:

#!/bin/bash

# source directory where all files are
SOURCE_DIR=/var/xms/batch/PDF/

# Name of directory you want to move the PDFs to.
DEST_DIR=/var/xms/batch/PDF/put_ready/

# Create the destination directory for the moved PDFs, if it doesn't already exist.
[ ! -d $DEST_DIR ] && mkdir -p $DEST_DIR

# Search for .done files starting in $SOURCE_DIR
find $SOURCE_DIR -type f -name "*.done" | while read fin
do

  # Try to Find the specific PDF which names the Pattern
  fpdf=?????

  # If a file with .pdf extension exists, move the .done file to the destination dir.
  # In the event of a file name clash in the destination dir, the incoming file has
  # a number appended to its name to prevent overwritng of the existing files.
  [ -f "$fpdf" ] && mv -v --backup=numbered "$fin" $DEST_DIR/
done

##
## End of script.
##

答案1

只需对您所拥有的进行一些更改即可。主要的变化是使用通配符来查找文件和参数扩展来划分文件路径和扩展名的部分。启用 nullglob 选项,以便在没有匹配项时 FOUND_FILE 的值为 null。由于您知道由于 for 循环中的全局匹配而存在完成文件,因此您可以检查是否存在匹配的 pdf。如果是这样,请检查目标目录中是否已存在 pdf 或完成的文件。当存在冲突时,使用纪元时间戳作为文件后缀。它并不完美,因为新文件名存在的可能性仍然很小。您可以再次检查这是否是真正的问题。

#!/bin/bash

# source directory where all files are
SOURCE_DIR=/var/xms/batch/PDF

# Name of directory you want to move the PDFs to.
DEST_DIR=/var/xms/batch/PDF/put_ready

# Create the destination directory for the moved PDFs, if it doesn't already exist.
[ ! -d "$DEST_DIR" ] && mkdir -p "$DEST_DIR"

# Enable nullglob in the event of no matches
shopt -s nullglob

# Search for .done files starting in $SOURCE_DIR
for FOUND_FILE in "$SOURCE_DIR"/*.done
do
  # Get root file path without extension
  FILE_ROOT="${FOUND_FILE%%.*}"

  # Is there a .pdf to go with the .done file
  if [ -f "${FILE_ROOT}.pdf" ]
  then
      # Do either of these files exist in the DEST_DIR
      if [ -f "$DEST_DIR/${FILE_ROOT##*/}.pdf" ] || [ -f "$DEST_DIR/${FILE_ROOT##*/}.done" ]
      then
          # Use epoch stamp as unique suffix
          FILE_SFX="$(date +%s)"

          ## You could still have a file conflict is the DEST_DIR and
          ## maybe you consider checking first or modifying the move command

          # Move the file pairs and add the new suffix
          mv "${FILE_ROOT}.done" "$DEST_DIR/${FILE_ROOT##*/}_${FILE_SFX}.done"
          mv "${FILE_ROOT}.pdf"  "$DEST_DIR/${FILE_ROOT##*/}_${FILE_SFX}.pdf"
      else
          # Move the file pairs
          mv "${FILE_ROOT}.done" "$DEST_DIR/${FILE_ROOT##*/}.done"
          mv "${FILE_ROOT}.pdf"  "$DEST_DIR/${FILE_ROOT##*/}.pdf"
      fi
  fi
done

##
## End of script.
##

答案2

尝试这个:

for FILE in $(ls $SOURCE_DIR/*.done); do NAME=${FILE::-4}; mv ${NAME}pdf $DEST_DIR 2>/dev/null && mv $FILE $DEST_DIR; done

相关内容