如何删除具有特定字符串的文件

时间:2014-06-14 15:22:40

标签: bash for-loop

我正在努力解决在处理之后删除文件的问题。我正在生成pcap文件,然后进行处理,最后在/home/marcin/workspace/bake/source/ns-3.19/RESULTS/TRAFFIC_LIGHTS/_udp_filtered/目录中我有如下文件:

  

1.pcap_udp_filtered.pcap

     

1_00000_19700101010009_udp_filtered.pcap

     

3.pcap_udp_filtered.pcap

     

3_00000_19700101010009_udp_filtered.pcap

然后我想在名称的开头删除包含$nodeID.pcap的所有文件。 它只适用于循环中的最后一个文件。在这种情况下,只删除3.pcap_udp_filtered.pcap。你有什么建议我应该把" rm"代码中的一行,使其适用于所有$nodeIDs

#!/bin/sh

#SETTING parameters to generate feature vectors

#set the directory of the initial .pcap files"
hdir=/home/marcin/workspace/bake/source/ns-3.19/RESULTS/TRAFFIC_LIGHTS/
files=/$hdir/*
udp_fil_dir=/$hdir/_udp_filtered/*
mac_sorted_dir=/$hdir/_udp_filtered/_MAC_sorted/*


#set <src> for source mac address (outcoming packets) or `<dst>` for destination source mac address (incoming packets)
ether_direction=src


#RENAMING the files

printf "...RENAMING the files to get proper nodeID...\n"

cd $hdir
ls aodv*|awk -F\. '{print;split($1,a,"-");c=a[2]+1;print c""FS""$2}'|xargs -n2 mv


for n in $files
    do

        export fspec2=./"$n" 
        fname2=`basename $fspec2`
        nodeID="${fname2%%.*}"  #nodeID is the variable that handles all the node numbers from directory 


        echo $nodeID

        mac_address="00:00:00:00:00:0$nodeID"
        echo $mac_address
        editcap -i 1 "$nodeID.pcap" "$nodeID"

#FILTERING only UDP packets from pcap file stored in a specific folder

        printf "...FILTERING only UDP packets from pcap files stored in a specific folder...\n"

        for f in $files
        do
            tshark -Y "udp&&!aodv" -r "$f" -w ""$f"_udp_filtered".pcap
            mkdir $hdir/_udp_filtered
            mv  ""$f"_udp_filtered".pcap $hdir/_udp_filtered
        find /home/marcin/workspace/bake/source/ns-3.19/RESULTS/TRAFFIC_LIGHTS/_udp_filtered/ -name "$nodeID.pcap*" -exec rm {} \;
        done

    done  

1 个答案:

答案 0 :(得分:0)

一般而言:

以下是您的代码的清理版本,至少可以帮助解决问题;我添加了标记为# !!的注释,我已对其进行了更改(较小的更改,例如针对健壮性的双引号变量引用,未明确标记)。

你说你确实需要内循环,所以我保留了它 - 我怀疑有重构的可能性,但我不了解发生了什么。

#!/bin/bash
# !! ^ Since your question is bash-tagged, use a *bash* shebang.

#SETTING parameters to generate feature vectors

#set the directory of the initial .pcap files"
hdir=/home/marcin/workspace/bake/source/ns-3.19/RESULTS/TRAFFIC_LIGHTS
# !! ^ terminal / removed, because you later synthesize paths explicitly with /
files=$hdir/*  # !! initial / removed, because $hdir is already an absolute path
udp_fil_dir=$hdir/_udp_filtered/* # !! ditto
mac_sorted_dir=$hdir/_udp_filtered/_MAC_sorted/* # !! ditto

#set <src> for source mac address (outcoming packets) or `<dst>` for destination source mac address (incoming packets)
ether_direction=src

#RENAMING the files

printf "...RENAMING the files to get proper nodeID...\n"

cd "$hdir"
ls aodv* | awk -F\. '{print;split($1,a,"-");c=a[2]+1;print c FS $2}' | xargs -n2 mv


for n in $files
do

  export fspec2=./"$n" 
  fname2=$(basename "$fspec2")
  nodeID="${fname2%%.*}"  #nodeID is the variable that handles all the node numbers from directory 

  echo "$nodeID"

  mac_address="00:00:00:00:00:0$nodeID"
  echo "$mac_address"

  editcap -i 1 "$nodeID.pcap" "$nodeID"

  printf "...FILTERING only UDP packets from pcap files stored in a specific folder...\n"

  for f in $files
  do
      tshark -Y "udp&&!aodv" -r "$f" -w "${f}_udp_filtered".pcap 
      # !! ^ use `${f}` rather than `"${f}"` (extra set of double quotes)
      # !!   to delineate variable names inside double-quoted strings.
      mkdir "$hdir/_udp_filtered"
      mv  "${f}_udp_filtered".pcap "$hdir/_udp_filtered"
      # !! `find` command removed here - there was never
      # !! a good reason for it to be *inside* the loop to begin with.
      # !! see below.
  done

  # !! Delete files now:
  # !! It looks like "$hdir/_udp_filtered" has no subdirectories, given that
  # !! you've just created it above, so we can simply use `rm`.
  # !! If you DO have subdirs., consider using `shopt -s globstar` 
  # !! and **/ (bash 4+).
  rm "$hdir/_udp_filtered/$nodeID.pcap"*

done