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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Move all files from dir and subdir to Archive with File name as complete path_filename
# 1  
Old 05-16-2012
Error 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
Code:
filetime()
{
        perl  -e '@d=localtime ((stat(shift))[9]); printf "%4d%02d%02d%02d%02d%02d\n", $d[5]+1900,$d[4]+1,$d[3],$d[2],$d[1],$d[0]' "$1"
}
und="_"
count="0"
FFile="/export/home/srcdata1/tTemp"
Flist="$FFile/Filelist"
Ftemp="$FFile/temp"
#$count -eq 0
for int in `ls -l $FFile | grep ^d | awk '{print $9}'`
do
arch="$FFile/Archived"
if [ ! -d arch ]; then
   echo " *******  not inside Arch and also  not in Organised ***** -----*"
   #echo `ls |wc -l`
   find $FFile -type f| awk 'BEGIN{FS="/"} {print $NF}' > $Flist
   while read line
                do
   #echo "  inside while read line "
                        if [ -e $FFile/"$line" ]; then
                                ftime=$(filetime $FFile/"$line" )
                                yyyy=`echo $ftime | cut -c 1-4`
                                mm=`echo $ftime | cut -c 5-6`
                                dd=`echo $ftime | cut -c 7-8`
                                echo "  Added time stamp to file name "
                        else
                                continue
                        fi
     monyyyy=${yyyy}${und}${mm}
                        if [ ! -d $arch/$int$monyyyy ]; then
                        mkdir $arch/$int$monyyyy
                        fi
                        echo $FFile/$int/"$line"
                        echo $arch/$int/$monyyyy/"$line"
                        mv $FFile/$int $arch/$int$monyyyy

   done < $Flist
  # echo " -----------   moved to archived now check ------------"
fi
done


My code is moving just the FileLIst ( which contains name of all files) not the files. Also how can i prefix filename before moving to folder with time stamp of file created

Last edited by Franklin52; 05-16-2012 at 06:45 AM.. Reason: Please use code tags
# 2  
Old 05-16-2012
You seem to have be solving a simple problem in a complicated way. Can you not just use tar?

Code:
tar cf $arch/$monyyyy.tar $FFile

creates a tar file in the "$arch" directory with your derived time stamp on it (which I would get using `date +%m%Y` ) and all the files underneath "$FFile"

Do you really need something more complicated?
# 3  
Old 05-16-2012
Hi,

Thanks for your help.
But my requirement is I need to moves files with full path + date & time of the file created not the sysdate time
# 4  
Old 05-16-2012
The tar program saves the time attributes of the files it archives.
# 5  
Old 05-17-2012
hi,

can any 1 help me with getting file creation time as the renamed file?
rest of the things are working.

but i need

dir1-->f1 ( time of creation of file can get by ls -lrt)
mv f1 dir1/archived/f1_time_of_file_creation

please help me with this.
# 6  
Old 05-17-2012
I don't understand why you use a perl script just to get the mtime from the file you want to move. Why not just do the whole thing in perl? I have no idea what $int is supposed to mean. I also don't understand why you use the perl script to output the time in a format that you then have to parse with 3 separate invocations. Here's a more logical alternative:
Code:
filetime() {
   # Change %F,%T as needed. See "man strftime"
   perl  -mPOSIX -e 'print POSIX::strftime("%F,%T\n",localtime((stat(shift))[9]))' ; 
}

while read line ; do 
        test ! -e "$FFile/$line" && continue;

        monyyyy=$(filetime "$FFile/$line")
        mkdir -p $arch/$line
        mv "$FFile/$line" $arch/$line/$monyyyy
done

But I think this entire approach could be simplified with using GNU's fileutils' find:
Code:
$adir=ARCHIVE_DIR
$sdir=SOURCE_DIR
$flist=/tmp/filelist.$$
# Step 1: Create the list of files
cd $sdir
find . -type f -printf "%T+ %p\n" > $flist

