自动创建目录脚本如下:
#!/bin/bash
#获取当前年代
currentyear=$(date +%Y);#获取当前日期
currentmonthdate=$(date +%Y%m%d);
echo "current year:$currentyear";
echo "current date:$currentmonthdate";
#判断目录是否存在
if [ -d "/root/listenlogs/$currentyear/$currentmonthdate/" ];then
echo "the dir[/root/listenlogs/$currentyear/$currentmonthdate/ ] is exit"
else#创建目录
mkdir -p /root/listenlogs/$currentyear/$currentmonthdate/
fi
最近发现了一段更简洁的语句 ,记录如下:
#判断路径是否存在,不存在则创建目录
[ -d "/root/listenlogs/$currentyear/$currentmonthdate/" ]||mkdir -p "/root/listenlogs/$currentyear/$currentmonthdate/";
其中,mkdir的参数p,意思是递归创建目录,即每级目录均进行创建,如果已存在也不报错。