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

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

loop through the list of files and gzip the ones you need - here is one way that is easy to understand:

cd /path/to/files
ls |
  while read fname
  do 
       echo $fname | grep -q '^AUS' 
       [ $?  -eq 0 ]  && continue  # skip AUS files
       gzip $fname
  done

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

try

gzip NZ*

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

create AUS.sh and run ./AUS.sh

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

gzip `ls -ltr | grep ^- | grep -v "AUS" | awk '{print $9}'`

more short

gzip `ls | grep -v "AUS"`

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:

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:

gzip !(AUS*)

Regards,
Alister

Thanks guys i will try these but i need to exclude file combinations how can i do that for e.g.

cd direct
AUS1.txt
AUS2.txt
NZ1.txt
AUS1.csv
AUS2.csv
USA.tx

i need to exclude file starting with AUS and ends with either .csv or .txt

output:

NZ1.gz
USA.gz

Does that mean if file starts with AUS and end with .log or anything other than (.csv or .txt) you wana zip

if yes then try

gzip `ls | grep -v "AUS.*csv" | grep -v "AUS.*txt"`
1 Like

it starts with AUS and ends with either .csv or .txt i need to exclude AUS.txt and AUS.csv

the above command does the same.have you tried it?

Or

gzip `ls | egrep -v "AUS.*csv|AUS.*txt"`
1 Like

Hi Alister,

will it be possible with your find command to exclude files starts with AUS and ends with either .csv or .txt i need to exclude AUS.txt and AUS.csv

Makarand

That really worked perfect but is there any way i can directly invoke the path using your code instead of applying using cd path. I need this to directly zip from the directory like it does

cd directory path
gzip $directory/*

try

find . -type f \! -name 'AUS*.*csv' -a \! -name 'AUS*.*txt' -exec gzip {} +
1 Like

That's not quite right. Both instances of *.* should be *. .

Regards,
Alister

1 Like

Makarand,

I need to use your code by invoking directory path which i have mentioned in my previous post will the below one work

gzip `ls $directory/| grep -v "AUS.*csv" | grep -v "AUS.*txt"`

It's beginning to seem that you are unwilling to help yourself. We are into double digit post count and there's no indication that you've actually tried any off the suggestions.

Why don't you just go ahead and run some tests? If there are problems or shortcomings, try to fix them yourself. If you succeed, report back with your changes so that others can benefit. If you fail, then post the exact errors seen so that we have more information to work with.

Regards,
Alister

hi

when i am using the test directory getting the below error

gzip `ls test| egrep -v "AUS.*csv|AUS.*txt"`

gzip: can't stat: NZ.csv (NZ.csv): No such file or directory
gzip: can't stat: NZ.txt (NZ.txt): No such file or directory

I have tried all the ways

Try

cd test
ls | ..... 

Hi I don't wanted to use cd test rather than accessing the direct path

Going back to your first post to this thread, you said you have a directory named test in the current directory containing the four files:

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

Now you are saying that you want to invoke the command:

gzip direct: AUS1.tx NZ1.txt

in a directory where none of these files exist.

So, you can choose to use a cd command to get into the directory where those files exist, or you can choose a completely different way to get the list of filenames to be given to gzip .

Why are you so opposed to using the cd command?

What does the following command do:

cd test;gzip `ls | egrep -v '^AUS.*[.](csv|txt)$'

that is different from what you have said you want?