Archive won't move


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Archive won't move
# 1  
Old 07-27-2012
Archive won't move

I' writing a script trying to archive the oldest two directories from one place, tar them, and move them to another.

Code:
#!/bin/bash

cd (/dir/with/dirforarchiving)
ARCHIVE=(/temp/dir/)
FIND=$(find . -maxdepth 1 -type d)
ARRAY=(`ls ${FIND} -ltd | tail -2`)

names=(${ARRAY[7]} ${ARRAY[15]})
dates=(${ARRAY[5]} ${ARRAY[13]})

for i in {0..1}
do
        CUT=$(echo ${dates[$i]} | cut -d- -f1,2)
        if [[ -d ${ARCHIVE}${CUT[$i]} ]]
        then
                tar -zcvf ${names[$i]}.tar.gz ${names[$i]}
                mv ${names[$i]}.tar.gz ${ARCHIVE}${CUT[$i]}
        else
                mkdir ${ARCHIVE}${CUT[$i]}
        fi
done

The problem I'm running into is that the oldest two directories, and some others, were created the same month, date, and different only because their modification time was one or two minutes apart. The oldest two have dates of 2012-07-25, when I run the directory exists check, it will created the directory 2012-07 and move ONE of the two targeted directories into it, the other tar does not go in the target directory but one level up. I want it in /temp/dir/2012-07 one of them goes into that and the other goes to /temp/dir.

How can I fix this?
# 2  
Old 07-27-2012
I cannot figure out what is in those arrays. Parsing ls into an array, and find into a string variable, is very likely to eventually break. It is best to operate using NUL delimited filenames printed from find. We can also have it print the date.

Code:
destprefix=/tmp/tars
find . -maxdepth 1 -type d -printf '%Ts %TY-%Tm %f\0' | sort -znk1 | while read -rd '' epoch date dir && ((max++ < 2))
do
    dest=$destprefix/$date
    echo mkdir -p "$dest"
    echo tar -zcvf "$dest/$dir.tar.gz" "$dir"
done

remove the echos if it prints what seems correct
This User Gave Thanks to neutronscott For This Post:
# 3  
Old 07-27-2012
Quote:
Originally Posted by jrymer
if [[ -d ${ARCHIVE}${CUT[$i]} ]]
then
tar -zcvf ${names[$i]}.tar.gz ${names[$i]}
mv ${names[$i]}.tar.gz ${ARCHIVE}${CUT[$i]}
else
mkdir ${ARCHIVE}${CUT[$i]}
fi
[/CODE]
If the archive destination doesn't exist, the else-block creates it but doesn't tar anything. The loop moves on to the second directory, and retests for the archive destination's existence. This time it's there and that second directory is archived.

Disentangle the directory creation from the tarring. In pseudocode, instead of ...
Code:
if directory exists
    tar
    mv
else
    mkdir
fi

... try ...
Code:
if directory doesn't exist
    mkdir
fi
tar
mv

Regards,
Alister
# 4  
Old 07-27-2012
Thanks neutronscott it worked, can you explain in a little more detail what your code does though? I am unfamiliar with printf, this section was hardest to read.
Quote:
-printf '%Ts %TY-%Tm %f\0' | sort -znk1 | while read -rd '' epoch date dir && ((max++ < 2))
I understand it for the most part, I'm guessing the...
Code:
%TY-%Tm

are the year and month, and that the...
Code:
sort -znk1

is sorting in modified date order? Is the...
Code:
read -rd

taking the input from the printf?

This helped a lot but I still do not understand why one tar was going into the created directory and the other was not with my script. I tried Alister's script and understand his change, but it yielded me the same result. Both tar files were still not going into the destination directory, only one was even though they should both have been in the same directory.
# 5  
Old 07-27-2012
Quote:
Originally Posted by jrymer
Thanks neutronscott it worked, can you explain in a little more detail what your code does though? I am unfamiliar with printf, this section was hardest to read.
It's in the find manual. Rather than just printing file names, we include the modify time in epoch and also YYYY-MM format, then finally the filename NUL terminated. So even a filename with a new-line won't mess it up.

Quote:
Originally Posted by jrymer
the...
Code:
sort -znk1

is sorting in modified date order? Is the...
Code:
read -rd

taking the input from the printf?
-z is to print it back NUL terminated (GNU extention). -nk1 is for numerical sort on first position... bash's read you can set the delimiter to be different than newline. here we set it to '' (empty string, NUL). Yes, it reads off what is piped in from find's -printf for further processing in the shell's while loop.

