Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find file extension using UNIX?

Tags:

unix

ksh

I need to find file extension for file to be processed using UNIX. The two file extension which i will be handling are '.dat' and '.csv'.

Please let me know how this can be done.

like image 652
Arun Antony Avatar asked Feb 17 '11 14:02

Arun Antony


3 Answers

find . -name "*.dat" -o -name "*.csv"

Finds in the current directory and recursively down, all files that end in those two extensions.

like image 173
Alberto Zaccagni Avatar answered Sep 28 '22 12:09

Alberto Zaccagni


So my stab at this.

filename=file.dat
extension=$(echo ${filename}|awk -F\. '{print $2}')
if [ ${extension} == "dat" ]; then
   your code here
fi

Echo the variable ${filename} pipe that output to awk. With awk reset the field separator to a . then pick up field 2 (the print $2 part)

like image 39
Michael Ballent Avatar answered Sep 28 '22 10:09

Michael Ballent


This is what you want ?

find . -name "*.dat"
find . -name "*.csv"
like image 29
alexl Avatar answered Sep 28 '22 11:09

alexl