Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a folder has contents? [duplicate]

I'm trying to create a Bash script that will delete everything in my .waste directory. I have a basic script I wrote but I want it to first check if the .waste directory has contents, and if so, to echo out a simple "Folder already empty!" message. I'm not too savvy about if and if else statements, and I don't know what the [ ] equation needs to check for presence.

Basic code:

#! /bin/bash
echo "The files have been deleted:"
cd /home/user/bin/.waste/
ls
rm -rf /home/user/bin/.waste/*

(P.S. not sure if the asterisk is correct at the end, I did try the script with it and I recall it deleted everything in the bin directory as well)

like image 289
Syler Avatar asked Dec 08 '13 17:12

Syler


People also ask

How do I find duplicates in a folder and subfolders?

You'll want to select “Duplicates Search” in the Search Mode box at the top of the window and then choose folders to search by clicking the “Browse” button to the right of Base Folders. For example, you could select C:\ to search your entire C: drive for duplicates.

How do I compare duplicate files in a folder?

Click on the “Select Files or Folders” tab in the far left, to start a new comparison. Each comparison you run opens in a new tab. To start a new comparison, click on the “Select Files or Folders” tab in the far left, change the targets and click “Compare” again.

Does Windows 10 have a duplicate file finder?

Answer: No, Windows 10 does not have a duplicate finder in it yet.


3 Answers

You can check if a directory is empty using find, and processing its output:

#!/bin/sh
target=$1
if find "$target" -mindepth 1 -print -quit 2>/dev/null | grep -q .; then
    echo "Not empty, do something"
else
    echo "Target '$target' is empty or not a directory"
fi

That is:

  • Use find to find the first filesystem entry under $target (-mindepth 1), print it (-print), and stop processing (-quit)
    • Redirect stderr to suppress any error messages (= noise)
  • Check if the output of the find command is empty using grep -q .
    • grep -q . will exit after processing at most one character. If it sees a character it exits with success, if it doesn't (its input is empty) then it exits with failure.
  • If the output of the find command is not empty, then the directory is not empty, and grep -q . exits with success.
  • If the output of the find command is empty, then $target is either an empty directory, or not a directory (does not exist), and grep -q . exits with failure.

The reason we have to rely on the stdout of find rather than its own exit code directly is that there's no way to make the find command use distinguishable exit codes in case files were found or not.

Instead of piping to grep -q, another alternative would be to capture the output of find and check if it's an empty string or not.

#!/bin/sh
target=$1
if [ "$(find "$target" -mindepth 1 -print -quit 2>/dev/null)" ]; then
    echo "Not empty, do something"
else
    echo "Target '$target' is empty or not a directory"
fi

Capturing command output like this uses a sub-shell. I think the solution using grep is probably faster, but I haven't tested it.

like image 90
janos Avatar answered Oct 07 '22 20:10

janos


GNU find will let you do this

find . -maxdepth 0 -empty -exec echo {} is empty. \;

pretty quick and no pipes

like image 27
kdubs Avatar answered Oct 07 '22 20:10

kdubs


Sorry, I don't have enough rep to answer Dominik's comment with a comment, so this is the best I can do...

My find on Solaris has no maxdepth option... how can I achieve the same? – Dominik Nov 14 at 12:46

I can't say for sure on earlier versions, but on Solaris 10 or better:

find . ! -name . -prune 
like image 5
Reese Murdock Avatar answered Oct 07 '22 21:10

Reese Murdock