Fedora Linux Support Community & Resources Center

Go Back   FedoraForum.org > Fedora 17/18 > Using Fedora
FedoraForum Search

Forgot Password? Join Us!

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 26th May 2010, 12:17 PM
theresonant Offline
Registered User
 
Join Date: Apr 2009
Posts: 32
linuxopera
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.
Reply With Quote
  #2  
Old 26th May 2010, 12:30 PM
itsallcrap Offline
Registered User
 
Join Date: Jun 2008
Posts: 16
windows_98_nt_2000firefox
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...
Reply With Quote
  #3  
Old 26th May 2010, 12:35 PM
theresonant Offline
Registered User
 
Join Date: Apr 2009
Posts: 32
linuxopera
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.
Reply With Quote
  #4  
Old 26th May 2010, 01:03 PM
itsallcrap Offline
Registered User
 
Join Date: Jun 2008
Posts: 16
windows_98_nt_2000firefox
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.
Reply With Quote
  #5  
Old 26th May 2010, 01:21 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,104
linuxfedorafirefox
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.
Reply With Quote
  #6  
Old 26th May 2010, 03:20 PM
Mikko Offline
Registered User
 
Join Date: Nov 2008
Location: Finland
Posts: 157
linuxsafari
Re: Accidentally renamed a batch of files to the same name

Quote:
Originally Posted by theresonant View Post
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.
Reply With Quote
  #7  
Old 26th May 2010, 05:37 PM
jpollard Online
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,104
linuxfedorafirefox
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...
Reply With Quote
  #8  
Old 27th May 2010, 06:40 PM
theresonant Offline
Registered User
 
Join Date: Apr 2009
Posts: 32
linuxopera
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.
Reply With Quote
Reply

Tags
accidentally, batch, files, renamed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Batch Converting Files with BASH Starclopsofish Using Fedora 1 10th February 2009 12:00 PM
Batch Removing files :( rm ?? leoleMCH Using Fedora 6 10th January 2006 11:40 PM
Can Fedora natively run batch files (.bat), if not ... .sAm. Using Fedora 6 12th March 2005 11:01 PM


Current GMT-time: 20:38 (Tuesday, 21-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat