What have I done?


#1

I made a big mistake… I typed in.
for file in *; do mv $file $file:4}; done
When I really meant to type
for file in *; do mv $file ${file:4}; done

I was just trying to remove the first 4 characters. I’m not sure what I have done. Can I reverse this?


#2

You did this in bash, yes? That style of for loop and the string operation is a bashism.

If before doing this ls showed something like:
tom.txt dick.txt larry.txt
after it probably shows
tom.txt:4} dick.txt:4} larry.txt:4}

Does it look something like that?
something here should be able to help. Look at the “Index” and substring extraction stuff.
http://www.tldp.org/LDP/abs/html/string-manipulation.html


#3

I wish… I could have removed the extra characters if need be. Their was about 100 files named ABC_1234, ABC_567, ABC_890, etc… I wanted to remove the ABC_ from each one, and I got file names like.

AZG9b0~L. I had a file named renamer.bat and it is now called RXSQ9F~G


#4

That is odd. Did you take a snapshot fo the dataset before you did this? :slight_smile:
I know it’s too late now, but I would have made inside the loop something like:
fname=${file:4}
mv $file $fname

Just to make sure.

I just did the foloowing on a Linux system running bash 4.3.43 and got what I expected.
[mremski@ENGPC2UA2120460 blah]$ touch abc_1234.txt
[mremski@ENGPC2UA2120460 blah]$ touch abc_1235.txt
[mremski@ENGPC2UA2120460 blah]$ touch abc_1265.txt
[mremski@ENGPC2UA2120460 blah]$ touch abc_1365.txt
[mremski@ENGPC2UA2120460 blah]$ touch abc_3365.txt
[mremski@ENGPC2UA2120460 blah]$ ls
abc_1234.txt abc_1235.txt abc_1265.txt abc_1365.txt abc_3365.txt
[mremski@ENGPC2UA2120460 blah]$ for file in *
> do
> echo $file $file:4}
> done
abc_1234.txt abc_1234.txt:4}
abc_1235.txt abc_1235.txt:4}
abc_1265.txt abc_1265.txt:4}
abc_1365.txt abc_1365.txt:4}
abc_3365.txt abc_3365.txt:4}

what language, keyboard, locale do you have defined? That’s the only thing I can think of.


#5

I did a echo $SHELL and it shows /bin/sh
I’m also using a English Language, U.S. Keyboard, and North America is the Locale.


#6

A. bash: OK
% bash -c 'echo “${SHELL:2}”'
in/csh

B. sh: NOT_OK
% sh -c 'echo “${SHELL:2}”'
sh: ${SHELL:2}: Bad substitution
Exit 2

C. sh: OK
% sh -c 'echo “${SHELL#??}”'
in/csh

D. Precautionary Measures:
Make ‘mv’ an alias for ‘mv -iv’.


#7

This topic should be renamed to something more meaningful.


#8

I’m not sure what I did, or if I could reverse it. That’s why I was asking, What I had done, and if I could reverse it.

What would a more meaningful name be?


#9

If the results are different than what I saw in #4, I don’t know if there is a way to recover.


#10

Regarding funny, new file names:

Did You do Your operations on a 12 Bit FAT MSDOS filesystem?

Mounted with a non-“UTF-8” IBM/Microsoft code page?

Or was this on a SaMBa mount point, configured for
stone age compatibility?


#11

Topic-name Suggestion:

Files renamed similar to old PC/DOS format. But why and how?


#12

This is one of those Linux based SANS. I’m not sure which one, but it’s used to share files with Windows machines here. So I’m going to say yes to the "Samba mount point, configured for compatibility.

The scanned file inside the misnamed folder had a hint at what the end of the file name would actually be, and also a machine that would be attached to that file name, then I had to go into an excel file (I’m looking into SC, grep and regular expressions might come in handy) but now I just looked in excel for that machine on that day and figure out what the first part of the file name would be. I just now finish matching all of them and renaming all the folders (I should say most of the folders) Some of them didn’t have “the hint.”

It was a lot of work and a lot of unpaid overtime, but at least I won’t be looking for another job for this mistake like I had originally thought. 98% recovery was pretty good.


#13

That’s good. A lot times when I do loops like this I put a few echos of the important stuff and comment out the actual work (the mv in your case). Then I run it a few times looking at the output, because the string manipulation in bash always takes me a few times to get right.

Very glad you were able to figure something out.