find files with 1 single row - UNIX

Hi,

How could Find files with 1 single row in unix

I need delete files with 1 single row

Is this a homework assignment? Homework and coursework questions can only be posted in the Homework & Coursework forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

If you did not post homework, please explain the company you work for and the nature of the problem you are working on. Tell us what operating system and shell you're using. Tell us where the files you want to remove are located. Tell us whether the files you want to remove are all in a single directory or are located in a file hierarchy. It would seem that some combination of wc and rm probably with find or a shell for loop should provide what you need.

It's for my job,For security reasons the company could not say, but I work on a migration of information,Find files with 1 single row in unix,
The files with 1 single row do not work, because they only have the header, I wanted to erase them

Assuming that the path of the file to investigate is stored on variable file, the naive approach to test, whether it is a candidate for deletion, would be (in, say, bash or zsh)

if [[ $(wc -l <$file) == 1 ]]
then
    ....
fi

Alternatively, you could also write in this particular case

if [[ $(wc -l <$file) -eq 1 ]]

or

if (( $(wc -l <$file) == 1 ))

The problem is how you want to treat files which have some content, but no terminating newline, and files which are completely empty. For them, wc -l would report zero. Depending on whether or not you want to delete them too, you would have to adapt the condition accordingly.

1 Like

Would this thread help?

I only want to delete the files of 1 single line, the other files if they are empty or with information for me is ok, I do not have files without line termination.

So - how would you adapt the hint given?

How about this? I added the redirects to filter out the errors on files that I could not read.
You can leave that in or take it out. I also chopped off the line count, word count character count.
I added the second part to remove the files. I have not tested the code for the remove.
But it should work.

# to display the files
for line in `find * -type f`
do
   if [ `wc -l $line | cut -d" " -f1` = 1 ]
   then
      echo "$line"
   fi
done

# to remove the files
for line in `find * -type f`
do
   if [ `wc -l $line | cut -d" " -f1` = 1 ]
   then
      rm -vi "$line"
   fi
done

If it is in a single directory, in pure shell, try:

for i in *
do
  if [ -f "$i" ] && read && ! read; then
    echo "$i"
  fi < "$i"
done
1 Like