用Xargs重命名一堆文件

发布于 2025-02-07 08:00:26 字数 363 浏览 1 评论 0原文

我一直在尝试使用XARGS以适当的顺序重命名一堆文件,但无济于事。在挖掘一堆类似问题的同时,我发现了与Xargs一起使用SED的答案。新手我想避免使用SED。我认为必须有一些简单的方法。

更具体地说,我有一些文件如下:

Abc.jpg
Def.jpg
Ghi.jpg
Jkl.jpg

我希望这些文件以有序的方式重命名,例如:

Something1.jpg
Something2.jpg
Something3.jpg
Something4.jpg

可以xargs命令以及seq实现此处?如果是这样,我该如何实施?

I've been trying to rename a bunch of files in a proper order using xargs but to no avail. While digging around on piles of similar question, I found answers with the use of sed alongside xargs. Novice me wants to avoid the use of sed. I presume there must be some easier way around.

To be more specific, I've got some files as follows:

Abc.jpg
Def.jpg
Ghi.jpg
Jkl.jpg

and I want these to be renamed in an ordered way, like:

Something1.jpg
Something2.jpg
Something3.jpg
Something4.jpg

Could xargs command along with seq achieve this? If so, how do I implement it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剩余の解释 2025-02-14 08:00:26

我不知道为什么有人会为此尝试参与sed。可能不是XARGSSEQ。这是一个纯粹的单线线:

(x=1; for f in *.jpg; do mv "$f" "Something$((x++)).jpg"; done)

从您要重命名的文件上进行循环,这是一个,在每个文件上执行mv命令。要运行的文件通过单个地球表达式表示,但是您也可以单独命名,使用多个地球仪或使用其他各种技术之一。变量x用作简单的计数器,在输入循环之前初始化为1。 $((x ++))扩展到x的当前值,并以1。递增x的副作用。在括号中以在子壳中运行它,因此中的任何内容都不会影响主机壳环境。 (在这种情况下,这意味着它不会在调用shell中创建或修改任何变量x。)

如果您将其放在脚本中而不是在命令行中键入,则会更多可以在几行上拆分它:

(
  x=1
  for f in *.jpg; do
    mv "$f" "Something$((x++)).jpg"
  done
)

如果愿意,您也可以以这种方式输入。

I don't know why anyone would try to engage sed for this. Probably not xargs or seq, either. Here's a pure-Bash one-liner:

(x=1; for f in *.jpg; do mv "$f" "Something$((x++)).jpg"; done)

At its core, that's a for loop over the files you want to rename, performing a mv command on each one. The files to operate on are expressed via a single glob expression, but you could also name them individually, use multiple globs, or use one of a variety of other techniques. Variable x is used as a simple counter, initialized to 1 before entering the loop. $((x++)) expands to the current value of x, with the side effect of incrementing x by 1. The whole thing is wrapped in parentheses to run it in a subshell, so that nothing in it affects the host shell environment. (In this case, that means it does not create or modify any variable x in the invoking shell.)

If you were putting that in a script instead of typing it on the command line then it would be more readable to split it over several lines:

(
  x=1
  for f in *.jpg; do
    mv "$f" "Something$((x++)).jpg"
  done
)

You can type it that way, too, if you wish.

为你拒绝所有暧昧 2025-02-14 08:00:26

这是如何查找,数字和重命名JPG的示例。
无论您如何使用查找(您需要哪些选项)。递归,hivepth,maxdepth,Regex,...)。

您可以添加数字以查找nl的OUPUT,并将数字和文件用作XARGS $ 1的2个参数,$ 2

$ find . -type f -name "*.jpg" |nl| xargs -n 2 bash -c 'echo mv "$2" Something"$1".jpg' argv0

echo echo MV ...将使用排序显示

mv ./Jkl.jpg Something1.jpg
mv ./Abc.jpg Something2.jpg
mv ./Def.jpg Something3.jpg

并测试参数数量

$ find . -type f -name "*.jpg" |sort|nl| xargs -n 2 bash -c '[ "$#" -eq 2 ] && echo mv "$2" Something"$1".jpg' argv0
mv ./Abc.jpg Something1.jpg
mv ./Def.jpg Something2.jpg
mv ./Jkl.jpg Something3.jpg

This is an example of how to find, number and rename jpgs.
Regardless of how you use the find (what options you need. recursive, mindepth, maxdepth, regex, ...).

You can add numbers to find ouput with nl and use number and file as 2 arguments for xargs $1, $2

$ find . -type f -name "*.jpg" |nl| xargs -n 2 bash -c 'echo mv "$2" Something"$1".jpg' argv0

the echo echo mv ... will show this

mv ./Jkl.jpg Something1.jpg
mv ./Abc.jpg Something2.jpg
mv ./Def.jpg Something3.jpg

Using sort and testing the number of arguments

$ find . -type f -name "*.jpg" |sort|nl| xargs -n 2 bash -c '[ "$#" -eq 2 ] && echo mv "$2" Something"$1".jpg' argv0
mv ./Abc.jpg Something1.jpg
mv ./Def.jpg Something2.jpg
mv ./Jkl.jpg Something3.jpg
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文