linux - 文件名和文件通配

标签 linux bash match filenames glob

我正在尝试找出问题,这非常有帮助 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/

相关文章:

python - 如何在 Windows 10 上的 VSCode 中激活 conda 环境

javascript - 匹配正则表达式 : [everything before number], [数字]

linux - 在Linux中,用户空间如何不能访问内核空间?

python - 是否可以运行连接到 virtualbox 实例的 pydev?

linux - 如何为安装程序编写 "yes"响应脚本?

linux - 文件 shell 脚本的编码

python - 如何在 Pandas 中有效地执行相当于 Excel MATCH 功能(小于)的功能?

regex - 在 Perl 正则表达式 "find & replace"中,如何单独挑选每个匹配结果?

c++ - 读取 MAC 地址时文件读取异常

linux - xdebug phpstorm docker linux