Loop through subdirectories to gzip files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop through subdirectories to gzip files
# 1  
Old 08-14-2009
Loop through subdirectories to gzip files

I have a directory has many subdirectories. Each subdirectories has several very large files. Some of the large files already compressed (*.Z). I need to uncompress those files if they are *.Z files then gzip them to save more space. But those files are too large so they can only be done uncompress and gizp one file at a time. Please be aware not every file are compressed. File name could be: abc.xyz.Z or abcd.xyzw I need to make them as abc.xyz.gz and abcd.xyzw.gz

Any good tip?
Thanks
# 2  
Old 08-14-2009
Hope you for something like this

Code:
find /dir -type f|while read fname
do
   extn=`echo $fname|awk -F'.' '{print $NF}'`
   if [ extn = "Z" ]
   then
     unzip $fname
     fname=`echo $fname|sed 's/\(.*\)\(.Z\)/\1/'`
   fi
   gzip $fname
done

Regards,

Ranjith
# 3  
Old 08-14-2009
thanks a lot Ranjith.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Append string to all the files inside a directory excluding subdirectories and .zip files

Hii, Could someone help me to append string to the starting of all the filenames inside a directory but it should exclude .zip files and subdirectories. Eg. file1: test1.log file2: test2.log file3 test.zip After running the script file1: string_test1.log file2: string_test2.log file3:... (4 Replies)
Discussion started by: Ravi Kishore
4 Replies

2. Shell Programming and Scripting

Unzip all the files with subdirectories present and append a part of string from the main .zip files

Hi frnds, My requirement is I have a zip file with name say eg: test_ABC_UH_ccde2a_awdeaea_20150422.zip within that there are subdirectories on each directory we again have .zip files and in that we have files like mama20150422.gz and so on. Iam in need of a bash script so that it unzips... (0 Replies)
Discussion started by: Ravi Kishore
0 Replies

3. UNIX for Dummies Questions & Answers

Foreach loop through subdirectories in csh

Hi You might find it very trivial but actually don't know how to loop through all sub-directories and their child directories into a csh. bash was easier I believe but here I am, stuck with csh. So elaborately here's my problem: Let's say I have my parent directory named C-H/ under which I have... (15 Replies)
Discussion started by: saleheen
15 Replies

4. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

5. UNIX for Advanced & Expert Users

gzip vs pipe gzip: produce different file size

Hi All, I have a random test file: test.txt, size: 146 $ ll test.txt $ 146 test.txt Take 1: $ cat test.txt | gzip > test.txt.gz $ ll test.txt.gz $ 124 test.txt.gz Take 2: $ gzip test.txt $ ll test.txt.gz $ 133 test.txt.gz As you can see, gzipping a file and piping into gzip... (1 Reply)
Discussion started by: hanfresco
1 Replies

6. Shell Programming and Scripting

Please help me on how to loop subdirectories

Here is my question in bash for f in f1 f2 do cd $f cd ??? # i need to enter the two layers of sub folders then find the folder named "abcde" ? cd .. # how to get out two layers subdirectories? cd .. done (3 Replies)
Discussion started by: ksgreen
3 Replies

7. UNIX for Dummies Questions & Answers

Please help me on how to loop subdirectories

Here is my question in bash for f in f1 f2 do cd $f cd ??? # i need to enter the two layers of sub folders then find the folder named "abcde" ? cd .. # how to get out two layers subdirectories? cd .. done (2 Replies)
Discussion started by: ksgreen
2 Replies

8. Shell Programming and Scripting

Bash: Gzip files in Directory and itīs Subdirectories

Hello dear Community, I have a task to wrtie a script which will gzip not zipped files in a directory and itīs subdirectories. I succeeded in gzippung the directory but not the subdirectories: #/bin/bash #go to the directory where to zip cd $1 #Zip unzipped files for i in `ls | xargs... (2 Replies)
Discussion started by: JamesCarter
2 Replies

9. UNIX for Dummies Questions & Answers

list the files but exclude the files in subdirectories

If I execute the command "ls -l /export/home/abcde/dev/proj/code/* | awk -F' ' '{print $9}' | cut -d'/' -f6-8" it will list all the files in /export/home/abcde/dev/proj/code/ directory as well as the files in subdirectories also proj/code/test.sh proj/code/test1.c proj/code/unix... (8 Replies)
Discussion started by: shyjuezy
8 Replies

10. Shell Programming and Scripting

unzip particular gzip files among the normal data files

Hello experts, I run Solaris 9. I have a below script which is used for gunzip the thousand files from a directory. ---- #!/usr/bin/sh cd /home/thousands/gzipfiles/ for i in `ls -1` do gunzip -c $i > /path/to/file/$i done ---- In my SAME directory there thousand of GZIP file and also... (4 Replies)
Discussion started by: thepurple
4 Replies
Login or Register to Ask a Question