Shell script to delete the specified directory file based on field state

Source: Internet
Author: User
Tags mysql query python script

Today received the relevant departments reflect that the storage space is not enough. You need to delete some video files to make room. As soon as I took the job, I wrote a python script. Helpless, personal Python level is also an introduction. So you can only write another shell script to get the job done.

Disclaimer: The following operations are performed on the virtual machine, after all, the production environment can not be chaotic, so after testing OK, and then to execute script on the line.

Requirements: Remove the video below/data/video/sports/shi/

Ideas:

1. First the relevant department has to delete the directory, the field statuscode changed to 0, the default is 1

2. Query the path to delete according to the status of StatusCode

3. After you delete the video file, you need to change the statuscode status to 1  

All right, open the whole:

Present situation:

[[email protected] shi]# lltotal 0-rw-r--r--1 root root 0 May 4 09:38 hc2012051620.mp4-rw-r--r--1 root root 0 May 4 09 : hc2012051621.mp4-rw-r--r--1 root root 0 May 4 09:38 hc2012051622.mp4-rw-r--r--1 root root 0 May 4 09:38 hc20120516 23.mp4

[[email protected] shi]# mysql -uroot -proot -e  "use sne;select  contentid,playaddress,statuscode from del_video_file; " +-----------+---------------------------------------------------------------------+------------+|  contentid | playaddress                                         | statuscode |+-----------+------------------- --------------------------------------------------+------------|    128704 |  http://xxx:17533/gdtv/sports/shi/hc2012051620.mp4 |           0 | |     128705 | http://xxx:17533/gdtv/sports/shi/hc2012051621.mp4 |           0 | |     128706 | http://xxx:17553/gdtv/sports/shi/hc2012051622.mp4 |           0 | |     128707 | http://xxx:17553/gdtv/sports/shi/hc2012051623.mp4 |           0 |+-----------+----------------------------------- ----------------------------------+------------+

Scripting ideas:

1. Log in MySQL query statuscode to 0, and use awk to fetch the required path

#!/bin/bash#author:ley# declares a variable for the path because the path in SQL is not an absolute path datadir= '/data/video '

result= ' mysql-uroot-proot-e ' use Sne;select contentid,playaddress from del_video_file where statuscode=0; "| awk ' begin{fs= ' 17553/'} {print $} '

2. Use the For loop to delete the video file in turn

For i in $resultdo rm-rf $datadir/$i echo "Remove the file was OK" done

3. According to Statuscode=0 to find out the corresponding ContentID

Contentid= ' mysql-uroot-proot-e ' use Sne;select contentid,playaddress from del_video_file where statuscode=0; "| awk ' {print '} '

4. then update statusCode=1 with the For loop in turn

For ID in $ContentID does update= ' mysql-uroot-proot-e "use sne;update del_video_file set statuscode=1 where contentid= $id ;" ' If [$? = = 0] Then echo ' update was OK ' else echo ' update is error ' Fidone

5. Check that the status of StatusCode is already 1.

[Email protected] script]# mysql-uroot-proot-e "use Sne;select statusCode from Del_video_file;" +------------+|          StatusCode |+------------+|          1 | |          1 | |          1 | | 1 |+------------+

Well, that's probably the script. Some details, continue to improve in the future.

This article is from the "Liang Enyu-9527" blog, be sure to keep this source http://liangey.blog.51cto.com/9097868/1641878

Shell script to delete the specified directory file based on field state

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.