WebJan 19, 2024 · We used the following approach in order to verify if a folder is empty: [ [ "$ (ls -A /var/folder)" ]] && echo "not empty" on /var/folder, we did the following: ls -l /var/folder total 0 indeed we not get any output. but when we run the shell as [ [ "$ (ls -A /var/folder)" ]] && echo "not empty" then it prints not empty WebDec 30, 2024 · To check if a file is empty using find, you can use the -empty option, which matches files that are empty: #!/usr/bin/env bash FILENAME=myfile.txt if find . -type f …
command line - how to check if $1 and $2 are null? - Ask Ubuntu
WebMar 7, 2024 · The find command does have a "-empty" test that will return "true" for an empty file or directory. Whether that is considered a "simple way" depends on the individual and the situation. ... So the fastest way to check if directory is empty is indeed to list it. Last edited by dugan; 03-07-2024 at 02:25 PM. 1 members found this post … WebApr 6, 2014 · A very frequent approach that you see around is this: if [ "x$var1" == "x" ] && [ "x$var2" == "x" ]; then echo "both variables are null" fi To quote Gilles: In [ "x$1" = "x" ], the x prefix ensures that x"$1" cannot possibly look like an operator, and so the only way the shell can parse this test is by treating = as a binary operator. ind to pap
How to Check If a File Exists in Linux Bash Scripts - How-To Geek
WebAug 28, 2024 · find in conjunction with ifne can work as a copy-on-empty: $ find test/ -maxdepth 0 -empty ifne cp -t test/ a Using it in an if statement can look something like … WebMay 7, 2015 · A simple way of testing whether a directory is empty is to list its content; if you get empty text, the directory is empty. if [ -z "$ (ls -A -- "$dir")" ]; then ... fi On older … WebAug 29, 2024 · The easiest way is to use ls -A in an if statement like this: path=$ (ls -A '/whatever/sub directory/more spaces') if [ [ ! -z "$path" ]]; then echo "Directory is NOT empty!" else echo "Directory is empty!" fi Share Improve this answer Follow edited Aug 29, 2024 at 9:24 answered Aug 29, 2024 at 8:56 Stormlord 5,689 2 24 39 ind to palm springs