开发者

How can I process the results of find in a bash script?

开发者 https://www.devze.com 2022-12-16 14:39 出处:网络
I\'m trying to use an array to store a list of file names using the find command. For some reason the array fails to work in the bash used by the school, my program works on my own laptop though.

I'm trying to use an array to store a list of file names using the find command.

For some reason the array fails to work in the bash used by the school, my program works on my own laptop though.

So I was wondering if there's another way to do it, this is what i have:

array = (`find . -name "*.txt"`)  #this will store all th开发者_JAVA技巧e .txt files into the array

Then I can access the array items and make a copies of all the files using the cat command.

Is there another way to do it without using an array?


You could use something like this:

find . -name '*.txt' | while read line; do
    echo "Processing file '$line'"
done

For example, to make a copy:

find . -name '*.txt' | while read line; do
    echo "Copying '$line' to /tmp"
    cp -- "$line" /tmp
done


I was having issue with Johannes Weiß's solution, if I was just doing an echo it would work for the full list of files. However, if I tried running ffmpeg on the next line the script would only process the first file it encountered. I assumed some IFS funny business due to the pipe but I couldn't figure it out and ran with a for loop instead:

for i in $(find . -name '*.mov' ); 
do
    echo "$i"
done


I think starpause has the cleanest solution, however it fails when there is whitespaces in paths. This is fixed by setting IFS. The correct answer is therefore:

IFS=$'\n'
for i in $(find . -name '*.mov' ); 
do
    echo "$i"
done
unset IFS

You unset IFS in order to reset behaviour for IFS and as to why the $ is needed in IFS=$'\n', see https://unix.stackexchange.com/questions/184863/what-is-the-meaning-of-ifs-n-in-bash-scripting


Just don't put blanks around the equals sign:

ar=($(find . -name "*.txt"))

Avoid backticks, if possible, since they're deprecated. They can be easily confused with apostroph, especially in poor fonts, and they don't nest so well.

In most cases you will be best served if you iterate through a find-result directly with -exec, -execdir, -ok or -okdir.

For and while loops are hard to do right when it comes to blanks in filenames or newlines and tabs.

find ./ -name "*.txt" -exec grep {} ";"

The {} doesn't need masking. You will often see a combination find/xargs which starts an additional process too:

find ./ -name "*.txt" | xargs grep {} ";"


find . -name '*.txt' | while IFS= read -r FILE; do
    echo "Copying $FILE.."
    cp "$FILE" /destination
done


One more variant to change some variable inside while loop which uses subshell

concat=""

while read someVariable
do
    echo "someVariable: '$someVariable'"
    concat="$concat someVariable")
done < <(find "/Users/alex" -name "*.txt")

echo "concat: '$concat'"


Generalized version tested with whitespace

echo $BASH_VERSION
4.2.46(2)-release

Make some horrible looking directories and files

mkdir "spaces test"
cd spaces\ test/
touch 'test one'{1..5}
touch 'testone'{1..5}
mkdir test\ two
cd test\ two/
touch 'test one'{1..5}

Create and run the script

set -u
while read -r line
do
    echo "$line"
done < <(find "spaces test" -type f)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号