rename a file if it exceeds 1MB in size


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers rename a file if it exceeds 1MB in size
# 1  
Old 02-24-2010
rename a file if it exceeds 1MB in size

Hi all.
I am trying to write a script that renames a logfile if it exceeds roughly 1 MB.

My find command is:

Code:
 
find some_logfile -type f -size +20000

Now I want to do my processing if it finds a file or not...
How can I go about this? The $? is 0 if it finds a file or not.

All help is appreciated.

Thanks
# 2  
Old 02-24-2010
Something like this?

Code:
for each in `find dir -type f -size +20000`
do
mv $each ${each}.new
done

# 3  
Old 02-24-2010
Here's what I get run that:

Code:
 
cp: cannot access find
cp: illegal option -- t
cp: illegal option -- y
cp: illegal option -- e
cp: illegal option -- t
cp: illegal option -- y
cp: illegal option -- e
cp: illegal option -- .
cp: illegal option -- n
cp: illegal option -- e
cp: illegal option -- w
cp: Insufficient arguments (0)
Usage: cp [-f] [-i] [-p] [-@] f1 f2
       cp [-f] [-i] [-p] [-@] f1 ... fn d1
       cp -r|R [-f] [-i] [-p] [-@] d1 ... dn-1 dn
cp: cannot access f
cp: illegal option -- s
cp: illegal option -- z
cp: illegal option -- e
cp: illegal option -- s
cp: illegal option -- z
cp: illegal option -- e
cp: illegal option -- .
cp: illegal option -- n
cp: illegal option -- e
cp: illegal option -- w
cp: Insufficient arguments (0)
Usage: cp [-f] [-i] [-p] [-@] f1 f2
       cp [-f] [-i] [-p] [-@] f1 ... fn d1
       cp -r|R [-f] [-i] [-p] [-@] d1 ... dn-1 dn
cp: cannot access +20000

I am running korn shell.

Thanks

---------- Post updated at 01:30 PM ---------- Previous update was at 01:24 PM ----------

Sorry I tried to copy the file so that is why you see the cp error...
I did however notice that the file does get copied with the new extension on it so I can probably get away with using that as the output will go to /dev/null anyway.
# 4  
Old 02-24-2010
Please avoid running scripts containing errors in a production environment. It will bite you in the bum.

Here is a script structure which you may wish to amend to your local environment. Please avoid blind cut/paste/run because you need to understand what is going to happen before you run a script.

There are several riders regarding logfile maintenance scripts. If the logfile is open by a program do not rename (mv) the file. In certain circumstances you can copy then truncate the logfile, in other circumstances you must ensure that the program does not have the logfile open. It is all to easy to crash a database engine by renaming logs.


Code:
filename="some_logfile"
MB="`echo '1024 * 1024'|bc`"    # One megabyte in bytes
#
# If the file exists, find out how big it is in bytes
if [ -f "${filename}" ]
then
        filesize=`ls -lad "${filename}" | awk '{print $5}'`
else
        echo "File missing: ${filename}"
        exit 1
fi
#
# Compare the size of the file with the maximum and rename the file if it is too big
if [ ${filesize} -gt ${MB} ]
then
        echo "Renaming ${filename} ${filename}.old"
        mv "${filename}" "${filename}.old"
fi


Last edited by methyl; 02-24-2010 at 05:03 PM.. Reason: Script comments
# 5  
Old 02-25-2010
MySQL

I don't know whether my understanding is correct on your requirement.But you can achieve this by a single stroke .

Code:
 find . -iname "test_log" -size +1M -exec mv {} test_log_mbs \;

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

To delete the oldest files in a file when file count in the folder exceeds 7

Hi All, I need to delete the oldest file in folder when the file count in the folder exceed 6 ( i have a process that puts the source files into this folder ) E.x : Folder : /data/opt/backup 01/01/2012 a.txt 01/02/2012 b.txt ... (1 Reply)
Discussion started by: akshay01987
1 Replies

2. Solaris

18-Mar-2012 14:25:03.209 general: error: socket: file descriptor exceeds limit (4096/4096)

I have BIND 9.8.1-P1 cache only DNS server running in Solaris 10. I have upgraded the same from 9.6.1 to 9.8.1-P1. Now i am facing "file descriptor exceeds limit (4096/4096)" error frequently on the server. Please help me on this issue! (1 Reply)
Discussion started by: sandeep.tk
1 Replies

3. Shell Programming and Scripting

How to remove a file in shell script if its size exceeds limit?

How can i remove a file using shell script when its size exceeds 10MB. Given that file is located in different location to the shell script where it is running? (4 Replies)
Discussion started by: vel4ever
4 Replies

4. Shell Programming and Scripting

Shell script to copy a log file if it exceeds 5000000 bytes

Hi, on unix box, under /local/home/userid/logs folder, apps generated the following files sw_warn.log sw_error.log eaijava.log if there any application specific errors, the above file will keep growing. if the file exceed 5000000 bytes, I would like to have a shell script which do... (6 Replies)
Discussion started by: lookinginfo
6 Replies

5. Shell Programming and Scripting

The scripts not able to make the file to size 0, every times it go back to its original size

#!/bin/sh ########################################################################################################## #This script is being used for AOK application for cleaning up the .out files and zip it under logs directory. # IBM # Created #For pdocap201/pdoca202 .out files for AOK #1.... (0 Replies)
Discussion started by: mridul10_crj
0 Replies

6. Shell Programming and Scripting

Log Size Exceeds Limit

Everytime I try to start Discreet Flame on my Octane2 machine it keeps giving me an error message "initial log size exceeds limit--aborting" Can anyone please help me how to fix this problem. Thanks. Octane2 IRIX64 Release 6.5 Flame 2007 (6 Replies)
Discussion started by: dewaraja
6 Replies

7. UNIX for Advanced & Expert Users

Log Size Exceeds Limit

Everytime I try to start Discreet Flame on my Octane2 machine it keeps giving me an error message "initial log size exceeds limit--aborting" Can anyone please help me how to fix this problem. Thanks. Octane2 IRIX64 Release 6.5 Flame 2007 (0 Replies)
Discussion started by: dewaraja
0 Replies

8. UNIX for Dummies Questions & Answers

Log Size Exceeds Limit

Everytime I try to start Discreet Flame on my Octane2 machine it keeps giving me an error message "initial log size exceeds limit--aborting" Can anyone please help me how to fix this problem. Thanks. Octane2 IRIX64 Release 6.5 Flame 2007 (0 Replies)
Discussion started by: dewaraja
0 Replies

9. Solaris

command to find out total size of a specific file size (spread over the server)

hi all, in my server there are some specific application files which are spread through out the server... these are spread in folders..sub-folders..chid folders... please help me, how can i find the total size of these specific files in the server... (3 Replies)
Discussion started by: abhinov
3 Replies

10. Shell Programming and Scripting

creating file of 1MB using shell command?

Hi everybody in the forum, I want to create an empty file of say some 1MB ,i mean at the command line itself.How is this possible??????EEK! (4 Replies)
Discussion started by: vijaya2006
4 Replies
Login or Register to Ask a Question