linux - 如何将选定的文件从一个文件夹移动到另一个带有子文件夹的文件夹
问题描述
这是我移动文件或复制文件的代码。
#Here $directory = www(so the directory will create like this RubbishBin/www/)
my $takingDir= "RubbishBin/$directory";
#@missingFiles= www/c2.html and www/image/1-3.gif
my $createDir = `mkdir -vp $@ "$takingDir" && cp @missingFiles "$takingDir"`;
say ("By Default Folder taking: RubbishBin");
我想将此文件移动到RubbishBin
文件夹中。
我的输出是这样的
RubbishBin/www/c2.html
RubbishBin/www/1-3.gif
但我的问题是如何在RubbishBin/www/
. 此外,下一个文件夹不必是image
. 它会有所不同,如Download
or video
。
我想要这样的输出
RubbishBin/www/c2.html
RubbishBin/www/image/1-3.gif
or
RubbishBin/www/Download/b.gif
在www/
我的脚本将选择自动创建该文件夹并像上面的输出一样移动该文件之后。
解决方案
下面是一个示例,说明如何在源 ( $directory
) 和目标路径 ( $takingDir
) 下创建相同的文件夹结构:
use strict;
use warnings;
use File::Basename qw(dirname);
use File::Copy::Recursive qw(rcopy);
use File::Path qw(make_path);
use File::Spec::Functions qw(catfile);
my $directory = 'www';
my $takingDir= "RubbishBin/$directory";
my @missingFiles= qw(c2.html image/1-3.gif);
for my $name (@missingFiles) {
my $dirname = dirname( $name );
my $dest_path = catfile($takingDir, $dirname);
make_path( $dest_path );
my $source_path = catfile( $directory, $name);
rcopy( $source_path, $dest_path ) or die "copy failed: $!";
}
推荐阅读
- python - 将 txt 文件中的字符串拆分为元组列表
- javascript - 以 60fps 运行功能
- reactjs - 下一个/动态中的悬念选项使用无效
- amazon-s3 - 如何自动导出架构并将其上传到 S3 存储桶并删除 AWS oracle RDS 中的旧转储
- java - 我缺少什么来完成 Coursier scala 开发环境设置?
- mysql - Golang正确发送带有状态的json响应的方法
- python - 拉丁字符的 Python Unicode 转换
- c++ - 不显示任何输出
- javascript - gridstack 没有得到最合适的行(0)和第二行(1)
- java - 如何在 Apache Camel 中创建 Excel (.xlsx) 文件?