Moving files based on size (string to integer)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving files based on size (string to integer)
# 1  
Old 04-01-2013
Moving files based on size (string to integer)

I have a log file that I want to archive out as it reaches 100MB. I am using the following to get the file size into a variable but get the error "line 5: [: -gr: binary operator expected"
This is still the test code to work out the comparison.

Code:
filesize=$(wc -c < logfile.log)
if [ $filesize -gr 100000000 ]
then
  echo "is greater than 100M"
else
  echo "is less than 100M"
fi

I'm sure there's something I'm being braindead on and overlooking. I'm pretty adept at batch files, but this is my initial foray into bash.

Thanks.
# 2  
Old 04-01-2013
You should either use stat or ls -l to get the actual size. wc -c gives you the word count.
And the error is probably because of -gr in the if loop, it should be -gt.

Code:
file_size=$( stat -c %s logfile.log )
if [ $file_size -gt 100000000 ]; then
  echo "is greater than 100M"
else
  echo "is less than 100M"
fi

--ahamed

Last edited by ahamed101; 04-01-2013 at 04:39 PM..
# 3  
Old 04-01-2013
Perfect! Thanks for the change. Bonehead typo -_-
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace integer string in a variable based on month?

Hi Folks - Linux Version = Linux 2.6.39-400.128.17.el5uek x86_64 I have a process that determines the start and end load periods for an Oracle data load process. The variables used are as follows follows: They are populated like such: However, the load requires the month to be the... (11 Replies)
Discussion started by: SIMMS7400
11 Replies

2. Shell Programming and Scripting

Moving old files based on pattern

Hi all I am trying to loop through a directory of files using a given search pattern. some of the files will be duplicated due to the pattern, but of the duplicate files i wanted to move the older files to another location. Is there any straightforward way of doing this ? One of ways I... (1 Reply)
Discussion started by: sthapa
1 Replies

3. Shell Programming and Scripting

Moving files based on file name

Hi All, I have multiple files in the folder, I want to move those files into the other folder on based of name File names: Template_server1_01==> Template_server1_02==>To one directory /Server1 Template_server1_03==> Template_server2_01==> Template_server2_02==>To one... (9 Replies)
Discussion started by: sharsour
9 Replies

4. UNIX for Dummies Questions & Answers

Script moving files based on date

Hi, I need a script that moves files based on date to a folder. The folder should be created based on file date. Example is : Date file name ----- -------- Oct 08 07:39 10112012_073952.xls Oct 09 07:39 10112012_073952.xls Oct 10 07:39 ... (6 Replies)
Discussion started by: rockingvj
6 Replies

5. UNIX for Advanced & Expert Users

Moving multiple files based on the pattern

I want to search for a particular file name patterns and move them to a specific folder, is it possible to do it with awk or sed? (1 Reply)
Discussion started by: rudoraj
1 Replies

6. UNIX for Dummies Questions & Answers

moving files based on condition

hi i have to move files and send an email and attached the bad files to inform the developer about that. #!/bin/ksh BASE_DIR=/data/SrcFiles cd $BASE_DIR ## finding the files from work directory which are changed in 1 day find -type f -name "*.csv" –ctime 0 > /home/mydir/flist.txt ##... (14 Replies)
Discussion started by: awais290
14 Replies

7. Shell Programming and Scripting

Moving a file based on size

hello, I am creating a script that is going to scan the contents of a directory, and if it finds a certain DocumentName that is a certain size (The example, DocumentName and a size of 8777) If this file exists, it needs to be moved to a directory for later removal/processing..anyone have... (7 Replies)
Discussion started by: jeffs42885
7 Replies

8. Shell Programming and Scripting

Compare two files based on integer part only

Please see how can I do this: File A (three columns): X1,Y1,1.01 X2,Y2,2.02 X3,Y3,4.03 File B (three columns): X1,Y1,1 X2,Y2,2 X3,Y3,4.0005 Now I have to compare file A and B based on the integer part of column 3. Means first 2 rows should be OK and the third row should not satisfy... (12 Replies)
Discussion started by: yale_work
12 Replies

9. Shell Programming and Scripting

Moving the files based on count and time.

Hi, I have a requirement ,let us say 1000 files needs to be transferred in an hour from one path to another path and if the files (1000 files) are transferred within an hour ( say 40 mins), then the process should remain idle for the remaining time ( 20 mins). (3 Replies)
Discussion started by: Asaikarthik
3 Replies

10. UNIX for Dummies Questions & Answers

Moving files till the size is less than 200000.

Hi, I need your help. Suppose I have a directory called /home/rooh This directory contains files and directories. For ex drwxr-xr-x 3 rooha arboradm 96 Apr 6 03:24 batches drwxr-xr-x 2 rooha arboradm 96 Apr 6 03:21 worker -rw-rw-rw- 1 rooha arboradm ... (1 Reply)
Discussion started by: rooh
1 Replies
Login or Register to Ask a Question