How to check if a folder is empty or not and use its content in an if-then-else-statement?

kunal

I need to check if a folder is empty or not and according to the output I need to run some other commands. I'm working on Ubuntu 18.04.5 LTS.

My bash script:

if [ "$(ls -A /mnt/mamdrive/"As Metadata"/)" ] || ["$(ls -A /mnt/mamdrive/"As Video"/)"  ]; then
    echo "copy file"
else
    echo "dont copy"
fi

The condition works sometimes, but sometimes it doesn't and it's hard to reproduce it. Is there any other way to check if the directory is empty and do some action accordingly?

steeldriver

I'd suggest something that doesn't rely on the string output of ls - for example, testing if there are any results of a glob expansion:

#!/bin/bash

shopt -s nullglob       # don't return literal glob if matching fails
shopt -s dotglob        # make * match "almost all" like ls -A

set -- /mnt/mamdrive/"As Metadata"/*

if (( $# > 0 )); then
  echo "not empty"
else
  echo "empty"
fi

If you want to test whether two directories are both empty, you can simply glob both of them:

set -- /mnt/mamdrive/"As Metadata"/* /mnt/mamdrive/"As Video"/*

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Check if folder is empty or not php

From Dev

How to check content of zip folder before uploading?

From Dev

How to move folder and its content in powershell

From Dev

how to use if-else statement in accessor?

From Dev

How to use an if else statement in the middle of an echo

From Dev

How to remove all empty files within folder and its sub folders?

From Dev

How to check if a UILabel is empty and append content to a label?

From Dev

Rails – How to use if else statement in array?

From Dev

IF statement To Check If DataSet Is Empty

From Dev

How to use AND/OR in if else PHP statement

From Dev

How to use espresso matcher as a condition for If else statement?

From Dev

How to use if-else statement with function returns?

From Dev

delete a folder and its content

From Dev

c# beginner-How to use if-statement to check textbox is empty?

From Dev

How do you use an if/else statement with an ActionSheet?

From Dev

How to use else single line statement in Perl ?

From Dev

How to use multiple lines of code for an else statement?

From Dev

How to use a for loop with an if-else statement

From Dev

How to move folder and its content in powershell

From Dev

How to check if folder has any content in puppet

From Dev

how to use if-else statement in accessor?

From Dev

How to check if all folder and sub-folder are not empty?

From Dev

Rails – How to use if else statement in array?

From Dev

IF statement To Check If DataSet Is Empty

From Dev

How to use AND/OR in if else PHP statement

From Dev

How to use HTML in a Javascript if/else statement?

From Dev

How to use if/else statement in Selenium IDE

From Dev

How to use else statement in jade template?

From Dev

How to use if and else inside echo statement?

Related Related

  1. 1

    Check if folder is empty or not php

  2. 2

    How to check content of zip folder before uploading?

  3. 3

    How to move folder and its content in powershell

  4. 4

    how to use if-else statement in accessor?

  5. 5

    How to use an if else statement in the middle of an echo

  6. 6

    How to remove all empty files within folder and its sub folders?

  7. 7

    How to check if a UILabel is empty and append content to a label?

  8. 8

    Rails – How to use if else statement in array?

  9. 9

    IF statement To Check If DataSet Is Empty

  10. 10

    How to use AND/OR in if else PHP statement

  11. 11

    How to use espresso matcher as a condition for If else statement?

  12. 12

    How to use if-else statement with function returns?

  13. 13

    delete a folder and its content

  14. 14

    c# beginner-How to use if-statement to check textbox is empty?

  15. 15

    How do you use an if/else statement with an ActionSheet?

  16. 16

    How to use else single line statement in Perl ?

  17. 17

    How to use multiple lines of code for an else statement?

  18. 18

    How to use a for loop with an if-else statement

  19. 19

    How to move folder and its content in powershell

  20. 20

    How to check if folder has any content in puppet

  21. 21

    how to use if-else statement in accessor?

  22. 22

    How to check if all folder and sub-folder are not empty?

  23. 23

    Rails – How to use if else statement in array?

  24. 24

    IF statement To Check If DataSet Is Empty

  25. 25

    How to use AND/OR in if else PHP statement

  26. 26

    How to use HTML in a Javascript if/else statement?

  27. 27

    How to use if/else statement in Selenium IDE

  28. 28

    How to use else statement in jade template?

  29. 29

    How to use if and else inside echo statement?

HotTag

Archive