I manage the file name as a number like 111.jpeg, and I want to re-number it all at once with xargs-n2mv, but some files will disappear.

Asked 1 years ago, Updated 1 years ago, 423 views

ls
1.jpeg
2.jpeg
3.jpeg
11.jpeg
12.jpeg
13.jpeg
101.jpeg
102.jpeg
103.jpeg

I want to increase the number by 2

ls|seds/\.jpeg//|awk'{print$1".jpeg""$1+2".jpeg"}'
1.jpeg 3.jpeg
101.jpeg 103.jpeg
102.jpeg 104.jpeg
103.jpeg 105.jpeg
11.jpeg 13.jpeg
12.jpeg14.jpeg
13.jpeg 15.jpeg 
2.jpeg 4.jpeg
3.jpeg 5.jpeg

If you put this in xargs-n2mv,

ls|seds/\.jpeg//|awk'{print$1".jpeg""$1+2".jpeg"}'|xargs-n2mv
ls
104.jpeg 105.jpeg 14.jpeg 15.jpeg 4.jpeg 5.jpeg

101.jpeg, 11.jpeg, and 1.jpeg disappear

If you try minus, it will work.

ls|seds/\.jpeg//|awk'{print$1".jpeg""$1-2".jpeg"}'|xargs-n2mv
ls
-1.jpeg0.jpeg1.jpeg10.jpeg100.jpeg101.jpeg11.jpeg9.jpeg99.jpeg

Try printf

ls|sed-es/.jpeg//|awk'{printf("%d%s%d%s\n", $1, ".jpeg", $1+2, ".jpeg")}'
1.jpeg 3.jpeg
101.jpeg 103.jpeg
102.jpeg 104.jpeg
103.jpeg 105.jpeg
11.jpeg 13.jpeg
12.jpeg14.jpeg
13.jpeg 15.jpeg
2.jpeg 4.jpeg
3.jpeg 5.jpeg

ls | sed-es /.jpeg // | awk' { printf("%d%s%d%s\n", $1, ".jpeg", $1+2, ".jpeg")}' | xargs-n2mv
ls
104.jpeg 105.jpeg 14.jpeg 15.jpeg 4.jpeg 5.jpeg

As expected, 101.jpeg, 11.jpeg, and 1.jpeg disappear.
1. Try jpeg to 136.jpeg

ls
10.jpeg 100.jpeg 101.jpeg 11.jpeg 137.jpeg 138.jpeg

Files other than 8,9,98,99,135,136 will disappear.
I don't know what the law is.
I don't have basic knowledge, so I can't solve it even if I refer to it online.
I'm sorry to bother you, but I'd appreciate your help.

macos command-line zsh shell

2022-10-30 00:01

2 Answers

Why it doesn't work out as intended

ls|seds/\.jpeg//|awk'{print$1".jpeg""$1+2".jpeg"}'|xargs-n2mv

For

If you don't run mv and see what commands are running on echo,

ls|seds/\.jpeg//|awk'{print$1".jpeg""$1+2".jpeg"}'|xargs-n2 echo mv

The results are as follows:

mv1.jpeg3.jpeg
mv101.jpeg103.jpeg
mv102.jpeg104.jpeg
mv103.jpeg105.jpeg
mv11.jpeg13.jpeg
mv12.jpeg14.jpeg
mv13.jpeg15.jpeg
mv2.jpeg4.jpeg
mv3.jpeg5.jpeg

After changing 101.jpeg to 103.jpeg, I changed 103.jpeg to 105.jpeg.

Workaround

ls-r would work instead of ls.

 ls-r | sed s/\.jpeg//|awk'{print$1".jpeg""$1+2".jpeg"}'|xargs-n2 echo mv

Results

mv3.jpeg5.jpeg
mv2.jpeg4.jpeg
mv13.jpeg15.jpeg
mv12.jpeg14.jpeg
mv11.jpeg13.jpeg
mv103.jpeg105.jpeg
mv102.jpeg104.jpeg
mv101.jpeg103.jpeg
mv1.jpeg3.jpeg

[Additional note]
The ls sorting is dictionary order, so there is a problem.
For example,

1.jpeg101.jpeg102.jpeg103.jpeg11.jpeg12.jpeg13.jpeg2.jpeg3.jpeg9.jpeg

In the case of , after changing 9.jpeg to 11.jpeg, change 11.jpeg to 13.jpeg.

Sort by number to avoid this problem.

 ls-1|sort-rg|seds/\.jpeg//|awk'{print$1".jpeg""$1+2".jpeg"}'|xargs-n2 echo mv

ls-r is changed to ls-1|sort-rg and sorted numerically.


2022-10-30 00:01

The cause has already been determined, so for your information, I will list how to use arithmetical expansion.Since it is a POSIX component, I think it is a function other than bash (in fact, it can be done with dash).

$commandls-1r | while IFS=.read-raxt; domv "$a.$ext""$(a+2)).$ext"; done

add

The ls command in GNU coreutils has an optional switch (-v) that sorts file names in numerical order.

$ls --version
ls (GNU coreutils) 8.32

$ mans

- v natural sort of (version) numbers with text


2022-10-30 00:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.