# Step 2: create the directory tree in adir
cd $adir
awk '{ print $2 }' $flist | xargs $DEBUG mkdir -p 

# Step 3: move the files
cd $sdir
cat $flist | 
while read time path ; do
   $DEBUG mv $path $adir/$path/$time
done

To prevent this from destroying files while you test, export DEBUG=echo
# 7  
Old 05-17-2012
Useless Use of Cat

You could just do

Code:
...

while read time path ; do
   $DEBUG mv $path $adir/$path/$time
done <$flist

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How do I create a tar file for only dir and its subdir structures??

How do I create a tar file for only dir and its subdir structures?? (4 Replies)
Discussion started by: vx04
4 Replies

2. Shell Programming and Scripting

Script to move all files in a dir into a certain dir

Hello all, I'm very new to shell scripting and need quite urgently to do this thing for my student job. I have a directory called "vectors" with a bunch of files all named ".vector". also i have for each of those files a directory with the name . I now want to move each of those *.vector files... (2 Replies)
Discussion started by: sherresh
2 Replies

3. UNIX for Dummies Questions & Answers

copying the dir/subdir structure from one server to another?

Hi All, I want to copy the dir/subdir structure from SERVER-A to SERVER-B without copying all the files in each dir. Is it possible using SCP / SFTP command? For example, SERVER-A has following two dir/subdirectories and files under each subdir. ... (1 Reply)
Discussion started by: Hangman2
1 Replies

4. Shell Programming and Scripting

Need a script to move the files from one dir to other other dir

Need a script to move the files from one dir to other dir and at the same time it has to read the log in the source dir. Please help me ASAP. (4 Replies)
Discussion started by: viswanathkishor
4 Replies

5. Shell Programming and Scripting

Help - Script to move files to subdir depending on file extension.

Hi, First off I'm pretty new to scripting so please be gentle. I am looking for some help with a script that will move all files with a certain extension into a folder within their current location. Just for clarity I have all my photos organised in directories such as: ... (4 Replies)
Discussion started by: guinch
4 Replies

6. Shell Programming and Scripting

Perform action in dir if dir has .git subdir

Hi, I want to run git status for the dir which has subdir ".git" in it with dir path mentioned in output log? If a dir does not have .git subdir then skip that dir. Dir will have 100 main dirs & 500 + subdirs and so on. I appreciate all your help :b: (4 Replies)
Discussion started by: dragon.1431
4 Replies

7. Shell Programming and Scripting

replace string in multiple files, dir and subdir

Hello, I have a directory www with multiple directories. Every directory has site name with .htm, .html, .php files or sub directories with .htm, .php, .html file as example - www - sitename 1 - site 1 - sitename 2 - sitename 3 What I'm looking for is a... (7 Replies)
Discussion started by: andyjill
7 Replies

8. Shell Programming and Scripting

moving dir subdir and files

I have created a directory structure and under the directory subdirectories and files are there.I need to move the entire thing to another path.How can i write a script to do that. currently the path of files is as below : /data1/serial/mcycle/archive : under this path differnt sub dir exist ... (6 Replies)
Discussion started by: dr46014
6 Replies

9. Shell Programming and Scripting

need to move files of particular day from one dir to another dir

Hi, I have hundered's of files of the name CMP_PORT_IN_P200903271623042437_20090328122430_err.xml in error directory of todays date ie 20090328 and in the file name 5th field specifies date only now i want to move all files of 20090328 to another directory i.e reprocess directory. So... (3 Replies)
Discussion started by: ss_ss
3 Replies

10. Shell Programming and Scripting

move files and retain subdir structure

hi: I have some files like this folder1/recording1.mp3 folder1/docs/budget.doc folder2/others/misc.mp3 folder3/others/notes.doc all this folders and files are under the mp3 folder. I would like to move just the mp3s to another folder but retain the subdir structure i have. So if... (4 Replies)
Discussion started by: jason7
4 Replies
Login or Register to Ask a Question