python - 将特定文件从一个目录复制到另一个目录并附加其基本目录名称

标签 python linux bash find xargs

我有
~/a/a1/1.pdf
~/a/a1/2.pdf
~/a/b1/1.pdf
~/a/b1/2.pdf
我想将所有 pdf 文件复制到 ~/a/c 中,名称为 a1_1.pdf、a1_2.pdf、b1_1.pdf、b2_2.pdf。我该怎么做?

请原谅我的无知。我是编程和Linux新手

最佳答案

您提到您对 Linux 不熟悉。下面是如何执行此操作的 shell 命令。了解这一点后,如果您想在 Python 脚本中执行此操作,则可以通过导入 os 模块来执行类似的操作。

首先创建你的目标目录:

# mkdir ~/a
# mkdir ~/a/c

然后就可以复制了

# cp ~/a/a1/1.pdf ~a/c/a1_1.pdf
# cp ~/a/a1/2.pdf ~a/c/a1_2.pdf
# cp ~/a/b1/2.pdf ~a/c/b1_1.pdf
# cp ~/a/b1/2.pdf ~a/c/b2_2.pdf

关于python - 将特定文件从一个目录复制到另一个目录并附加其基本目录名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50406598/

相关文章:

python - 当 "pip install <package name>"时会发生什么

linux - Libfreenect2 点云帧捕获

linux - 说说如何在 bash 中创建一个不使用递归的菜单?

c++ - 体系结构 x86_64 : "std::__1::locale::use_facet(std::__1::locale::id&) const" 的 undefined symbol

linux - 在 bash 脚本中自动向 ftp 提供命令

python - 无法在 matplotlib 中使用自定义字体

python - 超 HTTP2 自定义 SSLContext 错误

python - 使用多个条件进行 numpy 切片,其中一个条件搜索元素的邻域

linux - awk 语法和字符串错误

windows - 获取目录大小和磁盘大小的最快方法