転送スクリプト

転送スクリプト

ファイルペアをフォルダに転送するためのスクリプトが必要です。

ファイルを含むディレクトリがあります.pdf。 for everything.pdf.done(同じ名前、ちょうど別のモードpdf --> done)です。

問題は、場合によってはaが.pdf存在しないか、.doneaが.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

持っているものにいくつか変更するだけです。主な変更点は、ワイルドカードを使用してファイルを検索し、パラメータを拡張してファイルパスと拡張子の一部を区別することです。一致するものがない場合、FOUND_FILE値がnullになるようにnullglobオプションを有効にします。 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.
##

おすすめ記事