[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

Shell Script gone wrong!

Hi,

I am pretty new to Linux and was attempting to make a shell script that would move all the files from one directory to another.  It looked something like this:

(Please note, the syntax is not correct, its just to give you an idea of what i was doing)

read sourceDir;
read targetDir;

cd $sourceDir;

for x in *; do mv $x $targetDir; done;

When I was testing it I just put some rubbish in the inputs, such as "asdsad"

When the script run, it moved all of the files from the root of the drive (not the root folder) and they all appear to be deleted.  I know this was a stupid mistake, but I was hoping there might be some sort of roll-back I can perform.  At the moment all of my files have been deleted and the OS is crippled or non-existant.

Is there any possible way to get the old data back from this system?

Thanks

Garry

I wont make this mistake again, lol!
0
garry_m
Asked:
garry_m
  • 3
  • 2
  • 2
  • +1
3 Solutions
 
jlevieCommented:
When you ran the script what was $targetDir? That's where the data got moved to and you should be able to move things back.
0
 
garry_mAuthor Commented:
Unfortunately the target dir was a directory that didn't exist, just some rubbish I typed in.
0
 
jlevieCommented:
The data may still exist and you may be able to find it by booting up into "rescue" mode from your distro CD or use one of the mini-Linux distros (Knoppix, tomsrtbt, etc).
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
da99rmdCommented:
Hi garry_m,
then mv should have complained. and dont moved the files.

else it would have renamed the input dirs to the output dir.
This would have mv the first dir to the rubbish name and then moved the outher dirs to tha dir.
So in the source dir you will have a file/dir named <rubbish> and the outher files dirs inside it so mv the files/dirs inside the dir back first and then mv the <rubbish> dir bask to its orignial name.

/Rob
0
 
garry_mAuthor Commented:
When the command had completed I did an 'ls' and it said that no files were found, but I could still navigate through the directories.  The directories didn't appear in the ls command, but it looks like they were still existant.

The only problem is I rebooted the machine in the hope that it might sort itself out, now it throws a load of errors while booting and doesn't come to any command prompt.  Am going to investigate the rescue option.  The OS was Red Hat 8.0 so hopefully should be able to find something on it.

Thanks
0
 
brettmjohnsonCommented:
> Unfortunately the target dir was a directory that didn't exist, just some rubbish I typed in.

That means that each file was subsequently renamed to the "rubbish" name you typed in.
Only the last file remains intact - under the rubbish name you provided.  The rest were lost.


> When the script run, it moved all of the files from the root of the drive (not the root folder)
> and they all appear to be deleted.

That means you were running as the root user, giving your self more than enough rope
with which to shoot yourself in the foot.  It is rarely a good idea to be running as root
in a general login session because you open yourself up to these types of unrecoverable
errors that can render your system unusable.  It is much better to run as an unprivileged
user and use 'sudo' to perform privileged operations when you really know what you are
doing.
0
 
da99rmdCommented:
brettmjohnson,
>>  Unfortunately the target dir was a directory that didn't exist, just
>> some rubbish I typed in.

> That means that each file was subsequently renamed to the "rubbish"
> name you typed in.
> Only the last file remains intact - under the rubbish name you provided.  
> The rest were lost.
case:
1. The first file is a file, the file is renamed to <rubbish> the rest of the moves will complain and not be made if not set in -f mode.
2. The first is a dir then the dir is renamed to <rubbish> the rest of the dir files is moved to that <rubbish> dir this even if set to -f.
3. The hole path didnt exixt then mv will have complained big times not moving any files at all.

So I think what append was alternative 2.

What was the rubbish name you put as a outdir ?
if this was what append you can get your files back.

The default mode of mv is not to owerwrite any files at all.

What files and in what dir was that you ran the command in ?

Alternatives:
1.
Do as jlevie said and donload a knoppix cd and burn it. Then start your computer on this cd and mount your linux partition and try to set things back as they where.
2.
Run the RH 8.0 cdrom and make an upgrade or repair of your intsallation if this was system files then you will be able to keep all your settings files and all other files.

/Rob
0
 
garry_mAuthor Commented:
Thanks guys, I did what jlevie said and tried the rescue option from Red Hat, but unfortunately the files where all over the place and I dont have the expertise to be able to recover them all and put the OS back together, so I went for a fresh install. It was only a test machine anyway, so wasn't too fussed about loosing stuff on it.  Thanks again for the help.
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now