I'm quite new to shell scripting so this might be trivial, though 3 days of struggle and search didn't help to solve the problem:
I want to look for files called '*HUN*' in a huge amount of directories most of their names contain whitespaces and print the path of the directory if it contains such a file.
example:
/home/usr/test/f 3/
so I try to use:
[ -e "/home/usr/test/f 3/*HUN*" ] && echo y || echo n
it returns "n", though there is a file called "f_HUN.avi" in the directory.
the funny thing is, the directory is recognised with the -d option, i.e.
[ -d "/home/usr/test/f 3" ] && echo y || echo n
returns "y".
Is there a way to overcome this issue with the space for the -e or -f options?
@OP: By using quotes you are preventing glob expansion so you are testing for a file called *HUN* . And without the quotes it would only work if the asterisks expand to exactly one file. If there are more files, then the test expression will become invalid.
@gaurav1086: it works in deed, but I thought there must be a way to get it working with "test"
Actually I want to have a list with all the directories containing movies without Hungarian subtitles in them. So I have a list of directories and want to check them.
The problem with find is that it will give me a lot of double records, as there are many files in the directories.
Here's the little script I want to use (it seems working now, I must have been changing something):
#!/bin/bash
find $(pwd)/* -type d | while read dir ; do
[ -f "$dir"/*HUN* ] && echo "found in: $dir" || echo "not found in: $dir"
done
---------- Post updated at 09:22 AM ---------- Previous update was at 09:21 AM ----------
@Scrutinizer: thanks for your comment, it explains why it was not working and why the script above seemed to be working but actually giving error messages if found more than 1 file in the dir. Is it possible to fix this with regex? I mean something like this:
find $(pwd)/* -type d | while read dir
do
for file in "$dir"/*HUN*
do
if [ -f "$file" ]
then
echo "found in: $dir"
break
else
echo "not found in: $dir"
fi
done
done
This works. So basically break exits the loop after the first file matching the criteria is found. Is it right? I just want to be sure I understand correctly what the script's doing.