当前位置: 技术问答>linux和unix
求shell 语句:保留最新的10个文件
来源: 互联网 发布时间:2016-10-23
本文导语: 我的文件夹下有20,30 个文件,想保留最新的10 个文件,怎么写法: 我的文件: data01 data02 .... data34 思想:1:可以ls 按时间排序,列出file 名, 2:删除最新10个以外的。 大家帮忙想想,可以定期实现监控...
我的文件夹下有20,30 个文件,想保留最新的10 个文件,怎么写法:
我的文件:
data01
data02
....
data34
思想:1:可以ls 按时间排序,列出file 名,
2:删除最新10个以外的。
大家帮忙想想,可以定期实现监控的。
我的文件:
data01
data02
....
data34
思想:1:可以ls 按时间排序,列出file 名,
2:删除最新10个以外的。
大家帮忙想想,可以定期实现监控的。
|
还是这个 4楼的方法比较简单,但应该将 '>=' 换成 '>' 才是准确的,
ls -lt | awk '{if(NR>11){print "rm "$9}}' | sh
|
#!/bin/bash
cd source
tmp=`ls -rt|tail -n 4 `
for i in $tmp
do
mv $i /dist
done
rm -rf source
cd source
tmp=`ls -rt|tail -n 4 `
for i in $tmp
do
mv $i /dist
done
rm -rf source
|
function Usage()
{
echo "Usage: corerotate [directory path] [numbuer]"
exit 1
}
if (( $# != 2 ))
then
Usage
fi
if [ -d $1 ]
then
dir=$1
else
Usage
fi
if [[ $2 =~ ^[0-9]+ ]]
then
num=$2
else
Usage
fi
cd $dir
/bin/ls --sort=time data* > tmp.txt
while read line
do
/bin/ls --sort=time data* | gawk 'NR > "'"$num"'" {system("/bin/rm -rf " $0)}'
done
{
echo "Usage: corerotate [directory path] [numbuer]"
exit 1
}
if (( $# != 2 ))
then
Usage
fi
if [ -d $1 ]
then
dir=$1
else
Usage
fi
if [[ $2 =~ ^[0-9]+ ]]
then
num=$2
else
Usage
fi
cd $dir
/bin/ls --sort=time data* > tmp.txt
while read line
do
/bin/ls --sort=time data* | gawk 'NR > "'"$num"'" {system("/bin/rm -rf " $0)}'
done