`
joyocaowei
  • 浏览: 32630 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

A Simple linux shell

阅读更多

目录结构是:mydir下有同级的三个目录:archive log data

这个shell是在特定的时间运行的,主要的功能是将log目录下的文件移到archive目录下,文件名的要求是在原来的文件名加上时间戳(年月日,如20111122)。

另一个功能是删除data目录下21天前的文件,data目录下可能有多级目录,所以需要递归删除。

#!/bin/sh
# If the directory is not exist, create it first.


namefordir="/mydir/archive"
if [ ! -d $namefordir ]; then
    mkdir $namefordir
fi

# Move file to archive directory and retain last modified time
cd log
# Find files that modified in one day
# find . -type f  -mtime -1


FILE_DATE=`date +%Y%m%d`
for file in `ls`
do
   
# Get the last modified format time, like as: 201101100900.01
    filetime=`ls --time-style=+%Y%m%d%H%M.%S  -l "$file"|awk '{print $6}'`
   
   
# New file name
    newfilename="$namefordir/$file.$FILE_DATE"
    mv $file $newfilename

    touch -m -t $filetime $newfilename
done


# Judge which file can be deleted, here remove the file older than 21 days
delFileByTime(){
    echo "run delFileByTime() $1"
    file_time=`stat -c%Y "$1"`
    local_time=`date +%s`
    n=$(( $local_time - $file_time ))
    if [ "$n" -ge "1814400" ]; then
        rm "$1"
        echo "delete successful $1"
    else
        echo "The file < $1 > needn't to delete"
    fi
}

# Process to recursively delete all files older than a given date
delFiles(){
echo "run delFiles()"
for dirfile in `ls "$1"`
do
    if [ -d $1/$dirfile ]; then
        echo "fun-this dir is $dirfile"
        delFiles $1/$dirfile
    else
        echo "fun-this is a file $dirfile"
        delFileByTime $1/$dirfile
    fi
done
}


# Remove all data files older than 21 days from data directory
cd ../data
for deletefile in `ls`
do
    if [ -d $deletefile ]; then
        echo "main-this dir is $deletefile"
        delFiles $deletefile
    else
        echo "main-this file is $deletefile"
        delFileByTime $deletefile
    fi
done

分享到:
评论

相关推荐

    Linux Shell Scripting Cookbook.epub

    Proper usage of shell commands can easily solve many complex tasks with a few lines of code, but most linux users don't have the right know-how to use the Linux shell to its full potential. ...

    Linux Shell Scripting Cookbook.mobi

    Proper usage of shell commands can easily solve many complex tasks with a few lines of code, but most linux users don't have the right know-how to use the Linux shell to its full potential. ...

    Mastering Linux Shell Scripting

    Shell scripting is a quick method to prototype a complex application or a problem by automating tasks when working on Linux-based systems. Using both simple one-line commands and command sequences ...

    Mastering Linux Shell Scripting.epub

    Shell scripting is a quick method to prototype a complex application or a problem by automating tasks when working on Linux-based systems. Using both simple one-line commands and command sequences ...

    Linux Shell Scripting Cookbook - Third Edition

    Linux Shell Scripting Cookbook - Third Edition by Clif Flynt English | 29 May 2017 | ASIN: B01N80F75Z | 552 Pages | AZW3 | 1.36 MB Do amazing things with the shell About This Book Become an expert ...

    Mastering Linux Shell Scripting.mobi

    Shell scripting is a quick method to prototype a complex application or a problem by automating tasks when working on Linux-based systems. Using both simple one-line commands and command sequences ...

    Mastering Linux Shell Scripting 2nd Edition

    Then, you'll learn how to write a simple bash script and how to edit your bash script using Linux editors. Following this, you will learn how to define a variable and the visibility of a variable. ...

    Mastering Linux Shell Scripting(PACKT,2015)

    Shell scripting is a quick method to prototype a complex application or a problem by automating tasks when working on Linux-based systems. Using both simple one-line commands and command sequences ...

    hdu-OS-simple-shell,Linux_的_Shell_命令窗口_demo_版实现_shell-demo.zip

    hdu-OS-simple-shell,Linux_的_Shell_命令窗口_demo_版实现_shell-demo

    SimpleShell.c

    C语言实现的简单命令行解析器(Simple shell) 支持用户输入命令行并在后台运行即&后台命令运行模式 支持history命令功能,用户可以查看最近使用的10条命令行,并且调用他们。

    Linux Shell 编程

    例如`# This is a very simple example`这一行就说明了这是一个简单的示例程序。 - **echo命令**:`echo`是一个常用的命令,用于输出文本到标准输出。例如`echo "Hello World"`会打印出`Hello World`到终端。值得...

    Linux Shell Scripting Essentials(PACKT,2015)

    Using simple commands or a combination of them in a shell can solve complex problems easily. This book starts with the basics, including essential commands that can be executed on Linux systems to ...

    linux shell

    【Linux Shell 快速入门】Linux Shell,全称为Bash(Bourne-Again SHell),是Unix和Linux操作系统中最常用的命令行解释器。Bash不仅用于接收用户在终端中的命令,还支持脚本编写,使得自动化任务处理变得简单高效。...

    Mastering Linux Shell Scripting.pdf

    Shell scripting is a quick method to prototype a complex application or a problem by automating tasks when working on Linux-based systems. Using both simple one-line commands and command sequences ...

    simple-shell.c

    The next task is to modify the shell interface program so that it provides a history feature that allows the user to access the most recently entered commands. The user will be able to access up to 10...

    Linux Shell Scripting Cookbook

    Solve real-world shell scripting problems with over 110 simple but incredibly effective recipes Master the art of crafting one-liner command sequence to perform tasks such as text processing, digging...

    linux bash shell中文手册

    Linux Bash Shell 中文手册是为Linux用户和系统管理员提供的一份详细指南,涵盖了Bash Shell的基本用法和高级特性。Bash(Bourne-Again SHell)是GNU项目下的Unix/Linux操作系统默认的命令行解释器,它继承并扩展了...

Global site tag (gtag.js) - Google Analytics