Need help to write a script for moving the log files to some other folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to write a script for moving the log files to some other folder
# 1  
Old 09-20-2011
Need help to write a script for moving the log files to some other folder

Hi Experts,

I want to write a script, based upon the following requirement

1) I am having 5 application

HTML Code:
$ cd logs
$ ls -l

drwxr-xr-x   2 natraj nat       5.0K Sep 20 10:25 one
drwxr-xr-x   2 natraj nat       5.0K Sep 20 10:39 two
drwxr-xr-x   2 natraj nat       1.5K Sep 20 10:58 three
drwxr-xr-x   2 natraj nat       7.5K Sep 20 11:06 four
drwxr-xr-x   2 natraj nat       2.0K Sep 20 11:48 five
2) Each application having log files

HTML Code:
$ cd one
$ ls -l

-rw-r--r--   1 natraj nat          11K Sep 15 15:31  y.log.150911
-rw-r--r--   1 natraj nat         439K Sep 15 16:05  x.log.150911
-rw-r--r--   1 natraj nat          15K Sep 16 12:08  y.log
-rw-r--r--   1 natraj nat         470K Sep 16 17:14  x.log.160911
-rw-r--r--   1 natraj nat          58K Sep 19 18:40  x.log
3) I want only last 2 days log files , remaning need to zip and move to archive foldeer (logs_archive).

4) Want to move the logs to archive folder with same directory name , if same directory not exist means need to create and then move the logs file.

5) Finally it should be like as below

HTML Code:
$ cd /logs_archive/one
$ ls -l

-rw-r--r--   1 natraj nat          11K Sep 15 15:31  y.log.150911.z
-rw-r--r--   1 natraj nat         439K Sep 15 16:05  x.log.150911.z
-rw-r--r--   1 natraj nat          15K Sep 16 12:08  y.log.z
-rw-r--r--   1 natraj nat         470K Sep 16 17:14  x.log.160911.z
help me to solve it out Smilie. . . .

Thanks in advance . . . .

---------- Post updated at 05:52 AM ---------- Previous update was at 02:44 AM ----------

hi friends help me to write a script for moving the files . . . . .
# 2  
Old 09-20-2011
You want to write a script, or you want us to write you a script?

Did you write nothing so far?
# 3  
Old 09-20-2011
I have written the below script , but i am getting error .

so i just want to know i am in correct way r need to change ?

This is my script

HTML Code:
#!/bin/bash
#set -x

LS=`which ls`
FIND=`which find`

PATH="logs"

PATH1="/logs_arch/"


$LS $PATH | /usr/bin/awk '{print NR" : "$0}' > /export/home/wasadmin/chumma.txt



j=`/usr/bin/head -1 /export/home/wasadmin/chumma.txt | /usr/bin/cut -d " " -f3`

l=`/usr/bin/tail -1 /export/home/wasadmin/chumma.txt | /usr/bin/cut -c1,2`

                $FIND $PATH/$j -mtime +2 -type f -exec compress {} \ ; > /export/home/wasadmin/test.lst


                     if [ ! $PATH1/${j} ];
                     then

                        mkdir -p $PATH1/$j
                        for file in `awk -F"/" '{$NF=" ";print }' OFS="/" /export/home/wasadmin/test.lst | sort -u`
                        do
                        echo "$file"
                        mv "$file" $PATH1/$j


                    else


                        for file in `awk -F"/" '{$NF=" ";print }' OFS="/" test.lst | sort -u`
                        do
                        echo "$file"
                        mv "$file" $PATH1/$j

                   fi

        else

                echo "  exit  "
fi

rm -rf test.lst chumma.txt
# 4  
Old 09-20-2011
You could give us the output you know... (helps to know what errors we are looking for...)
from
Code:
PATH="logs"

I understand you have to be in a precise directory for this script to succeed, is it the case? why set paths for commands and neglect here to do the same?
If this line
Code:
$LS $PATH | /usr/bin/awk '{print NR" : "$0}' > /export/home/wasadmin/chumma.txt

works, I have more doubts on what you are trying to achieve here
Code:
 $FIND $PATH/$j -mtime +2 -type f -exec compress {} \ ; > /export/home/wasadmin/test.lst

...
# 5  
Old 09-20-2011
Actually i am declaring the path to that variable because, in future i can easly change it.

plz see the code , have given the explanation on side of the syntax
HTML Code:
#!/bin/bash
#set -x

LS=`which ls`
FIND=`which find`

PATH="/logs"

PATH1="/logs_arch/"


