Saturday, October 30, 2021

How to gzip thousands of files using for loop

UNIX admin may get task like housekeep Unix filesystem.

 In such situations he may housekeep existing files in the file system or increase file system. Few day back I got such ticket but there was no scope for increasing file system size, so decided to find which files occupying space on that file system.

Finally found more than  2000 files, now question is how to gzip those??

Solution:

Step 1 created directory named like following,
#mkdir /tmp/housekeep

Then created file "listfiles" using touch command and redirected all files inside that file. Cross check done whether it contain correct files only , which we are going to gzip.
Step 2
#touch listfiles
#vi listfiles // add all files which need to be gzip
Step 3

Use following for loop
for i in `cat listfiles`
do
gzip $i
done

UNIX admin can check whether gzip happening or not by executing a following command  in target directory

#ls -lrt *.gz |wc -l

Or by simply take another session of server and execute while loop with above command

While true
do
ls -lrt *.gz |wc -l
sleep 1
done

**Before doing gzip make sure that it will not impact any production application or database.

Usefull command to find files modified in last hours
Print files which are modified in last 24 hrs
#find /var -mtime -1 -print

Thanks !!

No comments:

Post a Comment