How to zip or rm the multi part named files?

Hello There,

There are more than 1000 files in my log folder and i want to zip it to relase the space. But my method throwing
syntax error due to the multi part file name, how to overcome in this ?

ls -lart | grep "MDB_Kernel11.1_gwlog_SUN 22_09_2013" | awk '{print $9,$10,$11,$12}' | head -10
MDB_Kernel11.1_gwlog_SUN 22_09_2013 09_21_34 PM_45563.log
MDB_Kernel11.1_gwlog_SUN 22_09_2013 09_21_34 PM_40460.log
MDB_Kernel11.1_gwlog_SUN 22_09_2013 09_21_34 PM_39326.log
MDB_Kernel11.1_gwlog_SUN 22_09_2013 09_21_34 PM_37854.log
MDB_Kernel11.1_gwlog_SUN 22_09_2013 09_21_34 PM_36535.log
MDB_Kernel11.1_gwlog_SUN 22_09_2013 09_21_34 PM_36162.log
MDB_Kernel11.1_gwlog_SUN 22_09_2013 09_21_34 PM_35668.log
MDB_Kernel11.1_gwlog_SUN 22_09_2013 09_21_34 PM_35190.log
MDB_Kernel11.1_gwlog_SUN 22_09_2013 09_21_34 PM_34760.log
MDB_Kernel11.1_gwlog_SUN 22_09_2013 09_21_34 PM_33756.log
for i in 'ls -lart | grep "MDB_Kernel11.1_gwlog_SUN 22_09_2013" | awk '{print $9,$10,$11,$12}' | head -10'
do
gzip $i
done

From above the files having space in between their name, so zip or remove commands throwing error. Please assist.

Gowtham.G

...
gzip "$i"
...

--ahamed

 
for i in 'ls -lart | grep "MDB_Kernel11.1_gwlog_SUN 22_09_2013" | awk '{print $9,$10,$11,$12}' | head -10'

Those quotes should be ` instead of '

for i in MDB_Kernel11.1_gwlog_SUN\ 22_09_2013*
do
  gzip "$i"
done

--ahamed

awk '{print $9,$10,$11,$12}'
This is extracting PART of the file name, you will need the full file name to perform zip or rm commands.

for i in 'ls -lart | grep "MDB_Kernel11.1_gwlog_SUN 22_09_2013"  | head -10'
do
    rm "$I"
done

Thanks everyone for the reply. But am still getting the same error.

for i in `ls -lart | grep "MDB_Kernel11.1_gwlog_TUE 24_09_2013 *" | head -10 | awk '{print $9,$10,$11,$12}' `
> do
> rm "$i"
> done
rm: MDB_Kernel11.1_gwlog_TUE: A file or directory in the path name does not exist.
rm: 24_09_2013: A file or directory in the path name does not exist.
rm: 03_37_06: A file or directory in the path name does not exist.

Please help me.

Faced this issue once. Do this for your gzip:

gzip '"$i"'

(Single quotes,double quotes, $i, double quotes,single quotes)

All of the uses of:

for i in `pipeline`

will set the variable i to the elements in the output from the pipeline split on the spaces and newlines in the output. You need something that will just split on the newlines. If you didn't want to restrict your processing to the oldest ten matching files, you could just use:

for i in "MDB_Kernel11.1_gwlog_SUN 22_09_2013"*

With the restriction, you could try:

ls -rt "MDB_Kernel11.1_gwlog_SUN 22_09_2013"* | head -10 | while IFS='' read -r i
do      gzip "$i"
done

but I'm guessing you probably want something like:

ls -rt "MDB_Kernel11.1_gwlog_SUN 22_09_2013"* | grep -v '.gz$' | head -10 | while IFS='' read -r i
do      gzip "$i"
done

It is working fine with the below command.

find . -name "MDB_Kernel11.1_gwlog_TUE 24_09_2013*"  -type f | xargs -I xx rm -rf xx