일정기간이 지난 자료를 삭제하는 쉘만들기

shiningstarz의 이미지

안녕하세요.
제목그대로 특정 디렉토리에 위치한 자료에 대하여 일정기간이 지나면 삭제하는 쉘을 만들고자 합니다.

자료의 위치는

/home/tornado/yyyymmdd/var1
/home/tornado/yyyymmdd/var2
/home/tornado/yyyymmdd/var3

이라고 하고,

yyyymmdd 디렉토리의 경우,
매일 자료를 날짜별로 정리하기위해 디렉토리를 생성한 것입니다.

만일 yyyymmdd의 날짜가 7일이상 경과하였을때
해당변수들을 삭제하려는 쉘을 구성하려고 한다면 어떻게 해야할까요.

도와주세요ㅜㅜ

cinsk의 이미지

shiningstarz의 이미지

에제가 많네요 좋은자료 감사드립니다.

==============================================================================
Man who has had a dream for a long time will finally get looked like the dream

blackridge의 이미지

date -d '7 days ago' +%Y%m%d

OSX 에서는 date -v-7d +%Y%m%d 처럼 하시면 되구요.

예를 들어 7일전 디렉토리를 지우려면 다음과 같이 하면 되죠...

for i in 1 2 3
do
rm -rf /home/tornado/`date -d '7 days ago' + %Y%m%d`/var$i
done

shiningstarz의 이미지

만약에 'date -d 를 실행하면
월같은 경우 문자로 나오는데, 숫자로된 디렉토리를 인식할수 있을까요?

==============================================================================
Man who has had a dream for a long time will finally get looked like the dream

ymir의 이미지

아래처럼 find 로 mtime 이 오래된 녀석들 찾아서 지워도 되고..

find /home/tornado -type d -mtime +7 | xargs /bin/rm -rf

아니면 date 로 날짜 변환해서 지워도 됩니다.

$ ls -l
drwxrwxr-x 2 ymir ymir 4096 Jan  4 11:38 20121230
drwxrwxr-x 2 ymir ymir 4096 Jan  4 11:38 20121231
drwxrwxr-x 2 ymir ymir 4096 Jan  4 11:07 20130101
drwxrwxr-x 2 ymir ymir 4096 Jan  4 11:07 20130102
drwxrwxr-x 2 ymir ymir 4096 Jan  4 11:07 20130103
drwxrwxr-x 2 ymir ymir 4096 Jan  4 11:07 20130104
-rwxrwxr-x 1 ymir ymir  269 Jan  4 11:40 dcls.sh
$ cat dcls.sh
#!/bin/bash
 
preserv_day=3   # preserve recent 3 days logs
 
let preserv='86400 * preserv_day'
today=`date -d "$(date +%Y%m%d)" +%s`
 
for dir in *
do
        [ ! -d $dir ] && continue;
        dday=`date -d "$dir" +%s`
        let ddiff='today - dday'
        [ $ddiff -gt $preserv ] && echo "/bin/rm -rf $dir"
done
 
exit 0;
 
$ ./dcls.sh
/bin/rm -rf 20121230
/bin/rm -rf 20121231

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

shiningstarz의 이미지

다양한 방법이 있네요 감사드립니다. 정말 도움이 많이 되었습니다 ^^

==============================================================================
Man who has had a dream for a long time will finally get looked like the dream

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.