rohit
rohit

Reputation: 1

Bash script getting error in files

Hi Guys pls help on this...

[root@uenbe1 ~]# cat test.sh
#!/bin/bash
cd /vol/cdr/MCA
no='106'
value='55'
size=`df -kh | grep '/vol/cdr/MCA' | awk '{print $5}'| sed 's/%//g'`
if [ "$size" -gt "$value" ] ;
then
delete=$(($size-$value))
echo $delete
count=$(($no*$delete))
`ls -lrth | head -n  $count   | xargs rm -rf`
fi

output:

+ cd /vol/cdr/MCA
+ no=106
+ value=55
++ df -kh
++ grep /vol/cdr/MCA
++ awk '{print $5}'
++ sed s/%//g
+ size=63
+ '[' 63 -gt 55 ']'
+ delete=8
+ echo 8
8
+ count=848
++ ls -lrth
++ head -n 848
++ xargs rm -rf
rm: invalid option -- 'w'
Try `rm --help' for more information.``

i want to delete these files which in $count.

Upvotes: 0

Views: 180

Answers (3)

user2350426
user2350426

Reputation:

The command ls -lrth prints lines like:

-rw-r--r-- 1 bize bize    0 may 22 19:54 text.txt
-rw-r--r-- 1 bize bize    0 may 22 19:54 manual.pdf

that text given to the command rm will be interpreted as options

$ rm -rw-r text.txt
rm: invalid option -- 'w'

List only the name of files. That is: remove the long -l option from ls (and the -h option since it works only with -l):

$ ls -1rt | head -n "$count" | xargs

But Please: do not make a rm -rf automatic, that is a sure path to future problems.

Maybe?:

$ ls -1rt | head -n "$count" | xargs -I{} echo rm -rf /vol/cdr/MCA/'{}' \;

Upvotes: 1

tripleee
tripleee

Reputation: 189487

>  `ls -lrth | head -n  $count   | xargs rm -rf`

This line has multiple problems. The backticks are superfluous, and you are passing the directory permission, file size, owner information etc as if that were part of the actual file name.

The minimal fix is to lose the backticks and the -l option to ls (and incidentally, the -r option to rm looks misplaced, too); but really, a proper solution would not use ls here at all.

Upvotes: 0

Sanjeev
Sanjeev

Reputation: 107

why are you passing

ls -l

use just, it will find the list of file greater than given size, if you get this list in a file you can then take list of files which are to be deleted or whatever

find /vol/cdr/MCA -type f -size +56320c -exec ls '{}' \;

Upvotes: 0

Related Questions