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 ?
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 ?
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.
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.
touch $'hello\ncruel\nworld.mp4'; echo hello*.mp4 | wc -l
–
Triceps 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
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 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 ❱
© 2022 - 2024 — McMap. All rights reserved.
nb
for? And what command do you want to run? What do you want to achieve overall? – Antispasmodicfind
command? – Antispasmodicfind ... -print | wc -l
isn't an accurate count for all possible file names. Try creating a file withtouch $'name\nwith\nfour\nlines'
-- it'll be counted bywc
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-printf '.'
and count the periods. – Antispasmodic-print0
in some non-GNUfind
s that don't have-printf
, though. (Actually -- confirmed: MacOS has-print0
but not-printf
). – Tricepsfind
. 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 afind
expression includes-prune
, for example). – Tricepsrename
command. – W