删除已归档的文件

时间:2015-09-06 05:06:35

标签: bash tar

我需要删除比参数中给出的文件更旧的文件,并将其添加到存档中并保存结构。

文件列表

oracle@dbi-702D-6x:~/test.d$ ls -l
total 20
-rw-rw-r-- 1 oracle oracle    0 Sep  6 07:06 aug
-rw-rw-r-- 1 oracle oracle    0 Feb 10  2015 feb
-rw-rw-r-- 1 oracle oracle    0 Sep  6 07:06 june
-rw-rw-r-- 1 oracle oracle    0 Mar  3  2015 march
-rw-rw-r-- 1 oracle oracle    0 Sep  6 07:06 may
drwxrwxr-x 3 oracle oracle 4096 Sep  6 08:00 rem-old-res
-rwxrw-r-- 1 oracle oracle  469 Sep  6 08:00 rem-old.sh
-rwxrw-r-- 1 oracle oracle  467 Sep  6 08:00 rem-old.sh~
-rw-rw-r-- 1 oracle oracle   85 Sep  6 08:00 roll-back.sh
drwxrwxr-x 2 oracle oracle 4096 Sep  6 07:35 some.d

oracle@dbi-702D-6x:~/test.d/some.d$ ls -l
total 0
-rw-rw-r-- 1 oracle oracle 0 Sep  6 07:06 test1
-rw-rw-r-- 1 oracle oracle 0 Sep  6 07:06 test2
-rw-rw-r-- 1 oracle oracle 0 Jan  4  2015 test3

run-on.sh

#!/bin/bash

dirname=$2
date=$1
now=`date +%Y-%m-%d`
here=`pwd`

mkdir $HOME/rem-old-res
touch temp-file -d $date 

cp * -R $HOME/rem-old-res
cp -R $HOME/rem-old-res/ $here/rem-old-res

for file in `find rem-old-res -type f -newer temp-file`
do
    rm $file
done

tar czf rem-old-res.tar.gz rem-old-res 

echo "#!/bin/bash" > $here/roll-back.sh
echo "tar xvzf $HOME/rem-old-res.tar.gz $here/rem-old-res" >> $here/roll-back.sh


rm -rf $HOME/rem-old-res
rm $here/temp-file

脚本被称为:

./run-on.sh 20150501

2月,脚本必须删除march和test3文件,但它们不是。我无法理解循环无法正常工作的原因。

2 个答案:

答案 0 :(得分:1)

我认为你的方式非常好,但你没有阅读这些手册页。

tar有一个参数可以从磁盘中删除添加到存档中的文件。

touch使用-t参数设置文件时间。

此外,您应该注意在bash中使用变量,请参阅pitfalls始终使用引号

#!/bin/bash

dirname=$2
date=$1
now=`date +%Y%m%d%H%M`
here=`pwd`

touch -t "$date" temp-file
find "$here" -newer temp-file tarfiles 
tar -czf rem-old-res.tar.gz -T tarfiles --remove-files

echo "#!/bin/bash" > $here/roll-back.sh
echo "tar xvzf $HOME/rem-old-res.tar.gz $here/rem-old-res" >> $here/roll-back.sh

rm $here/temp-file
rm $here/tarfiles

答案 1 :(得分:0)

以下是我的问题的解决方案:

//Step1 : get the players array
 FollowModel.findOne({facebookId: req.user.facebookId}, function(err, result) {

//now for each id in the players array call a function which will make another mongoose call and get details
    async.map(result.players, gatherPlayerDetails, function(err, results) {
      console.log(results);
      res.send(results);
    });

  });