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
# 8  
Old 07-17-2012
Quote:
Originally Posted by scorpioraghu
Code:
for filename in ./*
 do	

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

I realize you have already solved your problem, but I will share a different solution that may be of use at some point:
Code:
for filename in ./*; do
    [ ! -f "$filename" ] && continue 
    file=${filename##*/}
    case $file in
        nexus*) printf "%s\n" "$file" >> test.log;;
    esac
done

Instead of wrapping the body of the for-loop within the file test, I test the inverse. If it is not a file, skip the rest of the loop's statements. This keeps the code from being needlessly idented (not a big deal here, but it can be with a more complex script).

The case statement is portable and can be easily extended to handle different actions for different patterns (or multiple patterns for the same case).

You probably want to use ${filename##*/} instead of ${filename#*/}. They both give the same result when there's only one slash in the pathname, but, when there are multiple, the former will return the basename of the file while the latter will only strip the first directory component.

Personally, I don't use echo when handling indeterminate data (like filenames, user input, etc), because problems can arise if the data looks like an option (and -- to disable option processing isn't supported).

Regards,
Alister
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