2
$ script.sh -i /storage/testFile

-i stores the path of /storage/testFile/.

I want to iterate over file in testFile. Everything goes good, but I want to make this code cleaner and add a directory for output and other checks.

A snippet of my code is:

for f in $inDir/*.vcf; do
    if [ -f $f ]; #check if file is true or exists
    then
    if [   ${f: -4} == ".vcf" ] # check if the file ends with .vcf
        then
        convert2annovar.pl -format vcf4 "$f" > ./"$f".avinput  #run my code
     fi
    fi
done

Issue:

echo $f returns /storage/testFiles/test.vcf. How do I get only test.vcf while I iterate in for loop?

slm
  • 363,520
  • 117
  • 767
  • 871
Death Metal
  • 269
  • 6
  • 15

1 Answers1

3

You need the to use basename:

Synopsis

basename NAME [SUFFIX]    
basename OPTION 

Description

Print NAME with any leading directory components removed. If
specified, also remove a trailing SUFFIX.

Therefore:

$ basename /storage/testFiles/test.vcf
test.vcf

Also, if you need the opposite, you can use dirname:

$ dirname /storage/testFiles/test.vcf
/storage/testFiles
garethTheRed
  • 33,289
  • 4
  • 92
  • 101