在 CentOS 系統中,copendir
是一個用于創建一個目錄結構,并將源目錄的內容復制到新目錄結構的函數
copendir
函數。如果沒有安裝,可以使用以下命令安裝:sudo yum install epel-release
sudo yum install perl
copydir.pl
的腳本文件,并將以下內容粘貼到文件中:#!/usr/bin/perl
use strict;
use warnings;
# 檢查參數數量
if (@ARGV != 2) {
die "Usage: $0 <source_dir> <destination_dir>\n";
}
my $source_dir = $ARGV[0];
my $destination_dir = $ARGV[1];
# 檢查源目錄是否存在
if (!-d $source_dir) {
die "Source directory does not exist: $source_dir\n";
}
# 如果目標目錄不存在,則創建它
if (!-d $destination_dir) {
mkdir($destination_dir, 0755) or die "Failed to create destination directory: $destination_dir\n";
}
# 使用 copendir 函數復制目錄結構
copendir($source_dir, $destination_dir) or die "Failed to copy directory structure from $source_dir to $destination_dir\n";
print "Directory structure copied from $source_dir to $destination_dir successfully.\n";
chmod +x copydir.pl
/home/user/source
目錄復制到 /home/user/destination
,可以運行以下命令:./copydir.pl /home/user/source /home/user/destination
這將創建一個新的目錄結構,并將源目錄的內容復制到新目錄中。