0

The original title was: Injecting vidir with vim like sequence of commands

I have been trying to use vidir to rename multiple files which are accidently renamed by someone. Problem is vidir doesnt use sorted order of files it sometimes picks randomly. The way my files are messed up i have to rename them using ls -rt

Problem: Files all randomly renamed. But i have a text file containing names of all files in ls -rt order. Wanted to use that text file read the names and rename files in folder using ls -rt order

ls -rt output

cbb308e52a3345e4ff436b2f
a4b8e2aacae0a3304b3c28b8
43ca3253da336012788d3279~
ada694316cc605c7ffb790d1
3b6cc01ff2aaa5b1709918ef
7057cbda90a7adf303a56bb4~
7c0dd50bc52bb444000cc175

Txt_with_Names_In_Order

e0e87002d27674ddafdb5f7077887da6-1.ps
c7685093e84da8074859efc5783687b6-2.ps
062936279f54f4f5a541b5bee4daf5d4-3.ps
6f91bb10840a4a5a5331c832e5c2beb1-4.ps
9a8c5e9ae8b633a2a588518d2b9ab656-5.ps
bcb77709b10eafbd2fc88ce8db41ed3f-6.ps
e65c35be3c8b0aa051fb33ed2ce43ff7-7.ps

Solutions:

Now,It can be done by loop but writing long loop command is not feasible, when vidir comes to handy very quickly.

My vim command sequence includes

^[ggVGg^A some regex
ls -rt | vidir -

Above works But it requires interactive working.

Wanted to inject sequence like

ls -rt | vidir - << EOF
commands
EOF

OR

vim file << EOF sequence EOF ------> it works
command | vim - << EOF sequence of vim commands to edit input ELF ----> Want a workaround for this

But input seems to be conflicting.

Can anyone help if workaround is possible.

[Not want to use expect] Can anyone help with:

how to use ^[ggVgg^A directly from command line in vim ex mode.?

What i am trying is to pipe output of some command to vim and also inject sequence of vim commands to act on that piped input non interactively

1
  • 1
    Scripting vim is not ideal. Show what you are trying to achieve and we may come up with a more efficient and rubust solution.
    – they
    Jan 13 at 14:38

2 Answers 2

1

Your request to find a solution based on vim or vidir is a typical XY problem.

As I understand your question, the real requirement is to rename the existing files in the current directory to the names in a text file which is in ls -rt order.

The following solution for bash assumes that both the current and saved file names don't contain spaces, newlines or other problematic characters, and that the text file contains one file name per line.

readarray -t new < /path/to/filenames.txt ; \
i=0 ; \
ls -rt1 | while read -r old
do
    echo mv "$old" "${new[$i]}"
    ((i++))
done

The script above will only show the commands that would be executed. If the output matches your requirements, remove the echo before mv and run the script again to actually rename the files.

1

This can be easily solved with shell commands (bash). First of all backup the folder.

Create a file containing the bad names in correct order: ls -tr > badnames.txt So these two files have the good and bad names with the correct order. Merge them together:

paste badnames.txt TXT_with_Names_In_Order > bothnames.txt

and finally type:

while read BADNAME GOODNAME
do
mv $BADNAME $GOODNAME
done < bothnames.txt

This chunk of code reads one line of your bothnames.txt file, and splits it in two variables. then it uses the mv command to rename your files.

2
  • It may work with the names in question, but in general the solution is fragile at least because of what read does, possible spaces and such. Jan 13 at 20:26
  • The advantage of this approach is that you have a file with the old and new names per line, so you can decide if it looks correct prior to processing it.
    – Tim B
    Jan 14 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.