bash - 将文件从一个文件夹结构移动到另一个文件夹结构

标签 bash file ubuntu terminal directory

假设我有这样的目录结构:

/home/user/Desktop/test/testdir1/
/home/user/Desktop/test/testdir2/
/home/user/Desktop/test/testdir3/

这里的结构相同:

/home/user/Documents/test/testdir1/
/home/user/Documents/test/testdir2/
/home/user/Documents/test/testdir3/

如何移动文件,例如

/home/user/Desktop/test/testdir3/hello.txt

到各自的目录? (仅移动文件)

最佳答案

我会使用这个命令:

cd /home/user/Desktop/
find . -type d -print0 | xargs -0 -I'{}' echo mkdir -p '/home/user/Documents/{}'
find . -type f -print0 | xargs -0 -I'{}' echo mv '{}' '/home/user/Documents/{}'

引用 {} 将确保您的脚本适用于包含空格的文件。

此外,-print0-0 还会使用 \将文件从 find 传递到 xargs 0 作为分隔符。这将确保您的脚本可以使用奇怪的文件名。

如果脚本似乎适合您,请删除echoecho 的使用是为了测试命令而不产生副作用。

关于bash - 将文件从一个文件夹结构移动到另一个文件夹结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22446270/

相关文章:

python - 使用 raw_input() 时,在 bash 中使用 > 保存 Python 脚本的输出

java - 如果文件不存在,readAllLines 会返回什么?

shell - 使用 "become: true"在 Ansible 不工作的情况下更改默认 shell

linux - ~/.local/bin 中的文件优先于/usr/bin

unix - Emacs 无法保存自定义 - 初始化文件未完全加载

bash - 如何在 bash 中为导出添加别名

linux - Linux 文件中缺少 EOL 字符

regex - 如何解决这个 sed 语法问题

c++ - 写入文件mfc

PHP:将文件从网络下载到本地计算机