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


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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error Message during CentOS 7 Minimal Install 3 41
Use CRON to save a copy of an open file 1 39
bash file 10 34
how to print sub line with  sed /awk /perl in bash file 11 22
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

813 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

15 Experts available now in Live!

Get 1:1 Help Now