linux 常用命令
文件压缩命令
1、gzip(不可压缩目录。压缩后格式:.gz)
使用“gzip”对文件进行压缩。使用“gunzip”或“gzip -d”进行解压缩。但是gzip只可对文件进行压缩。对于目录不可以使用。

使用“gzip”压缩后不保留原文件。
2、tar(打包目录,打包后格式:.tar。打包后可压缩,格式:.tar.gz)
-c:打包
-v:显示详细信息
-f:指定文件名
-z:打包同时压缩
下图为对test目录的打包:

打包后可以进行使用“gzip”进行压缩:

也可使用“tar -zcvf [压缩后文件名] [需压缩文件]"直接进行打包压缩:

对于解压缩将压缩选项”-c“换成”-x“表示”解包“。

3、zip(压缩后格式:.zip)
".zip"格式的压缩包在Windows和linux都可使用。zip [压缩后文件名] [需压缩文件名] ,使用-r选项可压缩目录。
解压缩使用 unzip [文件名]
4、bzip2(压缩后格式:.bz2)
bzip2 [文件名] 即可进行压缩。通过bzip -k [文件名]的方式可以压缩同时保留原文件。该方式压缩比可观,大文件通常使用该方法。
也可根据打包压缩的方式可以生成”.tar.bz2"的文件,使用 tar -cjf [压缩后文件名] [需压缩文件]

