Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Shell Script gone wrong!

Posted on 2004-08-16
Medium Priority
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 200 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).
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Assisted Solution

da99rmd earned 100 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 100 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

636 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