我需要找到在UNIX系统中处理文件的文件扩展名。我将要处理的两种文件扩展名是'.dat'和'.csv'。
请告诉我如何完成这个任务。
find . -name "*.dat" -o -name "*.csv"
递归地搜索当前目录及其子目录中,所有以这两个扩展名结尾的文件。
find . -name "*.dat"
find . -name "*.csv"
filename=file.dat
extension=$(echo ${filename}|awk -F\. '{print $2}')
if [ ${extension} == "dat" ]; then
your code here
fi
将变量${filename}的内容打印并通过管道传递给awk命令。在awk命令中重置字段分隔符为".",然后提取第2个字段(即print $2部分)。
awk
,当find
的设计就是用来解决这个确切的问题的? - jacobsimeonfilename = test.csv
echo ${filename##*.}
适用于bash,可以在ksh中尝试。
编辑:
filename=test.csv
fileext=${filename##*.}
if [ fileext = "csv" ]; then
echo "file is csv, do something"
else
if [ fileext = "dat" ]; then
echo "file is dat, do something"
else
echo "mhh what now?"
fi
fi
find /path -type f \( -name "*.dat" -o -name "*.csv" \) | while read -r file
do
echo "Do something with $file"
done
find . -name test.dat
只会查找名为test.dat
的文件。 - jacobsimeon