Quote:
Originally Posted by jrymer
This helped a lot but I still do not understand why one tar was going into the created directory and the other was not with my script. I tried Alister's script and understand his change, but it yielded me the same result. Both tar files were still not going into the destination directory, only one was even though they should both have been in the same directory.
If alister's suggestion did not fix your script, i'd try bash -x ./script to see exactly what it is doing to follow the logic. It was my first thought.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script won't run because hardware won't produce display

Can anyone offer any advice on how to modify the script below to work on a new system we have, that has no graphics capability? We admin the system through a serial RAS device. I've tried running the below script through the RAS and through an ssh -X session. It failed with something like "GTK... (3 Replies)
Discussion started by: yelirt5
3 Replies

2. Shell Programming and Scripting

Script to archive logs and sftp to another archive server

Requirement: Under fuse application we have placeholders called containers; Every container has their logs under: <container1>/data/log/fuse.log <container1>/data/log/fuse.log.1 <container1>/data/log/fuse.log.XX <container2>/data/log/fuse.log... (6 Replies)
Discussion started by: Arjun Goswami
6 Replies

3. Shell Programming and Scripting

Move all files from dir and subdir to Archive with File name as complete path_filename

HI, I need to move all files from a dir & its all subdir to Archive folder which is indise dir only. and moved filename should changed to complete path ( Like Dir_subdir_subdir2_.._filename ). also all files names shoud capture in a file in order to mail I written below code ... (11 Replies)
Discussion started by: minijain7
11 Replies

4. Shell Programming and Scripting

Archive & move the file

I have a set of files in path /ifx01/etldata/lmt/ a.xml b.xml I have to archive & move these files to path /ifx01/etldata/archive ,so that not even the original files should exists. After acrhiving there should be no files in path /ifx01/etldata/lmt/ (6 Replies)
Discussion started by: jagadeeshn04
6 Replies

5. AIX

xntpd won't stay up...

AIX 5.3-5300.09.06.1013 (AIX 5.3 TL9 SP6) # startsrc -s xntpd -a "-x" (with -x at the end of the xntpd line in /etc/rc.tcpip, too.) will run for 5-15 minutes, and then die. # errpt -a with a search on xntpd gives me this: ------------------------------------------------ LABEL: ... (7 Replies)
Discussion started by: dafydd2277
7 Replies

6. Shell Programming and Scripting

Extracting from archive | compressing to new archive

Hi there, I have one huge archive (it's a system image). I need sometime to create smaller archives with only one or two file from my big archive. So I'm looking for a command that extracts files from an archive and pipe them to another one. I tried the following : tar -xzOf oldarchive.tgz... (5 Replies)
Discussion started by: chebarbudo
5 Replies

7. UNIX for Dummies Questions & Answers

sample script to archive & move previous day syslog files

hi all. Please help me with archiving previous day syslog files. the files have no extension and have the format YYYY-MM-DD. I want to archive the file then move it to some other machine. thanks. (2 Replies)
Discussion started by: coolatt
2 Replies

8. AIX

won't mount /usr...won't boot fully

Hello: NOOB here. I attempted to use smit mkcd. Failed on first attempt, not enough space. 2nd attempt tried to place iso on /usr, not enough space there. Cleanup ran for about 5 minutes after aborting. Now AIX won't boot. LCD display on 7029-6E3 says: 0517 MOUNT /USR. Attempted to boot from CD... (11 Replies)
Discussion started by: bbird
11 Replies

9. UNIX for Dummies Questions & Answers

HP 10.20 Won start

After I log ino the machine, a window pops up indicating that I should check that the HOSTNAME is the same in these three files /etc//rc.configd/netconf /etc/hosts /var/adm/inetd.sec How do I change the hostname in these files? Thanks, Mike h (1 Reply)
Discussion started by: hutchin
1 Replies

10. UNIX for Dummies Questions & Answers

why won't this Work?

hey programmers! 1-why won't gcc accept as an argument? i tried the recommendations on the man page of getch(),..etc. nothing worked. 2-why it won't see <iostream> && <fstream> even if i implemented the function as follow std::cout<<"..etc"<<endl; 3-after i type this code in it gives... (6 Replies)
Discussion started by: mbabeli
6 Replies
Login or Register to Ask a Question