企業生產案例:批量建立目錄並移動帶日期檔案到相應目錄

fjzcau發表於2016-01-08
mysqldump目錄下有每天匯出的gz檔案,現在需要通過一個命令,批量建立帶日期格式的目錄,並將當天日期匯出的檔案mv到相應目錄中。
例如:20160108,檔案 
mysql_10.0.0.11_all_20160108_2100.sql.gz  mv 到目錄 20160108_mysqldump_10.0.0.11_3306

1、檔案如下
  1. [ftpuser01@linux-node1 mysqldump]$ ll
  2. total 30804
  3. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:20   20160108_mysqldump_10.0.0.11_3306
  4. -rw-r--r-- 1 ftpuser01 ftpgrp01 2158414 Jan 8 17:29 mysql_10.0.0.11_all_20151225_2100.sql.gz
  5. -rw-r--r-- 1 ftpuser01 ftpgrp01 2172565 Jan 8 17:29 mysql_10.0.0.11_all_20151226_2100.sql.gz
  6. -rw-r--r-- 1 ftpuser01 ftpgrp01 2186811 Jan 8 17:29 mysql_10.0.0.11_all_20151227_2100.sql.gz
  7. -rw-r--r-- 1 ftpuser01 ftpgrp01 2201111 Jan 8 17:29 mysql_10.0.0.11_all_20151228_2100.sql.gz
  8. -rw-r--r-- 1 ftpuser01 ftpgrp01 2215431 Jan 8 17:29 mysql_10.0.0.11_all_20151229_2100.sql.gz
  9. -rw-r--r-- 1 ftpuser01 ftpgrp01 2229360 Jan 8 17:29 mysql_10.0.0.11_all_20151230_2100.sql.gz
  10. -rw-r--r-- 1 ftpuser01 ftpgrp01 2243729 Jan 8 17:29 mysql_10.0.0.11_all_20151231_2100.sql.gz
  11. -rw-r--r-- 1 ftpuser01 ftpgrp01 2258170 Jan 8 17:29 mysql_10.0.0.11_all_20160101_2100.sql.gz
  12. -rw-r--r-- 1 ftpuser01 ftpgrp01 2272444 Jan 8 17:29 mysql_10.0.0.11_all_20160102_2100.sql.gz
  13. -rw-r--r-- 1 ftpuser01 ftpgrp01 2286784 Jan 8 17:29 mysql_10.0.0.11_all_20160103_2100.sql.gz
  14. -rw-r--r-- 1 ftpuser01 ftpgrp01 2300978 Jan 8 17:29 mysql_10.0.0.11_all_20160104_2100.sql.gz
  15. -rw-r--r-- 1 ftpuser01 ftpgrp01 2315247 Jan 8 17:29 mysql_10.0.0.11_all_20160105_2100.sql.gz
  16. -rw-r--r-- 1 ftpuser01 ftpgrp01 2329819 Jan 8 17:29 mysql_10.0.0.11_all_20160106_2100.sql.gz
  17. -rw-r--r-- 1 ftpuser01 ftpgrp01 2344475 Jan 8 17:30 mysql_10.0.0.11_all_20160107_2100.sql.gz
2、答案:

點選(此處)摺疊或開啟

  1. [ftpuser01@linux-node1 mysqldump]$ ls *.gz|tr ' ' '\n'|awk -F'_' '{print $4}'|sed -r 's/(.*)/mkdir \1_mysqldump_10.0.0.11_3306;mv mysql_*\1*.sql.gz \1_mysqldump_10.0.0.11_3306;/' | bash
3、結果如下:

點選(此處)摺疊或開啟

  1. [ftpuser01@linux-node1 mysqldump]$ ll
  2. total 60
  3. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20151225_mysqldump_10.0.0.11_3306
  4. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20151226_mysqldump_10.0.0.11_3306
  5. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20151227_mysqldump_10.0.0.11_3306
  6. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20151228_mysqldump_10.0.0.11_3306
  7. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20151229_mysqldump_10.0.0.11_3306
  8. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20151230_mysqldump_10.0.0.11_3306
  9. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20151231_mysqldump_10.0.0.11_3306
  10. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20160101_mysqldump_10.0.0.11_3306
  11. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20160102_mysqldump_10.0.0.11_3306
  12. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20160103_mysqldump_10.0.0.11_3306
  13. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20160104_mysqldump_10.0.0.11_3306
  14. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20160105_mysqldump_10.0.0.11_3306
  15. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20160106_mysqldump_10.0.0.11_3306
  16. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:46 20160107_mysqldump_10.0.0.11_3306
  17. drwxr-xr-x 2 ftpuser01 ftpgrp01 4096 Jan 8 17:20 20160108_mysqldump_10.0.0.11_3306
  18. [ftpuser01@linux-node1 mysqldump]$ tree .

  19. [ftpuser01@linux-node1 mysqldump]$ export LANG=
  20. [ftpuser01@linux-node1 mysqldump]$ tree .
  21. .
  22. |-- 20151225_mysqldump_10.0.0.11_3306
  23. | `-- mysql_10.0.0.11_all_20151225_2100.sql.gz
  24. |-- 20151226_mysqldump_10.0.0.11_3306
  25. | `-- mysql_10.0.0.11_all_20151226_2100.sql.gz
  26. |-- 20151227_mysqldump_10.0.0.11_3306
  27. | `-- mysql_10.0.0.11_all_20151227_2100.sql.gz
  28. |-- 20151228_mysqldump_10.0.0.11_3306
  29. | `-- mysql_10.0.0.11_all_20151228_2100.sql.gz
  30. |-- 20151229_mysqldump_10.0.0.11_3306
  31. | `-- mysql_10.0.0.11_all_20151229_2100.sql.gz
  32. |-- 20151230_mysqldump_10.0.0.11_3306
  33. | `-- mysql_10.0.0.11_all_20151230_2100.sql.gz
  34. |-- 20151231_mysqldump_10.0.0.11_3306
  35. | `-- mysql_10.0.0.11_all_20151231_2100.sql.gz
  36. |-- 20160101_mysqldump_10.0.0.11_3306
  37. | `-- mysql_10.0.0.11_all_20160101_2100.sql.gz
  38. |-- 20160102_mysqldump_10.0.0.11_3306
  39. | `-- mysql_10.0.0.11_all_20160102_2100.sql.gz
  40. |-- 20160103_mysqldump_10.0.0.11_3306
  41. | `-- mysql_10.0.0.11_all_20160103_2100.sql.gz
  42. |-- 20160104_mysqldump_10.0.0.11_3306
  43. | `-- mysql_10.0.0.11_all_20160104_2100.sql.gz
  44. |-- 20160105_mysqldump_10.0.0.11_3306
  45. | `-- mysql_10.0.0.11_all_20160105_2100.sql.gz
  46. |-- 20160106_mysqldump_10.0.0.11_3306
  47. | `-- mysql_10.0.0.11_all_20160106_2100.sql.gz
  48. |-- 20160107_mysqldump_10.0.0.11_3306
  49. | `-- mysql_10.0.0.11_all_20160107_2100.sql.gz
  50. `-- 20160108_mysqldump_10.0.0.11_3306
  51.     `-- mysqldump_10.0.0.11_3306_20160108.sql.gz

  52. 15 directories, 15 files
  53. [ftpuser01@linux-node1 mysqldump]$



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22661144/viewspace-1974005/,如需轉載,請註明出處,否則將追究法律責任。

相關文章