0

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.

6
  • If you go to the directory and do ls -lrth | head -n 848, what do you get then? Do you get a valid list of files? There's no file named -w? Commented May 22, 2016 at 14:03
  • yes am getting valid list. there is no file named with w Commented May 22, 2016 at 14:05
  • am not able to delete the value of count. Commented May 22, 2016 at 14:20
  • Are there any files with names that start with "-", or contain spaces or other unusual characters? BTW, you really should have an error check on the cd command (e.g. cd cd /vol/cdr/MCA || { echo "Error cd'ing to MCA" >&2; exit 1; }) -- otherwise if it fails, the script will be deleting files in some other directory... Commented May 22, 2016 at 14:43
  • This is the ls output: [root@uenbe1 MCA]# ls -lrth | head total 29G -rw-r--r--. 1 root root 9.6M May 21 11:48 uenbe1_1463811505382_5715272012959687109.cdr -rw-r--r--. 1 root root 9.6M May 21 11:49 uenbe1_1463811535549_9088964979948456061.cdr -rw-r--r--. 1 root root 9.6M May 21 11:49 uenbe1_1463811565349_8232105259091872441.cdr -rw-r--r--. 1 root root 9.6M May 21 11:50 uenbe1_1463811595836_778926381450928326.cdr -rw-r--r--. 1 root root 9.6M May 21 11:50 Commented May 22, 2016 at 15:12

3 Answers 3

1

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/'{}' \;
Sign up to request clarification or add additional context in comments.

Comments

0

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 '{}' \;

2 Comments

in this directory each file has same size i.e 9.6 MB. i want to do that it will delete the oldest files when it cross 75%
I guess you want to clean up old files based on dates, often get this question, check my blog if it helps you san-linux.blogspot.in/2010/06/find-files-betweend-dates.html
0
>  `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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.