我正在尝试找出问题,这非常有帮助 Linux file names & file globbing 但我仍然有问题。
我的 linux 系统目录中有超过一百万个文件。我需要将文件名小于或等于某个数字的文件复制到另一个目录。 例如:
将文件名小于或等于编号 29108273357520896 的所有文件复制到另一个目录。
有人可以帮我解决这个问题吗? [][] 这件事让我很困惑。
最佳答案
您可以很容易地将 dir1
复制到 dir2
0 - 29108273357520896
之间的每个文件:
#!/bin/bash
declare -i maxval=29108273357520896
function usage {
cat >&2 << TAG
Copy all files from 'srcdir' to 'tgtdir' with numeric names less than 'maxname'.
Usage: "${0//*\//}" srcdir tgtdir [maxname] (maxname default: $maxval)
TAG
exit 1
}
## test required input
if [ -z "$1" -o -z "$2" ]; then
printf "\n error: insufficient input.\n"
usage
fi
## assign variables
srcdir="$1"
tgtdir="$2"
declare -i maxname="${3:-$maxval}" # default maxval
## validate srcdir
if [ ! -d "$srcdir" ]; then
printf "\n error: source dir does not exist.\n"
usage
fi
## validate or create tgtdir
[ -d "$tgtdir" ] || mkdir -p "$tgtdir"
if [ ! -d "$tgtdir" ]; then
printf "\n error: tgtdir does not exist and cannot be created, check permissions.\n"
usage
fi
## validate maxname
if [ $maxname -gt $maxval ]; then
printf "\n error: invalid 'maxname'. value exceeds maximum allowed: %s\n" "$maxval"
usage
fi
## for 0 - $maxname, check that file exists, if so copy to tgtdir
for ((i=0; i<$maxname; i++)); do
[ -f "$i" ] && cp -a "${srcdir}/${i}" "${tgtdir}"
done
exit 0
作为文件目录中的一行
for ((i=0; i<29108273357520896; i++)); do [ -f "$i" ] && cp -a "$i" "/path/to/new/dir"; done
关于linux - 文件名和文件通配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27413916/