How to check if a filename in a directory starts with a certain string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to check if a filename in a directory starts with a certain string
# 1  
Old 07-16-2012
How to check if a filename in a directory starts with a certain string

Hello,

Trying to iterate over set of file in current directory and check if the file name in that folder matches certain string. This is what I have so far. Here I am checking if the file name starts with nexus, if so echo file name to log file.
Getting weird syntax errors. Any help is appreciated
Code:
for filename in ./*
 do	

   if [ -f "$filename" ];   
       then
	    file="${filename#*/}";
	    if [["$file"==*nexus]];
	    then               
          	 echo $file >> test.log     
  	    fi   
   fi   
 done;

Moderator's Comments:
Mod Comment code tags please

Last edited by jim mcnamara; 07-16-2012 at 06:24 PM..
# 2  
Old 07-16-2012
Although I haven't tested this, or your code, and because I don't know what shell you are using, and because it's too late in the evening to play "Let's guess the error message"
Code:
if [[ "$file" =~ ./nexus* ]];

This User Gave Thanks to Scott For This Post:
# 3  
Old 07-17-2012
Hi Scott,

It's failing with the following error. BTW, it's bash shell.

Code:
Command '"./test.sh"'
failed with return code 2 and error message
./test.sh: line 11: syntax error in conditional expression: unexpected token `;'
./test.sh: line 11: syntax error near `;'
./test.sh: line 11: `	    if [[ "$filename" =~ nexus*]];'.


Last edited by Scott; 07-17-2012 at 08:49 PM.. Reason: Guess the code tags
# 4  
Old 07-17-2012
Code:
for filename in ./*
 do

    if [ -f "$filename" ]
       then
            file="${filename#*/}"
            if [[ "$file" == "*nexus" ]]
            then
                 echo $file >> test.log
            fi
   fi
 done;

# 5  
Old 07-17-2012
Hi Prince of Persia,

That eliminated the syntax errors, but it's not matching the file name....
I have in the directory a file name like nexus-55511533

It's not able to match the filename....and therefore the echo is not working.
# 6  
Old 07-17-2012
That's because your logic/comparison is faulty.

Code:
if [[ "$file" == nexus* ]]


Last edited by elixir_sinari; 07-17-2012 at 11:51 AM..
This User Gave Thanks to elixir_sinari For This Post:
# 7  
Old 07-17-2012
Can you please explain what is wrong? Appreciate your help.

---------- Post updated at 11:33 AM ---------- Previous update was at 10:52 AM ----------

Thanks !! that worked....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check if a string starts with certain values and ends with numbers

This is very basic. Yet Iam struggling to get the right pattern for my check. Apologize in advance to ask a very lame question. I have to validate if a value of the variable starts with "efgh" and followed by 6 numbers. Var1="efgh234567" The condition Iam trying to achieve is similar to... (6 Replies)
Discussion started by: deepakwins
6 Replies

2. Shell Programming and Scripting

Need to write a shell script that starts one, then kills it, then starts another?

This is on a CentOS box, I have two scripts that need to run in order. I want to write a shell script that calls the first script, lets it run and then terminates it after a certain number of hours (that I specify of course), and then calls the second script (they can't run simultaneously) which... (3 Replies)
Discussion started by: btramer
3 Replies

3. Shell Programming and Scripting

Filename check in UNIX

Hello , I have to search for the file names which will either has ABC_DEF or NN in their filename Please note that both cannot appear in the same file name currently I am using ls -lrt /zthetl/SrcFiles/*ABC_DEF*.xls| head -1 | nawk '{print $9}' How to combine the NN in this code?... (4 Replies)
Discussion started by: Pratik4891
4 Replies

4. Shell Programming and Scripting

grep exact string from files and write to filename when string present in file

I am attempting to grep an exact string from a series of files within a directory and append that output to the filename when it is present in the file. I've been after this all day with no luck. Thanks for your help in advance :wall:. (4 Replies)
Discussion started by: JC_1
4 Replies

5. Shell Programming and Scripting

extract every filename containing certain string in a directory and do some commend per file

Hi, Here is my question: suppose I have files like 1990_8xdaily_atmos.nc 1991_8xdaily_atmos.nc 1992_8xdaily_atmos.nc 1993_8xdaily_atmos.nc 1990_daily_atmos.nc 1991_daily_atmos.nc 1992_daily_atmos.nc 1993_daily_atmos.nc 1990_month_atmos.nc 1991_month_atmos.nc 1992_month_atmos.nc... (1 Reply)
Discussion started by: 1988PF
1 Replies

6. Shell Programming and Scripting

check if a given string is a file or directory

hi i want to know how to do this if the given is /tmp/ and it is a valid directory then it will echo directory if the given is /tmp/file.txt and is a valid file then it will echo file.. thanks! (5 Replies)
Discussion started by: h0ujun
5 Replies

7. Shell Programming and Scripting

Check if string starts with $ symbol

How do I check that a string $AA22CC3 starts with the "$" symbol ? I have tried : checksum='$AAB3E45' echo $checksum case $checksum in $* ) echo success ; esac Thanks ... (4 Replies)
Discussion started by: cillmor
4 Replies

8. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

9. Shell Programming and Scripting

to check whether a directory or filename path is valid or not

the script on excution should take a directory path from useran a numric input and it should check indicate whether its write or not? if the cmmd sh<script-name>,dir/path.<500>" is greater than 500 in size should be copied to dir ,temp in pwd and display the mesage'files of 2000 bytes hav been... (4 Replies)
Discussion started by: arukr
4 Replies

10. UNIX for Dummies Questions & Answers

filename that starts with a space

I accidentally started a filename with a spce and I can not get rid of it. Any words of advice? (6 Replies)
Discussion started by: noobie_doo
6 Replies
Login or Register to Ask a Question