1

Just as the title says. I know how to use find's arguments in line with it, but not how to use them as if statements while evaluating a find return variable. For example:

find . -type f

This obviously finds all the files, and how I'm looking to use it is:

ARRAY=$(find .)
for item in $ARRAY
do
if [[ -f $item ]] ; then

And that does the same thing, it only deals with the files from the find. I obviously know how to do it with the -type argument, but not the others. For example:

find . -size +512k

if I want size, doing if [[ -size +512k $item ]] doesn't seem to work as expected. How do I use it?

Others I can't figure out how to use:

find . -executable
find . \( -name "*.html" -or -name "*.css" \)
find . -mtime +364

How would I use each of these in if statements if I'm looping over the results of a find?

Additionally, how would I grab the total size of a find result, after the fact, not during the find. Say I have my ARRAY=$(find .) and what to do a du -ch against my $ARRAY variable. How would I do that?

Sorry, I'm a bash noob and its syntax is proving difficult for me.

user3066571
  • 1,381
  • 4
  • 14
  • 37
  • Is there a single question, or multiple questions? Or are you not sure what the question is? – sjsam Jan 20 '18 at 17:07
  • mutiple questions. It's wordy, I know. – user3066571 Jan 20 '18 at 17:08
  • 2
    `bash` and `find` are two completely separate commands. You can't use arguments for one with the other. – chepner Jan 20 '18 at 17:14
  • Looks like an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me. The test(`[ ]`) or extended test(`[[ ]]`) are primitive enough so that they can't deal with everything that comes to your mind. – sjsam Jan 20 '18 at 17:15
  • 1
    @user3066571 As a side note, `ARRAY=$(find .)` might not be doing what you think it does. Moreover, using full uppercase identifiers for user defined variables is a very bad idea as it may break built-in variables which are meant for the shell. `:-(` – sjsam Jan 20 '18 at 17:18

2 Answers2

0

There's really no relationship between the find command (which is a standalone program, not actually part of Bash) and the [[ ... ]] syntax (which is built into Bash). So it should be no surprise that options supported by the find command need not have counterparts supported by the [[ ... ]] syntax.

For a complete list of what's supported by the [[ ... ]] syntax, see the Bash Reference Manual, §3.2.4.2 "Conditional Constructs" and §6.4 "Bash Conditional Expressions".

To test if "$foo" is an executable file:

if [[ -x "$foo" ]] ; then

To test if something ends in .html or .css:

if [[ "$foo" == *.html ]] || [[ "$foo" == *.css ]] ; then

The others are not so easy, and will require recourse to external programs such as stat.

ruakh
  • 175,680
  • 26
  • 273
  • 307
0

This does the trick

find . -size +10k -exec bash -c 'f="$1"; if [ $(du -k "$f" | cut -f 1) -gt 100000 ]; then echo "$f" ;fi' _ {} ';'

With the help of this answer: find -exec with multiple commands

LMC
  • 10,453
  • 2
  • 27
  • 52