Score:1

Find a directory with a prefix in name

do flag

I have one big directory, that contains many subdirectories whose names are staring with 13xxx,10xxx,11xxx and so on. These directories contains text files with a name my_file.txt. Now the problem is, I want to change the files in the directories 13xxx and 11xxx only and don't want to touch files in 10xxx directories. I am trying to search directories with:

[ -d "13*"] && ..
[ -d "13"*] && ..

But seems like it doesn't work like this. Can anyone provide a small code that solves this riddle?

Score:1
in flag

You can use a for-loop and globbing like:

for d in 1[13]*; do
    do_something "$d/my_file.txt";
done

or

for f in 1[13]*/my_file.txt; do
    do_something "$f";
done
do flag
This is performing operation to every file in every directory. So all files from 11xxx, 10xxx and 13xxx were changed.
do flag
Thanks man for the hint. I figured it out from the hint you have given.
pLumo avatar
in flag
Strange, should work fine exactly like this and 10xxxx should not be included. Maybe you did something wrong?!
Score:1
do flag

So the answer was :

for d in 11*/; do
    echo "#Text to be added" >> "$d"/my_file.txt;
done
for d in 13*/; do
    echo "#Text to be added" >> "$d"/my_file.txt;
done
eg flag
Don't forget to accept this as the answer to your question.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.