 |
 |
 |
 |
| Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum. |

26th May 2010, 12:17 PM
|
|
Registered User
|
|
Join Date: Apr 2009
Posts: 32

|
|
|
Accidentally renamed a batch of files to the same name
Hello,
I just did something stupid... I wanted to remove a numeric prefix from a bunch of files in a folder (EXT4 filesystem), and I ran the following Python commands to rename the files:
Code:
import os
currentdir = os.getcwd()
files = os.listdir(currentdir)
for file in files:
os.rename(currentdir + '/' + file, currentdir + '/' + file[5])
I made a mistake there, on the last line: I wrote "file[5]" instead of "file[5:]", which meant "rename every file to the sixth letter' instead of "rename the file with their name STARTING from the sixth letter". The bad part is that every file had "P" as their sixth letter. In other words, instead of my ~20 files, I now have a single file named "P" in the folder.
Is there a way to reverse this? I'm not very familiar with the inner workings of EXT4, but is there any chance for it? Also, is there an option that I can enable to deny any attempt to rename a file to a name that's already in use?
Cheers!
P.S.: I am ashamed of this stupid mistake... Please forgive a fellow hacker
EDIT: I still have the file names (I was running an interactive Python session, and didn't close it yet - the "files" list is still there)
Last edited by theresonant; 26th May 2010 at 12:28 PM.
|

26th May 2010, 12:30 PM
|
|
Registered User
|
|
Join Date: Jun 2008
Posts: 16

|
|
|
Re: Accidentally renamed a batch of files to the same name
I do believe you're boned.
As I understand it, os.rename in linux equates to the 'mv' command, meaning that you've overwritten the first file in your loop twenty times, finally leaving it with the contents of the last one.
Sorry...
|

26th May 2010, 12:35 PM
|
|
Registered User
|
|
Join Date: Apr 2009
Posts: 32

|
|
|
Re: Accidentally renamed a batch of files to the same name
Nasty.
Luckily, I have a backup, and the files weren't really important. At least I learned a good lesson
Before this issue, the files were occupying about 100 MB. Is this space lost, similar to a memory leak? Or will it be claimed as free space?
Last edited by theresonant; 26th May 2010 at 12:42 PM.
|

26th May 2010, 01:03 PM
|
|
Registered User
|
|
Join Date: Jun 2008
Posts: 16

|
|
|
Re: Accidentally renamed a batch of files to the same name
Oh no, no problem there.
In terms of freeing up disk space it'll just be as if you had deleted all but one of the files.
|

26th May 2010, 01:21 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,104

|
|
|
Re: Accidentally renamed a batch of files to the same name
The way a rename operates:
1 if target file exists, unlink the file (this can cause the inode use count to go to zero)
2 link the new name to the source file (the inode use count of the source is incremented)
3 unlink the source name (this decrements the inode use count)
A file is deallocated whenever the inode use count goes to zero. The use count
is incremented for each hard link to an inode. As the hard links are removed, the
count is decremented.
|

26th May 2010, 03:20 PM
|
|
Registered User
|
|
Join Date: Nov 2008
Location: Finland
Posts: 157

|
|
|
Re: Accidentally renamed a batch of files to the same name
Quote:
Originally Posted by theresonant
P.S.: I am ashamed of this stupid mistake... Please forgive a fellow hacker 
|
If it's any consolation, I once did the equivalent of 'rm -rf *' in the root directory / of a SGI Irix system. I forget the exact command. I'd inadvertently run as root something that created a bunch of hidden dotfiles in the / directory, and then tried to clean them up, but got tripped by the fact that wildcarding the initial '.' didn't quite work the same way in the Irix version of ksh as it does in bash, which I'd been used to. Running the command started to take much longer than it should have, as the whole system was being wiped. I had a 'o shi!' moment and hit ctrl-c. After that, doing 'ls' returned 'command not found'. Fortunately the system wasn't critical at all, so I simply reinstalled Irix (the installer of which btw. positively sucked compared to Linux even then). This was a long time go in a galaxy that I unfortunately still haven't got very far from.
|

26th May 2010, 05:37 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,104

|
|
|
Re: Accidentally renamed a batch of files to the same name
I did it with the SGI backup abomination called BRU. It just could not backup the
system disk, nor restore it to an alternate disk...
|

27th May 2010, 06:40 PM
|
|
Registered User
|
|
Join Date: Apr 2009
Posts: 32

|
|
|
Re: Accidentally renamed a batch of files to the same name
Hm... Now I remember how I was using GParted and absent-mindedly erased my Home partition. Yes, I know, "GParted" and "absent-mindedly" should never be used in the same sentence, but it happened. Unfortunately, I was very unexperienced in these matters then, therefore, while trying to recover my partition, I screwed up the whole MBR  I had to reinstall everything... Luckily, I was in a live-cd session, and some of my other partitions were still mounted - I got the chance to recover all the data from them.
While Linux systems are quite fault-tolerant, there's no barrier against the system and our innate instinct of accidentally breaking things.
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 20:38 (Tuesday, 21-05-2013)
|
|
 |
 |
 |
 |
|
|