Mehdi Anis
Mehdi Anis

Reputation: 358

Unix script to delete file if it contains single line

Consider I have a file abcde.txt which may contain one or more lines of text. I want a script that will DELETE the file if it contains single line.

Something like, if 'wc -l abscde.txt' = 1 then rm abscde.txt

My system : Solaris

Upvotes: 1

Views: 4681

Answers (3)

William Pursell
William Pursell

Reputation: 212424

This is (mostly) just a reformat of Ben's answer:

wc -l $PATH | grep '^1 ' > /dev/null && rm -f $PATH

Upvotes: 0

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72795

delifsingleline () {
    if [ $(cat $1 | wc -l) = "1" ]
    then
        echo "Deleting $1"
        echo "rm $1"
    fi
}

Lightly tested on zsh. Should work on bash as well.

Upvotes: 0

Ben
Ben

Reputation: 1210

Here's a simple bash script:

#!/bin/bash

LINECOUNT=`wc -l abscde.txt | cut -f1 -d' '`

if [[ $LINECOUNT == 1 ]]; then
   rm -f abscde.txt
fi

Upvotes: 3

Related Questions