Shell Script gone wrong!

Posted on 2004-08-16
Last Modified: 2013-12-06

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?



I wont make this mistake again, lol!
Question by:garry_m
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
LVL 40

Expert Comment

ID: 11811760
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.

Author Comment

ID: 11811790
Unfortunately the target dir was a directory that didn't exist, just some rubbish I typed in.
LVL 40

Accepted Solution

jlevie earned 50 total points
ID: 11812155
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).
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.


Assisted Solution

da99rmd earned 25 total points
ID: 11812195
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.


Author Comment

ID: 11812263
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.

LVL 23

Assisted Solution

brettmjohnson earned 25 total points
ID: 11812973
> 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

Expert Comment

ID: 11817854
>>  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.
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 ?

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.
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.


Author Comment

ID: 11822486
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.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
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…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

730 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