how to increment with find -exec?
Asked Answered
G

4

6

I Would like to do something like that

#!/bin/bash

nb=$(find . -type f -name '*.mp4' | wc -l)
var=0
find . -type f -name '*.mp4' -exec ((var++)) \;
echo $var

But it doesn't work ? Can you help me ?

Gawain answered 25/9, 2017 at 19:54 Comment(8)
What's nb for? And what command do you want to run? What do you want to achieve overall?Antispasmodic
Do you want to count the number of files found by your find command?Antispasmodic
BTW, find ... -print | wc -l isn't an accurate count for all possible file names. Try creating a file with touch $'name\nwith\nfour\nlines' -- it'll be counted by wc as four separate files. That's why you're safer using NUL delimiters (as with -print0) -- they can't show up in filenames themselves.Triceps
@CharlesDuffy Or you can do something like -printf '.' and count the periods.Antispasmodic
Yup. I want to say I've seen -print0 in some non-GNU finds that don't have -printf, though. (Actually -- confirmed: MacOS has -print0 but not -printf).Triceps
ok but where should i put printf or print0 ?Gawain
@romainlavisse, as the action for find. There's a default action of -print in some versions (which your original code is relying on), but it's not good practice to do so (there are cases where that default action will take place when you don't want it to -- that's often the case when a find expression includes -prune, for example).Triceps
What are you actually trying to do? Incrementing a variable (even supposing you could) serves no useful purpose. What do you want to use it for? Renaming files? If so, there is a rename command.W
T
6

You can't. Each exec is performed in a separate process. Those processes aren't part of your shell, so they can't access or change shell variables. (They could potentially read environment variables, but updated versions of those variables would be lost as soon as the processes exited; they couldn't make changes).

If you want to modify shell state, you need to do that in the shell itself. Thus:

#!/usr/bin/env bash
#              ^^^^- NOT /bin/sh; do not run as "sh scriptname"

while IFS= read -r -d '' filename; do
  ((++var))
done < <(find . -type f -name '*.mp4' -print0)

Note preincrement vs postincrement -- that helps you avoid some gotchas if you're running your script with set -e (though I'd argue that the better practice is to avoid that "feature").

See Using Find for details.

Triceps answered 25/9, 2017 at 20:0 Comment(0)
F
1

is this what you require:

bash-4.4$ var=$(find . -name "*.mp4" -exec echo {} \;|wc -l)                                                                                                                        
bash-4.4$ echo $var                                                                                                                                                                 
4   

it counts the number of *.mp4 files inside the dir and assigns the number to var.

Fitted answered 26/9, 2017 at 6:21 Comment(2)
Filenames can have newlines in them -- an ideal answer would avoid double-counting in that case.Triceps
To test this yourself: touch $'hello\ncruel\nworld.mp4'; echo hello*.mp4 | wc -lTriceps
P
0

This is using find but not with the option -exec, if you just want to store in a variable the number of items found, something like this could work:

#!/bin/bash

var=$(find . -type f -name '*.mp4' | wc -l  | awk '{print $1}')

echo $var 
Procrustes answered 25/9, 2017 at 20:23 Comment(2)
Put the single quotes outside the curly braces if you want to be sure the braces are passed to awk by your shell (which otherwise will be handling them in an implementation-dependent manner which can vary between shells). That is: '{print $1}', not {'print $1'}Triceps
the first line in my script already do that. What i want is to know each time i got a ".mp4" i want to increment a countGawain
H
0

short and sweet with the help of -c option in egrep

ALP ❱ find . | egrep mp4$
./T/How_to_Use_Slang_at_the_Market_English_Lessons.mp4
./T/How_to_Use_Slang_on_the_Road_English_Lessons.mp4
./T/How_to_Use_Slang_on_Vacation_English_Lessons.mp4
./T/How_to_Use_Slang_at_the_Airport_English_Lessons.mp4
./T/How_to_Use_Slang_to_Talk_about_Health_English_Lessons.mp4
./list-mp4
ALP ❱ find . | egrep -c mp4$
6
ALP ❱ 
Hetaerism answered 26/9, 2017 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.