mv duplicating directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting mv duplicating directories
# 1  
Old 07-02-2011
mv duplicating directories

Hi Folks,
I've put together a script for sorting my backup files into sub folders to be run from a cron job. Each file is named username.tar.gz and the file /etc/trueuserowners contains all users and their owner in the format "user: owner".
The script works fine identifying users and their owners and is as follows:

Code:
#!/bin/bash
_w="/path/to/backups"
_wb="$_w/*.tar.gz"

if [ -d "$_w" ]; then
        for f in $_wb
        do
                _f=$(basename $f)
                _user=${_f%%.*}
                _o=`grep "^$_user:" /etc/trueuserowners`
                _owner=${_o#* }
                if [ ! -d "$_w/$_owner" ]; then
                        `mkdir $_w/$_owner/`
                fi
                `mv $f $_w/$_owner/`
        done
fi

Say I have the following:

/path/to/backups/userabackup.tar.gz
usera is owned by resellera.

The first time the script runs, the script will create the following directory and move userabackup.tar.gz into it:

/path/to/backups/resellera

The script has an if in it to ensure that /path/to.backups/resellera is only created if it does not already exist as you can see. My problem is that the mv command appears to be backing up the directory and creating a new one instead of moving the file into the existing directory and replacing the file if it is already there.

For example:

The second time the script runs, /path/to/backups/resellera will be renamed to /path/to/backups/resellera.1 and a new /path/to/backups/resellera is created.
The third time, /path/to.backups/resellera.1 is renamed /path/to.backups/resellera.2, /path/to.backups/resellera to /path/to.backups/resellera.1 and a new directory created.

I shouldn't need to have the script remove the directory first and the mv command does not create backup directories like this when run from the console. The VERSION_CONTROL environment is not set.
Is there something specific that makes mv act like this when run from a cron job? Do I have to tell it to over-write instead of create these backups?
# 2  
Old 07-02-2011
Quote:
#!/bin/bash
_w="/path/to/backups"
_wb="$_w/*.tar.gz"

if [ -d "$_w" ]; then
for f in $_wb
do
_f=$(basename $f)
_user=${_f%%.*}
_o=`grep "^$_user:" /etc/trueuserowners`
_owner=${_o#* }
if [ ! -d "$_w/$_owner" ]; then
`mkdir $_w/$_owner/`
fi
`mv $f $_w/$_owner/`
done
fi

1) Lose the backticks. In this context they are neither necessary or desirable.
e.g. `mv $f $_w/$_owner/` becomes mv $f $_w/$_owner/

2) There is no code posted which mentions $VERSION_CONTROL

3)
Quote:
Is there something specific that makes mv act like this when run from a cron job? Do I have to tell it to over-write instead of create these backups?
What is "act like this"? Please post detail of an example.
This User Gave Thanks to methyl For This Post:
# 3  
Old 07-03-2011
Quote:
Originally Posted by methyl
1) Lose the backticks. In this context they are neither necessary or desirable.
e.g. `mv $f $_w/$_owner/` becomes mv $f $_w/$_owner/
That seems to have done the trick, no more backed up directories. Cheers

Quote:
2) There is no code posted which mentions $VERSION_CONTROL
It is an envirmont variable that can be set instead of the --backup option for mv. Documented in man mv, I only mentioned it so that noone suggested it as a possible explanation.

Quote:
3)
What is "act like this"? Please post detail of an example.
I already did, I was referring to mv "backing up" the current destination directory as dirname.1 instead of simply overwriting any files in the existing directory.

Actually I did make a slight mistake, the numbering of the backups starts at 0, not 1

Last edited by beddo; 07-03-2011 at 06:57 AM..
# 4  
Old 07-10-2011
Apparently this didn't fix it. For reference I have "alias mv='mv -i'" in my .bashrc so I've change the mv to mv -f and am hoping that'll sort it out tonight.
# 5  
Old 07-26-2011
Apparantly I still don't have a fix for this..something like this should be nice and simple!
# 6  
Old 07-26-2011
Please post the current version of the script and mention what Operating System and what Shell you have.

We would expect changes to these original lines because of out-of-context backticks:
Quote:
`mkdir $_w/$_owner/`
`mv $f $_w/$_owner/`
There are ambiguty issues with $_w and $_wb . Try using "${_w}" and "${_wb}" .

Do any of your filenames or directory names contain space characters or characters special to Shell (e.g. colon)? If so, the "for" statement needs to be a "while" statement.

Btw. If there are no subdirectories under /path/to/backups the script can be much improved.

