ファイルを比較し、内容が異なる場合は名前を変更します。

ファイルを比較し、内容が異なる場合は名前を変更します。

次のタスクを実行するには、コードを作成する必要があります。

  1. 最初にスクリプト名自体を引いた2つの引数を確認します(例:dar /mnt/sdb1 /root/testdir/testarcwith$1 = /mnt/sdb1$2 = /root/testdir/testarc)
  2. ソースディレクトリ(およびサブディレクトリ)にあるファイル拡張子とファイル拡張子を持つ.docすべて.pdfのファイルをターゲットディレクトリにコピーします。.PDFsdb1
  3. コピープロセス中に名前は同じですが、データが異なる可能性があるファイルの名前を変更するには、いくつかの種類のcmpコマンド(または可能ですか?)を使用します。diff
  4. .PDF/.pdfファイルと同じデータを持つファイルがある場合、.docそのファイルはコピーされず、バージョンのみがコピーされます。.PDF/.pdf.doc

これまで1と2をやってみました。 3と4はめちゃくちゃです。

ループを無視すると役に立たforない。

これは私のソースコードです。

#!/bin/bash
sourcePath=$1
destPath=$2
Filedoc="*.doc"
Filepdf="*.pdf"
FilePDF="*.PDF"

if [[ $# -ne 2 ]]; then
    echo "Usage ; dar doc_path archive_path"
    exit 1
fi

if [ ! -d sourcePath ]
    then echo Directory does not exist
fi

if [ ! -d destPath ]
    then mkdir -p $destPath
fi

for file in $(find "${sourcePath}" -type f -exec basename {} \; | sort | uniq -d); do
  num=1 
  fileName=$(echo "${file}" | cut -d '.' -f1)
  fileExtension=$(echo "${file}" | cut -d '.' -f2)
  dirName=$(dirname "${duplicate}")
  for duplicate in $(find "${sourcePath}" -name "${file}" | tail -n +2 ); do
    mv "${duplicate}" "${duplicate}${fileName}_${num}.${fileExtension}"
    echo "Renamed duplicate file ${duplicate} ${duplicate}_${num}.${fileExtension}"
    (( num = num + 1 ))
  done
done

for file in $(find "${sourcePath}" -name "*.pdf"); do
  fileName=$(echo "${file}" | cut -d '.' -f1)
  if $(find $(sourcePath) -name "${fileName}.doc" &>/dev/nulll; then
    echo "Sorry, a .doc file with that extension already exists, skipping copy"
    continue
  fi
done

find "${sourcePath}" -name "$Filedoc" -exec cp -r {} "${destPath}" \;
find "${sourcePath}" -name "$Filepdf" -exec cp -r {} "${destPath}" \;
find "${sourcePath}" -name "$FilePDF" -exec cp -r {} "${destPath}" \;

ベストアンサー1

rsyncこのオプションを使用して、ターゲット--backupディレクトリにあるファイルを自動的にバックアップできます。注:これにより、バックアップコピーのみがアーカイブされます。 srcファイルが2回変更されると、最新の古いバージョンのみがバックアップとして使用されます(デフォルトfilename~では名前が変更されます)。

たとえば、

#! /bin/bash

src="$1"
dst="$2"

# comment out the next line when you are sure it does what you want.
# And then try it with a trial destination dir a few times before
# running it with your real destination.
dryrun="--dry-run"

rsync $dryrun -avu --stats --progress --backup "$src" --include '**.doc' \
--include '**.pdf' --include '**.PDF' --exclude '**' "$dst/"  

より多くのバックアップコピーが必要な場合は、この--backup-dirオプションを使用できます。例えば

#! /bin/bash

src="$1"
dst="$2"

dryrun="--dry-run"

BD=$(date '+%Y%m%d-%H%M%S')

rsync $dryrun -avu --stats --progress --backup --backup-dir="./$BD/" "$src" \
    --include '**.doc' --include '**.pdf' --include '**.PDF' \
    --exclude '**' "$dst/"  

おすすめ記事