Solved

Shell Script gone wrong!

Posted on 2004-08-16
8
234 Views
Last Modified: 2013-12-06
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
Comment
Question by:garry_m
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 40

Expert Comment

by:jlevie
Comment Utility
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
 

Author Comment

by:garry_m
Comment Utility
Unfortunately the target dir was a directory that didn't exist, just some rubbish I typed in.
0
 
LVL 40

Accepted Solution

by:
jlevie earned 50 total points
Comment Utility
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
 
LVL 8

Assisted Solution

by:da99rmd
da99rmd earned 25 total points
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:garry_m
Comment Utility
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
 
LVL 23

Assisted Solution

by:brettmjohnson
brettmjohnson earned 25 total points
Comment Utility
> 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
 
LVL 8

Expert Comment

by:da99rmd
Comment Utility
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
 

Author Comment

by:garry_m
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

If you use Debian 6 Squeeze and you are tired of looking at the childish graphical GDM login screen that is used by default, here's an easy way to change it. If you've already tried to change it you've probably discovered that none of the old met…
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now