-1

In bash coding,

What to do is that if the grep size is less than 800MB i want to ignore but if grep size is more than 800MB, i want to print that path in file xyz/symlinks_paths.txt. but problem coming in else statement. Can you help me, i want to print that path whose size is more than 800MB in xyz/size_list.txt file. Also, line3 is a directory path.

while
    read -r line3
        do
            if [[ "ls -lh $line3 | grep zzz.exe | grep '[8-9][0-9][0-9][MG]'" = " " ]] 
                then break
                else line3 >> xyz/size_list.txt
            fi
        done < xyz/symlinks_paths.txt
5
  • break breaks out of the loop Commented Feb 19, 2018 at 14:40
  • No, the error says, line3 is a directory OR line3 command not found, that means after break it is in if loop only. Commented Feb 19, 2018 at 14:43
  • stackoverflow.com/questions/21011010/… Commented Feb 19, 2018 at 14:43
  • Maybe you should try with find, which lists all your symlinks and can even give you the information on sizes for found files. Commented Feb 19, 2018 at 14:49
  • No Stefan, i dont want to use find command, because i have to run this on huge database, which will take days on find. Commented Feb 19, 2018 at 14:53

2 Answers 2

1

You really shouldn't parse ls.

On the other hand, you're exiting your while-loop with the break statement. Maybe you can negate your if-condition, so that you can put your else-Part into the then part.

What were you trying to achieve by this line:

line3 >> xyz/size_list.txt

Do you want to append the contents of ${line3} into the file? Than this should work:

echo "${line3}" >> xyz/size_list.txt
Sign up to request clarification or add additional context in comments.

2 Comments

how to negate this if loop?
With a !. See for example this answer to Negate if condition in bash script.
1

Imho, it's better (and more readable) to do like that:

#!/bin/bash

while
    read -r line3
        do
            #819200 kbytes = 800 Mbytes. Echo in file if the size greater or equal
            if [ $(du -s "${line3}/zzz.exe" | awk '{print $1}') -ge 819200 ]
            then 
                echo "${line3}" >> xyz/size_list.txt
            fi
        done < xyz/symlinks_paths.txt

4 Comments

Using ${line3} , still says that line3 is a directory. :(
@Pompy well..can you show me that variable value in your code?
/prj/pika/BMX/pikatree/1504729073xz-dir/pulser
sorry viktor, i forgot the echo. my bad. :)

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.