テキストファイルで指定されたファイルをBASHの他のディレクトリに移動するには? [コピー]

テキストファイルで指定されたファイルをBASHの他のディレクトリに移動するには? [コピー]

400以上のイメージを含むディレクトリがあります。それらのほとんどは腐敗しました。私は良いことを見つけました。テキストファイルとして一覧表示されます(100以上)。 BASHの別のディレクトリに一度に移動するにはどうすればよいですか?

ベストアンサー1

私はすぐにこれを行ういくつかの方法を考えました。

  1. whileループの使用
  2. xargsの使用
  3. rsyncの使用

ファイル名が1行に1つずつリストされており、files.txtそれをサブディレクトリからsource/サブディレクトリに移動したいとしますtarget

whileループは次のとおりです。

while read filename; do mv source/${filename} target/; done < files.txt

xargs コマンドは次のとおりです。

cat files.txt | xargs -n 1 -d'\n' -I {} mv source/{} target/

rsync コマンドは次のとおりです。

rsync -av --remove-source-files --files-from=files.txt source/ target/

各アプローチを実験してテストするためにサンドボックスを作成することをお勧めします。たとえば、次のようになります。

# Create a sandbox directory
mkdir -p /tmp/sandbox

# Create file containing the list of filenames to be moved
for filename in file{001..100}.dat; do basename ${filename}; done >> /tmp/sandbox/files.txt

# Create a source directory (to move files from)
mkdir -p /tmp/sandbox/source

# Populate the source directory (with 100 empty files)
touch /tmp/sandbox/source/file{001..100}.dat

# Create a target directory (to move files to)
mkdir -p /tmp/sandbox/target

# Move the files from the source directory to the target directory
rsync -av --remove-source-files --files-from=/tmp/sandbox/files.txt /tmp/sandbox/source/ /tmp/sandbox/target/

おすすめ記事