I need to make a shell script that has 2 arguments, each one a valid directory. The script makes a new directory in the first directory specified by the first argument with the same name as the second one and copies the content(both subdirectories and files) of the second one in the newly created directory. But it only copies the files with .txt extension.
我需要创建一个包含2个参数的shell脚本,每个参数都是一个有效的目录。该脚本在第一个参数指定的第一个目录中创建一个与第二个目录同名的新目录,并在新创建的目录中复制第二个目录的内容(包括子目录和文件)。但它只复制扩展名为.txt的文件。
This is what I got so far:
这是我到目前为止所得到的:
#!/bin/bash
if [ ! $# -eq 2 ]
then echo usage: file.sh directory1 directory2
exit 1
fi
if [ ! -d $1 ]
then echo $1 is not a directory \!
exit 1
fi
if [ ! -d $2 ]
then echo $2 is not a directory \!
exit 1
fi
4 个解决方案
#1
1
Leaving debugging to the student:
给学生留下调试:
die () { echo >&2 "$*"; echo "Usage...."; exit 1; } from="$1"; to="$2"; [ ."$from" = . ] && die "from dir name missing"; [ ."$to" = . ] && die "to dir name missing"; [ -d "$from" ] || die "from dir $from not a directory"; [ -d "$to" ] || die "to dir $to not a directory"; target="$to/$(basname "$from")"; #final target dir name, if I understand you correctly. find "$from" -name '*.txt' -maxdepth=1 | cpio -pd "$to" || # (cd "$from" && find * -name '*.txt' -maxdepth=1 | cpio -o...) | ( cd "$to" && cpio -i...) || die "cpio failed"
Beware that cpio has many options and you should review them before using it.
请注意cpio有很多选项,您应该在使用它之前查看它们。
The commented out technique allows you to more freely move to alternate target directories, which I do not think you need here.
注释掉的技术允许您更*地移动到备用目标目录,我认为您不需要这些目录。
Avoid grief: always quote file names.
避免悲伤:总是引用文件名。
#2
0
Simply append this at the and of your script:
只需将其附加到脚本和脚本:
cp -dR $2 $1
#4
0
Depending on your preferences about conservation of file properties, many one-liner alternatives exist around cp
, tar
or rsync
. Filtering can be obtained using the find
command.
根据您对文件属性保存的偏好,围绕cp,tar或rsync存在许多单行替代方案。可以使用find命令获取过滤。
#1
1
Leaving debugging to the student:
给学生留下调试:
die () { echo >&2 "$*"; echo "Usage...."; exit 1; } from="$1"; to="$2"; [ ."$from" = . ] && die "from dir name missing"; [ ."$to" = . ] && die "to dir name missing"; [ -d "$from" ] || die "from dir $from not a directory"; [ -d "$to" ] || die "to dir $to not a directory"; target="$to/$(basname "$from")"; #final target dir name, if I understand you correctly. find "$from" -name '*.txt' -maxdepth=1 | cpio -pd "$to" || # (cd "$from" && find * -name '*.txt' -maxdepth=1 | cpio -o...) | ( cd "$to" && cpio -i...) || die "cpio failed"
Beware that cpio has many options and you should review them before using it.
请注意cpio有很多选项,您应该在使用它之前查看它们。
The commented out technique allows you to more freely move to alternate target directories, which I do not think you need here.
注释掉的技术允许您更*地移动到备用目标目录,我认为您不需要这些目录。
Avoid grief: always quote file names.
避免悲伤:总是引用文件名。
#2
0
Simply append this at the and of your script:
只需将其附加到脚本和脚本:
cp -dR $2 $1
#3
#4
0
Depending on your preferences about conservation of file properties, many one-liner alternatives exist around cp
, tar
or rsync
. Filtering can be obtained using the find
command.
根据您对文件属性保存的偏好,围绕cp,tar或rsync存在许多单行替代方案。可以使用find命令获取过滤。