$LS $PATH | /usr/bin/awk '{print NR" : "$0}' > export/home/wasadmin/chumma.txt  // moving all the application in log dir. with line number to chumma.txt



j=`/usr/bin/head -1 /export/home/wasadmin/chumma.txt | /usr/bin/cut -d " " -f3`  

l=`/usr/bin/tail -1 /export/home/wasadmin/chumma.txt | /usr/bin/cut -c1,2` 

                $FIND $PATH/$j -mtime +2 -type f -exec compress {} \ ; > /export/home/wasadmin/test.lst  //compress all the logs except 2days logs and move the list to test.lst


                     if [ ! $PATH1/${j} ];
                     then

                        mkdir -p $PATH1/$j
                        for file in `awk -F"/" '{$NF=" ";print }' OFS="/" /export/home/wasadmin/test.lst | sort -u`
                        do
                        echo "$file"
                        mv "$file" $PATH1/$j  //using that test.lst i am moving that compress log files too archive folder


                    else


                        for file in `awk -F"/" '{$NF=" ";print }' OFS="/" test.lst | sort -u`
                        do
                        echo "$file"
                        mv "$file" $PATH1/$j

                   fi

        else

                echo "  exit  "
fi

rm -rf test.lst chumma.txt
I dono whether i am in correct way r not. Because this is my first script. If am in wrong direction plz guide me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

2. Shell Programming and Scripting

Need to write a script that checks whether files are moving out of a directory.

I have a directory that has files going into it and moving out on a regular basis. The normal state of the directory would be to be empty. I need to write a script that will check to see if files are Not moving out of the directory. Any help would be most welcome. (4 Replies)
Discussion started by: RoBKoS
4 Replies

3. Shell Programming and Scripting

Moving files and folders to another folder

I recently bought Synology server and realised it can run scripts. I would need fairly simple script which moves all files and folders from ARCHIVE folder to WORKING folder. I would also need to maintain folder structure as each of the folders may contain subfolders. How would I go about it as I am... (1 Reply)
Discussion started by: ###
1 Replies

4. UNIX for Dummies Questions & Answers

Moving files to a folder with a variable name

hello there- first post here- maybe someone can help- Basically I am trying to copy the contents of a folder to a different folder that has a variable name. the content I want to copy would be in a folder on my desktop called: myfolder the variable folder would look something like: ... (3 Replies)
Discussion started by: infothelonghaul
3 Replies

5. Shell Programming and Scripting

Finding files with wc -l results = 1 then moving the files to another folder

Hi guys can you please help me with a script to find files with one row/1 line of content then move the file to another directory my script below runs but nothing happens to the files....Alternatively Ca I get a script to find the *.csv files with "wc -1" results = 1 then create a list of those... (5 Replies)
Discussion started by: Dj Moi
5 Replies

6. Shell Programming and Scripting

moving files from one folder to many folders

I have a more than 10 K files in a folder. They are accumulated in a period of more than an year (Say from 13th July 2010 to 4th June 2011). I need to perform housekeeping on this. The requirement is to create a folder like 13Jul2010,14July2010,......3June2011,4June2010 and then from the main... (2 Replies)
Discussion started by: realspirituals
2 Replies

7. UNIX for Advanced & Expert Users

Moving 1000s of files to another folder

Hi, I need to move 1000s of files from one folder to another. Actually there are 100K+ files. Source dir : source1 Target dir : target1 Now if try cp or mv commands I am getting an error message : Argument List too long. I tried to do it by the time the files are created in the... (1 Reply)
Discussion started by: unx100
1 Replies

8. Shell Programming and Scripting

Script for moving files from one folder to other

Hi I need to move last 1 year old files from one folder to another location on same server.How to write a shell script for the same? thanx Lalit (8 Replies)
Discussion started by: lalitkumar
8 Replies

9. Shell Programming and Scripting

Script for moving files from one folder to other

Hi All I need a shell script for move the 1 year old files from one folder to another folder with date and time. How to write a shell script for the same pls hepl me out. thanx in advance Thanx Lalit (2 Replies)
Discussion started by: lalitkumar
2 Replies

10. Shell Programming and Scripting

Moving Files from one folder to another folder

Hi, I have a folder which contain the log files. The folder may contain sub folders as well. I want to move the contents of the log folder to tmp folder periodically. I have used the command. LOG_DIR=/logs DESTINATION_DIR=/tmp/logs find ${LOG_DIR} -mtime +1 -exec mv {}... (10 Replies)
Discussion started by: farooqpervaiz
10 Replies
Login or Register to Ask a Question