解压缩:
1、bunzip2:直接解压,不保留压缩包。可以通过”-k"
解压的同时保留压缩包。
2、tar -xjf [文件名]
shell脚本关于日期的操作
一、计算指定日期的前一天的日期 date -d "yesterday 20150401 " +%Y%m%d 二、如果获取当前日期的前一天 date -d "yesterday" +%Y%m%d 三、计算指定日期的前几天(例如计算20190716的前10天),如果是负数的话,则会往前数 date -d "10 day ago 20190716" +%Y%m%d 或 date -d "10 day ago 2019-07-16" +%Y%m%d 四、计算当前日期的前几天(例如计算当前日期的前10天),如果是负数的话,则会往前数 date -d "10 day ago" +%Y%m%d 五、获取指定小时点之后的时间 date -d "20190716 12 3 hour" +"%Y%m%d%H" # 表示2019年07月16日12点 向后移动三个小时的时间,运行结果为:2019071615 六、获取当前时间点之后的几小时的时间点 date -d "3 hour" +"%Y%m%d%H" # 表示获取当前时间点3小时之后的时间,结果精确到小时 七、获取指定时间点之后几分钟 date -d "20190716 21:15 10 minute" +"%Y%m%d%H%M" # 表示2019年07月16日21时15分在10分钟的时间点,精确到分 八、获取当前时间点之后的几分钟 date -d "10 minute" +"%Y%m%d%H%M" 九、获取本月 date +"%Y%m" 十、获取下月 date -d "1month" +"%Y%m" 十一、获取上月 date -d "-1month" +"%Y%m" 十二、获取昨天 date -d yesterday # 这样是未经过任何的格式化,比较难看 date -d yesterday +"%Y%m%d" # 这样是经过格式化的 十三、获取明天 date +%Y%m%d -d "+1 day" date +"%Y%m%d" -d '+1 day' # 这里引号不是必须的 date +%Y%m%d --date "+1 day" date +"%Y%m%d" --date '+1 day' 十四、指定月份 date -d 1May 十五、现在 date -d now 或 date 十六、获取当前时间的日期 1、date +"%Y-%m-%d" 2、date +"%F" 十七、获取当前的时间的各指标 1、date +%H #小时 2、date +%M #分钟 3、date +%S #秒 4、date +%T #时间 5、date +%w #星期 6、date -d "-1 day" +%F # 前一天
shell if语句
逻辑运算符解析:
-f 判断文件是否存在 eg: if [ -f filename ]
-d 判断目录是否存在 eg: if [ -d dir ]
-eq 等于 应用于:整型比较
-ne 不等于 应用于:整型比较
-lt 小于 应用于:整型比较
-gt 大于 应用于:整型比较
-le 小于或等于 应用于:整型比较
-ge 大于或等于 应用于:整型比较
-a 双方都成立(and) 逻辑表达式 –a 逻辑表达式
-o 单方成立(or) 逻辑表达式 –o 逻辑表达式
-z 空字符串
#!/bin/bash TODAY=`date +%Y%m%d` YESTERDAY=`date -d'-1 day' +%Y%m%d` LAST_MONTH=`date -d'-1 month' +%Y%m%d` week_start=`date -d 20200406 +%w` week_end=`date -d 20200412 +%w` echo $TODAY 、$YESTERDAY、 $LAST_MONTH、$week_start if [ $week_start -eq 1 ];then echo "今天是周的开始日期,也就是周一" elif [ $week_end -eq 0 ];then echo "今天是周的结束日期,也就是周日" else echo "日期是周一到周六的时间" fi
基于输入日期参数计算 周信息的相关数据
---- 日期计算逻辑 #! /bin/sh if [ $# -eq 1 ]; then day=$1 if [ `date -d $day +%w` -eq 1 ]; then #如果今天是星期一 start_time=`date -d "7 day ago $day" +%Y-%m-%d` #基于输入日期参数 减去7天 end_time=`date -d "1 day ago $day" +%Y-%m-%d` #基于输入日期参数 减去1天 elif [ `date -d $day +%w` -eq 0 ]; then #如果今天是星期日 start_time=`date -d "6 day ago $day" +%Y-%m-%d` ##基于输入日期参数 减去6天 end_time=$day else day_num=$((`date -d $day +%w`-1)) #数字减去1 start_time=`date -d "$day_num day ago $day" +%Y-%m-%d` #基于输入日期参数 减去day_num(变量)天数 end_time=$day fi else day=`date -d '1 day ago' +%Y-%m-%d` if [ `date -d $day +%w` -eq 1 ]; then start_time=`date -d "7 day ago $day" +%Y-%m-%d` end_time=`date -d "1 day ago $day" +%Y-%m-%d` elif [ `date -d $day +%w` -eq 0 ]; then start_time=`date -d "6 day ago $day" +%Y-%m-%d` end_time=$day else day_num=$((`date -d $day +%w`-1)) start_time=`date -d "$day_num day ago $day" +%Y-%m-%d` end_time=$day fi fi echo $day、$day_num、$start_time、$end_time
#! /bin/sh if [ $# -eq 1 ]; then day=$1 else day=`date -d '1 day ago' +%Y%m%d` fi day1=`date -d"$day -1 day ago" +%Y%m%d` echo $day echo $day1 beeline -u 'jdbc:hive2://node03-bigdata-prod-bj1.ybm100.top:10000/;principal=hive/node03-bigdata-prod-bj1.ybm100.top@YBM100.COM' -e" set hive.exec.dynamic.partition=true; set hive.exec.parallel=true; set hive.exec.dynamic.partition.mode=nonstrict; set hive.exec.dynamic.partition=true; set hive.auto.convert.join = true; set hive.mapjoin.smalltable.filesize=200000000; set hive.exec.parallel=true; set hive.auto.convert.join = true; set hive.mapjoin.smalltable.filesize=25000000; set mapreduce.map.memory.mb=3072; set mapreduce.reduce.memory.mb=3072; set yarn.nodemanager.vmem-pmem-ratio=3; drop table xyy_app_data.sale2_bad_stock_provice; CREATE TABLE xyy_app_data.sale2_bad_stock_provice ( create_time string comment '日期', branch_code string comment '省份编码', province_name string comment '省份', sum_inventory double comment '动销不良和效期不良' ) comment '省区报表_动销不良和效期不良' PARTITIONED BY ( dt string) stored as parquet; insert overwrite table xyy_app_data.sale2_bad_stock_provice partition(dt='"$day"') select '"$day"' as create_time ,all_badness.branch_code --,all_badness.branch_name ,substr(all_badness.branch_name,1,2) as province_name ,sum(all_badness.sum_inventory) sum_inventory from (select badness_not_oem.branch_code,badness_not_oem.branch_name,badness_not_oem.type,sum(badness_not_oem.inventory) sum_inventory from (--动销不良 select not_sales.product_code,not_sales.branch_code,not_sales.branch_name,not_sales.type,storage_goods.inventory from xyy_temp_data.provice_table_not_sales not_sales LEFT JOIN ( SELECT * FROM xyy_bigdata_dim.dim_branch_ss WHERE dt = date_add(current_date(), -1)--当前日期减1天 AND branch_code NOT IN ('NULL', 'XS000000', 'XS777777', 'XS666666') ) org on not_sales.branch_code = org.branch_code join ( --不含OEM库存金额 SELECT ss1.org_code,ss1.product_code, SUM( ss1.storage_total_cnt * ss1.cost_price_tax) AS inventory FROM (select * from xyy_bigdata_dwd.dwd_b2b_wh_inventory_product_ss WHERE storage_type = '1' -- AND yn = '1' AND dt = date_add(current_date(), -1)--当前日期减1天 AND storage_amt_tax > 0 ) ss1 LEFT JOIN ( SELECT * FROM d274.oemlist_lz ) oem ON ss1.product_code = oem.product_code WHERE oem.oemtype IS NULL group by ss1.org_code,ss1.product_code ) storage_goods ON not_sales.product_code= storage_goods.product_code AND org.erp_org_code = storage_goods.org_code ) badness_not_oem group by badness_not_oem.branch_code,badness_not_oem.branch_name,badness_not_oem.type ) all_badness group by all_badness.branch_code,substr(all_badness.branch_name,1,2);"
#!/bin/bash startDate=20190730 endDate=20190720 startSec=`date -d "$startDate" "+%s"` endSec=`date -d "$endDate" "+%s"` for((i=$startSec;i>=$endSec;i-=86400)) do current_day=`date -d "@$i" "+%Y-%m-%d"` one_day_ago=`date -d "$current_day yesterday" +%Y-%m-%d` echo "current_day===========正在跑的数据日期是:${current_day}=================================:${current_day}, yesterday:${one_day_ago}" hive -e " insert overwrite table xyy_temp_data.sale_order_activation_mcid PARTITION (dt='${current_day}') select '${current_day}' statistics_time, m1.mc_Id from ( select a.mc_id from xyy_dw_data.bas_member_info a inner join ( select merchant_id from xyy_dw_data.fact_sale_order where substr(old_create_time,0,10) ='${current_day}' and merchant_id is not NULL group by merchant_id) b on a.mc_id=b.merchant_id where a.audit2_status='1' group by a.mc_id ) m1 inner join ( select a.mc_id from xyy_dw_data.bas_member_info a left join ( select merchant_id from xyy_dw_data.fact_sale_order where substr(old_create_time,0,10) >=date_sub(date_format(substr('${current_day}',0,10),'yyyy-MM-dd'),90) and substr(old_create_time,0,10) <=date_sub(date_format(substr('${current_day}',0,10),'yyyy-MM-dd'),1) and merchant_id is not NULL group by merchant_id ) b on a.mc_id=b.merchant_id where b.merchant_id is null group by a.mc_id ) m2 on m1.mc_id =m2.mc_id; insert overwrite table xyy_temp_data.sale_order_loss_2mcid PARTITION (dt='${current_day}') select '${current_day}' statistics_time, a.mc_id from xyy_dw_data.bas_member_info a left join ( select merchant_id from xyy_dw_data.fact_sale_order where substr(old_create_time,0,10) >=date_sub(date_format(substr('${current_day}',0,10),'yyyy-MM-dd'),30) and substr(old_create_time,0,10) <='${current_day}' and merchant_id is not NULL group by merchant_id ) b on a.mc_id=b.merchant_id where b.merchant_id is null and a.audit2_status='1' group by a.mc_id ; insert overwrite table xyy_temp_data.sale_order_loss_activation_mcid PARTITION (dt='${current_day}') select t1.statistics_time ,t1.mc_id ,case when t2.statistics_time is null and t2.mc_id is null then 0 else 1 end loss_type ,case when t3.statistics_time is null and t3.mc_id is null then 0 else 1 end action_type from ( select t1.statistics_time,t1.mc_id from ( select statistics_time,mc_id from xyy_temp_data.sale_order_loss_2mcid where dt='${current_day}' union all select statistics_time,mc_id from xyy_temp_data.sale_order_activation_mcid where dt='${current_day}' ) t1 group by t1.statistics_time,t1.mc_id ) t1 left join xyy_temp_data.sale_order_loss_2mcid t2 on t2.dt = '${current_day}' and t1.statistics_time=t2.statistics_time and t1.mc_id=t2.mc_id left join xyy_temp_data.sale_order_activation_mcid t3 on t3.dt = '${current_day}' and t1.statistics_time=t3.statistics_time and t1.mc_id=t3.mc_id ; " done

浙公网安备 33010602011771号