0

I am trying to rename a file using the move (mv) command

$ls

-0
-1
-2

to

000.json
001.json
002.json

the code I tried

for i in -*; do
  N=3
  echo mv -v -- "\"""$i""\"" "$(echo "$i" | sed -r ":r;s/\b[0-9]{1,$((N - 1))}\b/0&/g;tr" | sed "s/-//" | sed "s/$/.json/")"
  mv -v -- "\"""$i""\"" "$(echo "$i" | sed -r ":r;s/\b[0-9]{1,$((N - 1))}\b/0&/g;tr" | sed "s/-//" | sed "s/$/.json/")"
done

this code outputs

mv -v -- "-0" 000.json
mv: cannot stat '"-0"': No such file or directory
mv -v -- "-1" 001.json
mv: cannot stat '"-1"': No such file or directory
mv -v -- "-2" 002.json
mv: cannot stat '"-2"': No such file or directory

If I try to execute mv -v -- "-0" 000.json this works. but when I try to do it with mv -v -- "\"""$i""\"" "$(echo "$i" | sed -r ":r;s/\b[0-9]{1,$((N - 1))}\b/0&/g;tr" | sed "s/-//" | sed "s/$/.json/")". It won't work.

Can anybody help me fixing mv -v -- "\"""$i""\"" ... this part of command Where in "\"""$i""\"" I am just trying to add quotes? So It give No such file or directory.

Mohit Kumar
  • 552
  • 9
  • 29
  • The solution is the same as when the value does not come from a variable. Probably the easiest way to fix your code is to use `for i in ./-*` – tripleee Mar 06 '23 at 08:47
  • More quotes do nothing to solve this problem, because the problem is not related to quoting. Quotes simply tell _the shell_ to not perform various changes to the string, but the problem here is not that the shell passes the wrong value, it's that the value has a special meaning to `mv` – tripleee Mar 06 '23 at 08:49
  • how can this help `for i in ./-*` . it still gives `mv: cannot stat '"./-2"': No such file or directory ` – Mohit Kumar Mar 06 '23 at 08:50
  • 1
    You don't have to use external commands such as `sed` for this task. This should do the trick at once: `for f in -[0-9]*; do echo mv -- "$f" "$(printf '%03d.json' "${f:1}")"; done` – M. Nejat Aydin Mar 06 '23 at 08:50
  • @M.NejatAydin thx a lot. your comment worked. Can you post it as an answer so that I can accept it. – Mohit Kumar Mar 06 '23 at 08:52
  • Because you have the extra quotes in the body of the loop. You just want `mv -v "$i" "00${i#-}.json"` inside the loop, though that won't work if you have more than nine files. – tripleee Mar 06 '23 at 08:57
  • 1
    Tangentially, see also https://stackoverflow.com/questions/7657647/combining-two-sed-commands – tripleee Mar 06 '23 at 08:59
  • 1
    Closed questions cannot receive answers. This is a common duplicate which has been answered dozens or hundreds of times before. – tripleee Mar 06 '23 at 09:00
  • This works... `ls | sed -E 's/-([0-9]+)/mv .\/-\1 0\1.json/g' | bash` – hacker315 Mar 06 '23 at 09:45

1 Answers1

1

Basically, to avoid errors on files starting with a dash -, use:

command -- *
command ./*

Using Perl's rename:

Remove -n switch, aka dry-run when your attempts are satisfactory to rename for real.

rename -n 's/-(\d+)/sprintf("%.3d%s", $1, ".json")/e' ./*
rename(-0, 000.json)
rename(-1, 001.json)
rename(-2, 002.json)
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223