使用Bash 3 及更高版本中可用的大括号扩展,以及 Zsh 和其他几个 shell:*
mv IMG_{9431..9999}.jpg some_other_dir
Run Code Online (Sandbox Code Playgroud)
大括号将扩展为 9431 和 9999 之间的所有数字,因此相当于写出:
mv IMG_9431.jpg IMG_9432.jpg … IMG_9999.jpg some_other_dir
Run Code Online (Sandbox Code Playgroud)
如果文件太多,这将失败(有关ARG_MAX
背景信息,请参阅这篇文章。)
如果你的 shell 缺少大括号扩展功能,或者你有太多的文件,你可以这样做——这可能会慢一点:
for n in $(seq 9431 9999); do mv "IMG_$n.jpg" some_other_dir; done
Run Code Online (Sandbox Code Playgroud)
如果你真的想按字典顺序排序,看看??????? ?????????????的回答。比这里接下来的要好得多。
您必须以某种方式对文件名进行排序。这就是我很快想到的,它既不漂亮也不理想。它适用于 GNU/Linux ( grep
, sort
, xargs
) 并处理任何文件名,包括引号和空格。
tmp="$(mktemp /tmp/files.XXX)"
find . -type f -name 'IMG*' -maxdepth 1 -print0 | sort -z > "$tmp"
line=$(grep -nz IMG_9984.jpg "$tmp" | cut -d: -f1)
tr '\0\n' '\n\0' < "$tmp" | tail -n "+$line" | tr '\0\n' '\n\0' |
xargs -0 -I{} echo mv {} some_other_dir
rm "$tmp"
Run Code Online (Sandbox Code Playgroud)
echo
当您确定这符合您的要求时,请删除它。我们在这里做什么:
创建一个临时文件来保存文件名。
找到所有匹配该模式的文件,并将它们排序到一个临时文件中。记录由NUL
字符 ( -print0
, -z
)分隔,因此我们可以处理任何文件名。
找到文件名的“行”号,例如 IMG_9984.jpg
NUL
在临时文件中交换和换行以便tail
可以处理它。
将它们交换回来,以便xargs
可以处理它 ( -0
) 并将mv
文件转移到另一个目录。
如果我们不必处理包含引号或空格的文件,这会更容易,但是……那只是我。安全总比后悔好。
您可以使用sort
和sed
来获取比某些字符串更大的文件列表,如下所示:
$ ls -v\n0?#Li 23?24 E.See NULib Yoush ce-Su edint ethat ibble itwil lines of16 plesA ryGen t6?#C witht #ver\n0?#mo 25?i Examp NYWAR along cribb edist ev\xc2\xa0\xc2\xa0\xc2\xa0 ibrar ix\xc2\xa0B. mapfo ofthe ppend sdist tetot y8?#9 (atyo\n0,22 27?# FORAP NextA areFo dacop edwar frees ic11 lPubl mored oftwa publi sefor theGN yGene )1995\n1.scr 02111 Finla Peter aryis datio eful, ftheG ight( landJ mport on23# ralPu se,or tunde yofth ,USA.\n1-200 ARTIC GIMPT RANTY avere difyi eitan ftwar imbal lbeus ncerK on,MA raryG shedb t,wri ytheF ;ifno\n2John Appen HANTA Softw bleof dix\xc2\xa0B enera ght(C impli ld7?# nc.,5 oolki raryi simpl ublic #19?# ;with\n4?#Th BILIT HOUTA TNESS blic2 e.py4 enthe gtk26 ingar lePla ngpix opyri rdraw sion. ucanr #Bost \xc2\xa0Code\n5?#GT Backi K-The U17?# brary e.pyB eralP he21 ion)a lesPr nseas oshMa reeSo sion2 undat #Free\n9Temp Conte Libra ULARP cDona eExam ermso hehop ion,I lesTa ntsB. ouldh re;yo slibr uropt #Lice\n13?#1 Copyr Licen URPOS ceive eFoun erver her12 islib lescr nylat outev ribut s,Spe utWIT #MERC\n15?#b C)200 Matti YorFI cense eGNUL etail hisli ite33 lesim n;eit ple.p rthet s.18 vpyth #Thi\n\n$ mkdir greater-than-sion\n
Run Code Online (Sandbox Code Playgroud)\n\n这就是魔法:
\n\n$ find -type f -print0 | \n sort -z |\n sed -z '1,/sion/d' | \n xargs -0 mv -t greater-than-sion\n
Run Code Online (Sandbox Code Playgroud)\n\n线路:
\n\nNUL
换行符 ( -print0
)分隔sion
) \xe2\x80\x93 请注意,这仅适用于 GNU sed
,它实现了-z
解析NUL
终止输入的选项mv
给xargs
以及期望的结果:
\n\n$ ls -R\n.:\n\xc2\xa0Code ;ifno 25?i avere Conte edist erver GIMPT ic11 K-The lines nseas opyri raryG se,or\n#19?# ;with 27?# Backi Copyr edwar etail greater-than-sion/ ight( landJ lPubl ntsB. oshMa raryi sefor\n#Bost 0?#Li 2John BILIT cribb eExam ethat gtk26 imbal lbeus mapfo NULib ouldh rdraw shedb\n#Free 0?#mo 4?#Th bleof dacop eFoun ev\xc2\xa0\xc2\xa0\xc2\xa0 HANTA impli ld7?# Matti nylat outev re;yo simpl\n#Lice 0,22 5?#GT blic2 datio eful, Examp he21 ingar lePla mored NYWAR Peter reeSo sion.\n#MERC 02111 9Temp brary difyi eGNUL Finla hehop ion)a lescr mport of16 ple.p ribut\n#Thi 1.scr along C)200 dix\xc2\xa0B eitan FORAP her12 ion,I lesim n;eit ofthe plesA rthet\n#ver 1-200 Appen cDona e.py4 enera frees hisli islib lesPr nc.,5 oftwa ppend ryGen\n(atyo 13?#1 areFo ceive e.pyB enthe ftheG HOUTA ite33 lesTa ncerK on,MA publi s,Spe\n)1995 15?#b ARTIC cense E.See eralP ftwar ibble itwil Libra NextA on23# ralPu s.18\n,USA. 23?24 aryis ce-Su edint ermso ght(C ibrar ix\xc2\xa0B. Licen ngpix oolki RANTY sdist\n\n./greater-than-sion:\nsion2 Softw t6?#C theGN tunde ublic ULARP uropt utWIT witht yGene YorFI ytheF\nslibr t,wri tetot TNESS U17?# ucanr undat URPOS vpyth y8?#9 yofth Yoush\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
1329 次 |
最近记录: |