我有一个文件列表,我想将其从一个文件A
夹移动到另一个文件夹B
,但我不想手动执行此操作。有没有更简单的方法来遍历列表并移动该列表中的每个文件?
roa*_*ima 10
您可以使用rsync
移动它们,假设您的文件列表是每行一个文件名。
rsync -av --remove-source-files --files-from filelist.txt sourceDir/ targetDir/
Run Code Online (Sandbox Code Playgroud)
如果您的文件是绝对名称(即名称以 开头/
),则sourceDir
应该是/
. 否则它应该是相对名称的根。
例子
$ mkdir src dst
touch src/{one,two,three}
$ cat >filelist.txt <<EOF
one
two
EOF
$ ls src
one three two
$ ls dst
$ rsync -av --files-from filelist.txt --remove-source-files src/ dst/
building file list ... done
one
two
sent 165 bytes received 70 bytes 470.00 bytes/sec
total size is 0 speedup is 0.00
$ ls src
three
$ ls dst
one two
Run Code Online (Sandbox Code Playgroud)
如果您提供了 GNU 核心实用程序(或具有这些特定功能的其他实现),您可以使用它xargs
来构建mv
基于文件列表的参数列表:
cd A
xargs -rd '\n' -- mv -t B -- < file-list.txt
Run Code Online (Sandbox Code Playgroud)
如果没有 GNU 实用程序,您仍然可以使用 while-read 循环。在 Bash 中,这可能是:
while IFS= read -r file; do
mv "A/$file" "B/$file"
done < file-list.txt
Run Code Online (Sandbox Code Playgroud)
我假设你只有文件名。我认为这是最简单的方法:
while IFS= read -r file; do mv -v "dirA/$file" "dirB/$file"; done < listfile.txt
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3712 次 |
最近记录: |