Exclude files in gzip command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exclude files in gzip command
# 1  
Old 05-01-2014
Blade Exclude files in gzip command

hi,

I would like to exlcude certain files which starts with AUS from a directory while gzip the files but i need it in gzip command only

Code:
direct:
AUS1.tx
AUS2.txt
NZ1.txt

i want to gzip only NZ1.txt to NZ1.gz files starting with AUS should not be gzipped and i need it in gzip command only
# 2  
Old 05-01-2014
loop through the list of files and gzip the ones you need - here is one way that is easy to understand:
Code:
cd /path/to/files
ls |
  while read fname
  do 
       echo $fname | grep -q '^AUS' 
       [ $?  -eq 0 ]  && continue  # skip AUS files
       gzip $fname
  done

# 3  
Old 05-01-2014
hi is it possible without loop becoz this is will impact the performance becoz there are huge number of files in the directory with huge sizes
# 4  
Old 05-01-2014
try
Code:
gzip NZ*

# 5  
Old 05-01-2014
hi there will be other files other than NZ it was just an example i want to exclude only files starting with AUS using gzip command
# 6  
Old 05-01-2014
create AUS.sh and run ./AUS.sh
Code:
for file in $(ls -ltr | grep ^- | grep -v "AUS" | awk '{print $9}');
do
gzip $file
done

Remember here loop is to only zip files one by one and not for any conditional check

One more way without looping
Code:
gzip `ls -ltr | grep ^- | grep -v "AUS" | awk '{print $9}'`

more short
Code:
gzip `ls | grep -v "AUS"`


Last edited by Makarand Dodmis; 05-01-2014 at 10:34 AM..
# 7  
Old 05-01-2014
Quote:
Originally Posted by Makarand Dodmis
create AUS.sh and run ./AUS.sh
Code:
for file in $(ls -ltr | grep ^- | grep -v "AUS" | awk '{print $9}');
do
gzip $file
done

Remember here loop is to only zip files one by one and not for any conditional check

One more way without looping
Code:
gzip `ls -ltr | grep ^- | grep -v "AUS" | awk '{print $9}'`

Your regular expression is underspecified. The goal is to eliminate files beginning with AUS, but you are eliminating anything that contains AUS. A leading anchor is required.

Using find is a much better solution; it is just as efficient (if not more so) and is safe against exceeding ARG_MAX with a massive argument list:
Code:
find . -type f \! -name 'AUS*' -exec gzip {} +

Regards,
Alister

---------- Post updated at 09:39 AM ---------- Previous update was at 09:34 AM ----------

If the shell supports it, and if you're willing to risk the implosion, you can use:
Code:
gzip !(AUS*)

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Exclude directories in FIND command

Can you please help tweak the below command to exclude all directories with the name "logs" and "tmp" find . -type f \( ! -name "*.tar*" ! -name "*.bkp*" \) -exec /usr/xpg4/bin/grep -i "user_1" /dev/null {} + >result.out bash-3.2$ uname -a SunOS mymac 5.10 Generic_150400-26 sun4v sparc sun4v... (9 Replies)
Discussion started by: mohtashims
9 Replies

2. Shell Programming and Scripting

Exclude files in ls

Hi, I need to exlucde the files which are present in exclude.txt from a directory exlcude.txt AUZ.txt AUZ.chk NZ.txt NZ.chk tried with below code but not working ls -ltr | grep -v `cat exclude.lst` (9 Replies)
Discussion started by: rohit_shinez
9 Replies

3. Shell Programming and Scripting

Need to exclude .NFSxxx files in clear old files batch script

I am new to Shell Scripting and need some help. The following batch job has been failing for me due to the .nfsxxx files in use. I need to know how to modify the following script to exclude the .nfsxxx files so this batch job will not fail on me. I have done lots of googling and keep coming back... (2 Replies)
Discussion started by: kimberlyg2007
2 Replies

4. UNIX for Dummies Questions & Answers

Find command to exclude files with no extension

The below 'ls' command will list down files with extensions and suppress the ones with no extension ls |grep "\\." But this dosen't work when I apply the same logic using 'find' command find . -type f |grep "\\." I need help on how this logic can be implemented using 'find' command (3 Replies)
Discussion started by: meenavin
3 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

Find command with exclude

I had a Shell script that removes the files that are in a directory older than the specified days. find /test/files -mtime +10 I would like to add another condition to the find command above that is to exclude any file starting with ‘CGU' Thanks (1 Reply)
Discussion started by: db2dbac
1 Replies

7. Shell Programming and Scripting

Using GZIP command

Hi All, Is it possible to redirect the o/p of gzip command to another file? I need to store the o/p in a separate file Regards, sh_kk (7 Replies)
Discussion started by: sh_kk
7 Replies

8. 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

9. 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

10. UNIX for Advanced & Expert Users

du (exclude files)

Hi, I want to get the disk usage of a directory. But I want it to ignore a particular directory within it. Lets say I want disk usage of all files/dirs within dir1 except those that are named .snapshot Does du have the option of excluding a particular directory. (1 Reply)
Discussion started by: the_learner
1 Replies
Login or Register to Ask a Question