LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-30-2012, 11:59 PM   #1
Toushi
LQ Newbie
 
Registered: Apr 2011
Posts: 18

Rep: Reputation: 0
Bash Scripting -- Find big files and gzip it


Hello All ,

I am newbie in bash scripting I stuck in one script. My script purpose is to find out the big files from current path 'pwd' and move the big file after gzip to particular destination.

My script is working if one file is exist in that path but if there are more than 2 files then script not execute as expected.

Could you please someone help me to solve this issue.

#!/bin/bash

ARCHIVENR=`date +%Y%m%d`
DESTDIR="$PWD/archive-$ARCHIVENR"
mkdir "$DESTDIR"

BIGFILE=`find "$PWD" -xdev -type f -a -size +10000 -exec ls -lrt {} \; |awk '{ print $9 }'`

for i in $BIGFILE

#while [ -f "$BIGFILE" ]

do
gzip "$BIGFILE"; mv "$BIGFILE".gz "$DESTDIR"
echo "$BIGFILE archived"
done


Regards,
Toushi
 
Old 07-31-2012, 12:04 AM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

you need to use $i inside the loop, not $BIGFILE. Eg
Code:
for i in $BIGFILE
do
  gzip "$i"
  mv "${i}.gz" $DESTDIR
done
HTH,

Evo2.
 
1 members found this post helpful.
Old 07-31-2012, 12:43 AM   #3
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Also, some changes for you to consider (to avoid performing unnecessary / redundant tasks):

Code:
#!/bin/bash

PATH=/bin:/usr/bin

_archive="archive-$(date +%Y%m%d)"

[ -e "${_archive}" ] || mkdir "${_archive}"

find . -xdev -maxdepth 1 -type f -size +10000 \
	-exec gzip {} \; \
	-exec mv {}.gz ${_archive} \;

exit 0
NB the -maxdepth option I added to find(1). That will prevent find(1) from descending into your archive directory and trying to gzip an already gzip'd file that still matches the size criteria.

Last edited by anomie; 07-31-2012 at 12:45 AM.
 
Old 08-01-2012, 02:44 AM   #4
Toushi
LQ Newbie
 
Registered: Apr 2011
Posts: 18

Original Poster
Rep: Reputation: 0
Hello Evo2 anomie,

Thank you very much for replying on my post. I tried both suggestion and it worked for me.

Regards,
Toushi
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting - find command with variable substitution and quoting bgeddy Programming 4 07-29-2022 08:28 AM
how to use Big O, Big Omega, and Big Theta to find running time of algorithms naveed17 Programming 3 03-17-2011 10:25 AM
How to find big files on the disk? hadimotamedi Linux - Newbie 2 10-17-2010 06:38 AM
Find big files markvc Linux - General 4 12-05-2006 07:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration