Need if statement to run a task only if number of files in the directory are greater than 1

5,206

Something like

[ "$(ls -b | wc -l)" -gt 1 ] && { ... your statements ... }
Share:
5,206

Related videos on Youtube

jlacroix82
Author by

jlacroix82

Updated on September 18, 2022

Comments

  • jlacroix82
    jlacroix82 over 1 year

    I want to write a script, but I want an if statement so it will run only if the number of files in the directory are greater than 1. Is this possible?

    • Ozair Kafray
      Ozair Kafray almost 12 years
      What have you tried? Paste your current script in the question to solicit better answers!
    • jlacroix82
      jlacroix82 almost 12 years
      The script is too large to fit in this box. I haven't tried anything yet since I don't have a clue how to do this.
    • fmanco
      fmanco almost 12 years
      @jlacroix82: You can paste the script in pastebin and put the link here.
    • jlacroix82
      jlacroix82 almost 12 years
      Here it is in Pastebin: pastebin.com/4SGFHzVG Please note that I am very sensitive about the script, I don't want anything changed other than the added functionality I'm requesting. I put comments where I want it to check if there are more than one file before doing it.
  • jlacroix82
    jlacroix82 almost 12 years
    Is there any way to do it with an if statement?
  • Izzy
    Izzy almost 12 years
    That is a (hidden/abbreviated) if..then statement. In full length, it would read if [ "$(ls -b | wc -l)" -gt 1 ]; then ... your statements ...; fi
  • jlacroix82
    jlacroix82 almost 12 years
    Just tried it, and the <do something here> will not run, even if there are files in the directory.
  • Izzy
    Izzy almost 12 years
    The [ test ] statement may be incorrect: it uses strings (in quotes), but a numeric comparision (-gt). Try replacing that part by [ $nf -gt 1 ] (for a numeric comparision). If you want to do that with strings, it should be [ "$nf" != "0" -a "$nf" != "1" ]. Moreover you could (for test purposes) output the value stored in $nf before the line starting with "if".
  • jlacroix82
    jlacroix82 almost 12 years
    The statements still run whether there is more than 1 file in the directory or not. Should I increase -gt 1 to -gt 2?
  • fmanco
    fmanco almost 12 years
    @Izzy: That's wrong. The comparisson works with Strings.
  • fmanco
    fmanco almost 12 years
    @jlacroix82: Please. Make sure the current directory is what you want. Don't forget the find is finding files in the current directory. If you want you can change find . to find <directory>. Also you can put the command pwd in your script to know what is your current directory. The script was tested and it works.
  • Izzy
    Izzy almost 12 years
    @criziot: it may work (as sorted alphanumerically, any "string-number" larger than 1 starts with the digit 1 or up), but a) not good style and b) you never know about implementations whether "string -gt string" gets silently converted. The man page of test explicitly states: INTEGER1 -gt INTEGER2: INTEGER1 is greater than INTEGER2. Nothing about strings with "-gt". But don't we argue -- as long as it works ;)
  • fmanco
    fmanco almost 12 years
    @Izzy: Don't forget that -gt always perform arithmetic comparissons. If you try "a" -gt "b" it won't work. But I'm not an expert in bash to discuss this, I wrote it that way because it is how I'm used to see it (like here). And you're right, it is good as long as it works :D
  • Izzy
    Izzy almost 12 years
    @criziot: I'm also used to placing quotes around variables (to avoid trouble with blanks -- regarding those the quote marks are a very good thing), so I thought it was "accidentally". So nevermind, as we agreed: "as long as it works" ;)
  • fmanco
    fmanco almost 12 years
    @jlacroix82: Don't forget this will also count directories, not only files. So if you have two directories inside the current directory the test will succeed even if there are no files.
  • jlacroix82
    jlacroix82 almost 12 years
    There are no subdirectories at all, unless it's counting ..
  • jpalecek
    jpalecek almost 12 years
    @Izzy: There is no difference between "11", 11 and even "1"1 in shell. All are strings with two characters 1.
  • Izzy
    Izzy almost 12 years
    @jpalecek: Thanks, a test confirmed that [ "11" -eq 11 ]. So shell is basically "typeless" -- leaned something new :)
  • Izzy
    Izzy almost 12 years
    @jlacroix82: I just tried if [ $(find . -maxdepth 1 -type f | wc -l) -gt 1 ]; then echo 'Work to do!'; else echo "Go to bed."; fi in a directory with 1 file ("Go to bed.") and with multiple files ("Work to do!") -- so it does work. This is basically the very same construct criziot offered, just "condensed".
  • fmanco
    fmanco almost 12 years
    So the problem is basically what I've told you in my comment. Anyway I've edited my answer so you can see how to use a specific directory, but I'm glad you found a solution.