Checking files in folder using starting string for filename

Hi,

How do i check if there are any files present in the folder with some specific starting string.

For eg :- I have used this where Source_File is filename parameter.

if [ ! -r ${SOURCE_FILE} ]
  then
  return 2
  fi

But in my case the source file name is not constant. The only constant thing is that files with starting string 'XX_IN*' are to be searched.

Please help :frowning:

Chetan

Folder1
  FileA
  DoorB
  DoorC
Folder2
  DoorD
  DoorE
Folder3
  FileB
  FileC
Dir1
  FileD
  DoorF

Assuming the above names and structure, what exactly are you trying to do?
Please note that
Folder and Dir names are for the various folders.
File and Door names are the respective filenames.

So, for instance, are you trying to find all filenames that begin "File" in any directory or only those named "Folder"?

If I have this all wrong, please clarify with examples.

Yes, exactly...! I want to search in Folder and count if there are any files with files with filename like FILE* :slight_smile:

Could you do

find . -name "*.* | grep "File"

or

find .Folder -name "*.* | grep "File"
1 Like

go to that folder and try this command

ls | grep 'XX_IN*'

Thanks,
Pragyan

1 Like
ls | grep "^SOME"    # start with ...
# =
ls SOME*
# =
echo SOME*
# =
find . -depth 1 -name "SOME*"
1 Like

Thanks a lot all of you.

This solution finally worked for me...... Here I find and count the number of files matching the start string....!!

ls -l |grep 'XX_GT'| wc -l

Cheers :slight_smile:

Now I find the no. of files and display the count. How can I display the maching filenames in display?? :S

---------- Post updated at 02:21 PM ---------- Previous update was at 01:40 PM ----------

Can someone pls help me printing the filenames that match the search criteria?

you want to display the file names along with the count,or you only want the matching file names

if you only want the matching file names then this is enough

ls | grep 'XX_ID*'

Thanks. Yes this was what i was trying to do. Now i have done picking up the filenames and renaming all of them in a loop.

i=0
for file in `ls |grep "$filename"`
do 
echo "$file"
echo $i
i=`expr $i + 1`
mv /var/tmp/akk2/${file} /var/tmp/akk2/${file}_${i}
done

Folks

I have a very quick question. Is there any way that I can list out all the common files with the directory name. Say for example

/home has test1 test2 test3 directories
where test1 has the files named "ABC" "DEF"
test2 has the files named "EFG" "ABC"
test3 has the files named " BC"

For this i have a common file ABC which is present in test1 and test2 directories. Instead of searching each and every directory, i need to get those 2 files to be displayed with the directory name at the root directory itself. Can someone help me out in this regard.

Any help wud be greatly appreciated. Thanks