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?

  • What have you tried? Paste your current script in the question to solicit better answers! Jul 2, 2012 at 12:17
  • 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.
    – jlacroix82
    Jul 2, 2012 at 12:56
  • 1
    @jlacroix82: You can paste the script in pastebin and put the link here.
    – fmanco
    Jul 2, 2012 at 13:08
  • 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
    Jul 2, 2012 at 16:01

3 Answers 3


Something like

[ "$(ls -b | wc -l)" -gt 1 ] && { ... your statements ... }
  • Is there any way to do it with an if statement?
    – jlacroix82
    Jul 2, 2012 at 12:24
  • 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
    – Izzy
    Jul 2, 2012 at 12:30
  • The statements still run whether there is more than 1 file in the directory or not. Should I increase -gt 1 to -gt 2?
    – jlacroix82
    Jul 2, 2012 at 12:54
  • @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.
    – fmanco
    Jul 2, 2012 at 14:33
  • There are no subdirectories at all, unless it's counting ..
    – jlacroix82
    Jul 2, 2012 at 15:54

I needed to use this:

shopt -s nullglob
files=($dir/*) # $dir was declared earlier if you look at my script
if (( "${#files[@]}" >= 2 ));
shopt -u nullglob

The reason why none of the solutions posted here worked is because of the following. The script is executing the statements AGAINST the directories, it's not actually CD'ing IN to the directories. By telling it what folder I'm working with ($dir) it gets the hint and works as expected.

  • 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.
    – fmanco
    Jul 2, 2012 at 20:13

I will use something like:

    nf=`find . -maxdepth 1 -type f | wc -l`

    if [ "$nf" -gt "1" ] ; then
            <do something here>

This approach will only count files in the directory pointed by dir and discard any possible subdirectory.

Don't forget to change dir accordingly to your needs.

  • Just tried it, and the <do something here> will not run, even if there are files in the directory.
    – jlacroix82
    Jul 2, 2012 at 12:38
  • 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".
    – Izzy
    Jul 2, 2012 at 12:49
  • @Izzy: That's wrong. The comparisson works with Strings.
    – fmanco
    Jul 2, 2012 at 12:57
  • @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.
    – fmanco
    Jul 2, 2012 at 12:59
  • @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 ;)
    – Izzy
    Jul 2, 2012 at 13:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .