代码之家  ›  专栏  ›  技术社区  ›  hidemyname

Bash:如何根据日志文件中的变量将文件从一个目录移动到另一个目录?

  •  -3
  • hidemyname  · 技术社区  · 11 年前

    我有一个日志文件,如:

    /IE_copora/1987/07/14/0056700.xml.json 2
    /IE_copora/2006/11/01/1801433.xml.json 3
    

    日志文件中的第一列是文件路径列表,第二列只是一个数字。 我想从旧路径移动日志文件中记录的文件
    /IE_copora/1987/07/14/0056700.xml.json
    到新的路径,如:
    /new_directory/1987/07/14/0056700.xml.json
    基于日志文件中的第二列号(第二列编号小于10的文件路径)。

    1 回复  |  直到 11 年前
        1
  •  1
  •   AKS    11 年前

    首先,您必须逐个读取日志文件中的行。 第二步,在两个变量中获取行中的第一个和第二个字段。 第三,使用BASH/Shell中的case/esac语句来处理要移动的新位置。

    #!/bin/bash
    
    while read line
    do
       #Get first column/field - path
       from="$(echo $line | cut -d' ' -f1)"
       firstfolder="$(echo $from | cut -d'/' -f1)" 
    
       #Get Second column - number
       number="$(echo $line | cut -d' ' -f2)"
    
       case $num in
             1) 
               #When number is 1. I'll do this. This assumes you aleady
               #where $firstfolder exists.
               mv $firstfolder  new_directory1
    
             2)  
               #When number is 2. I'll do that. This assumes you aleady
               #where $firstfolder exists.
               mv $firstfolder  new_directory2
    
             3) 
               #When number is 3. I'll do this/that. This assumes you aleady
               #where $firstfolder exists.
               mv $firstfolder  new_directory3
    
             #4) ..
             #5) ..
             #..) ..
             #9) ..
    
             *) 
               #This is when number is not between 1 - 9.
               echo "-- oh oh, I got you a dollar !!! - File: $first"
        esac
    done < someTextLogFile.txt
    

    欲了解更多信息,请在线学习BASH。

    如何使用case语句的一个好例子是: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_03.html

    推荐文章