パスの代わりにディレクトリを繰り返すときにファイル名のみを取得する

パスの代わりにディレクトリを繰り返すときにファイル名のみを取得する
$ script.sh -i /storage/testFile

-iストレージパスです/storage/testFile/

testFileすべてがうまくいっていますが、このコードをより明確にし、出力やその他のチェック用のディレクトリを追加したいと思います。

私のコードスニペットは次のとおりです

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

質問:

echo $f返品/storage/testFiles/test.vcftest.vcfループが繰り返されるときにのみ取得する方法は何ですかfor

ベストアンサー1

以下を使用する必要がありますbasename

Synopsis

basename NAME [SUFFIX]    
basename OPTION 

Description

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

だから:

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

または、逆が必要な場合は、次のものを使用できますdirname

$ dirname /storage/testFiles/test.vcf
/storage/testFiles

おすすめ記事