Last edited by methyl; 07-26-2011 at 06:56 PM..
# 7  
Old 08-07-2011
This is the full script. I have made an interesting observation however. I have a number of resellers with differename usernames. For some reason, the problem does not affect the folder for the reseller root. The folders don't have anything to do with usernames other than being named after a user - there's no permissions differences across any of the files yet for some reason the one called root somehow works.

Code:
#!/bin/bash
_d="/home/scadmin/cpbackup/daily"
_db="$_d/*.tar.gz"

for f in $_db
do
        _f=$(basename $f)
        _user=${_f%%.*}
        _o=`grep "^$_user:" /etc/trueuserowners`
        _owner=${_o#* }
        if [ ! -d "$_d/$_owner" ]; then
                mkdir $_d/$_owner/
        fi
        mv -f $f $_d/$_owner/
done

Code:
# uname -a
Linux fred.southportcomputers.co.uk 2.6.18-238.12.1.el5 #1 SMP Tue May 31 13:22:04 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux

Running Centos 5.6 x86_64.

The script runs from the root crontab and root's shell is /bin/bash.

There are no spaces or special characters in the path, only lowercase letters.

I'll try it with curly brackets as you suggest, the only bit I haven't got the curlys in it the for statement itself.

Code:
#!/bin/bash
_d="/home/backup/daily"
_db="$_d/*.tar.gz"

for f in $_db
do
        _f=$(basename $f)
        _user=${_f%%.*}
        _o=`grep "^$_user:" /etc/trueuserowners`
        _owner=${_o#* }
        if [ ! -d "${_d}/${_owner}" ]; then
                mkdir ${_d}/${_owner}/
        fi
        mv -f $f ${_d}/${_owner}/
done


Last edited by beddo; 08-07-2011 at 06:12 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Duplicating and changing sh file

Hello, I have a file named file_1.sh that I want to duplicate into file_2.sh, file_3.sh,..., etc. I also need to change the text within each file so that it would fit the file name. For example, in file_1.sh there is a command to save some output as 'output_1.txt', and also there is an input... (3 Replies)
Discussion started by: haguyw
3 Replies

2. UNIX for Dummies Questions & Answers

duplicating a line

I have a text file which is the results of running a tests hundreds of times. For simplicity let's say that each test consists of 5 lines of text numbered 1-5 e.g. 1 aaa aaa aaa 2 bbb bbb bbb 3 ccc ccc ccc 4 ddd ddd ddd 5 eee eee eee 1 aaa aaa aaa 2 bbb bbb bbb 3 ccc... (4 Replies)
Discussion started by: millsy5
4 Replies

3. UNIX for Dummies Questions & Answers

Duplicating MAC linux or net?

hi guys I have a IBM eServer BladeCenter HS12 with Linux Red Hat 5.4 installed on it, it is using Bonding. ifcfg-bond0 DEVICE=bond0 BOOTPROTO=none ONBOOT=yes TYPE=Ethernet IPADDR=x.x.x.x NETMASK=x.x.x.x GATEWAY=x.x.x.x USERCTL=no IPV6INIT=no PEERDNS=no BONDING_OPTS="miimon=80... (1 Reply)
Discussion started by: karlochacon
1 Replies

4. Red Hat

Duplicating ethernet speed

Hi guys, Suppose you have a server with two ethernet cards (1GB each) and each cards are connecting to two different switches cisco 3750. My question is: How can I setup my server's network interfaces to increase the throughput up to 2GB? is it possible? If not, do you know another way to up... (3 Replies)
Discussion started by: iga3725
3 Replies

5. UNIX for Dummies Questions & Answers

remove duplicating lines

Hi, i have a webserver logfile and want to count how many page views there have been. I was thinking about removing lines that begin with the same user and same date&time, because it indicates they were just looking at one page and multiple hits were counted. My question is how do I do this?... (6 Replies)
Discussion started by: JudithBorg
6 Replies

6. HP-UX

duplicating ignite tapes

I have a B180L controller running HP-UX 10.2 with an internal DDS2 tape drive and an external Surestore DDS (24gb) tape drive. I want to make duplicate copies of ignite tapes from one tape drive to another. What is the best way to do this? (1 Reply)
Discussion started by: garyb
1 Replies

7. Post Here to Contact Site Administrators and Moderators

Sorry for duplicating posts

SORRY FOR DUPLICATING POSTS, COULD U JUST REMOVE THE FIRST ONE. Thank u. (1 Reply)
Discussion started by: vrguha
1 Replies

8. Shell Programming and Scripting

duplicating 1st row of data

Hi, I have data in the following format: data1 data2 data3 data4 data5 data6 data7 data8 data9 data10 I require the final output to be: data1 data1 data1 data1 i only require the 1st line but I need to replicate it in n rows where n is the number of rows where data... (4 Replies)
Discussion started by: ReV
4 Replies
Login or Register to Ask a Question