Analysis of the problem of deleting files in LS and RM in Linux

Source: Internet
Author: User
Tags current time


Normally the LS-listed files that want to be directly piped through the RM-RF delete are not valid. At this point, we need to cooperate with the command Xargs use:

For example:
Ls-t|tail-10|xargs RM-RF//sorted by time, delete the last 10 files

Of course, you can also use the Ls-lt|tail-100|awk ' {print $} ' |xargs RM-RF the effect is the same as the same, if you want to arrange in reverse chronological order, that is, the closest to the current time in the last show, with Ls-rt of course, you can also use Find with RM deletion.

You can also empty the content with the following statement

#!/bin/bash
For i in ' ls test.log.?? ';d o
echo "" > $i
Done

Of course, if it is under more than one directory, then clear the file, it is also OK. Just one more layer of nesting and memory. My directory structure is as follows:

[Root@web tomcat]# LL
Total 260
Drwxr-xr-x 2 www www 20480 12-28 09:23 bbs
Drwxr-xr-x 2 www. www. 16384 12-28 00:00 Comment
Drwxr-xr-x 2 www. www. 36864 12-28 03:50 Enterprise
Drwxr-xr-x 2 www. www. 24576 12-28 00:00 Expert
Drwxr-xr-x 2 www. www. 36864 12-28 00:02 Feedback
Drwxr-xr-x 2 www. www. 36864 11-15 12:30 Generator
Drwxr-xr-x 2 www. www. 24576 12-28 00:02 Passport
Drwxr-xr-x 2 www. www. 20480 12-28 00:00 Search
Drwxr-xr-x 2 www. www. 20480 12-28 09:35 Usercenter
[Root@web comment]# LL
Total 936
-rw-rw-r--1 www. 2 12-28 09:57 Catalina.2012-12-24.log
-rw-rw-r--1 www. 2 12-28 09:57 Catalina.2012-12-25.log
-rw-rw-r--1 www. 2 12-28 09:57 Catalina.2012-12-26.log
-rw-rw-r--1 www. 2 12-28 09:57 Catalina.2012-12-27.log
-rw-r--r--1 www. www. 34155 12-28 10:01 catalina.out
-rw-rw-r--1 www. 2 12-28 09:57 Localhost.2012-12-14.log
-rw-rw-r--1 www. 2 12-28 09:57 Localhost.2012-12-17.log
-rw-rw-r--1 www. 2 12-28 09:57 Localhost_access_log.2012-12-25.txt
-rw-rw-r--1 www. 2 12-28 09:57 Localhost_access_log.2012-12-26.txt
-rw-rw-r--1 www. www. 629729 12-28 09:57 Localhost_access_log.2012-12-27.txt
If you want to clear all the files in all of the above directories, you can do this:

For i in ' ls ';d o (CD $i, for M in ' ls ';d o echo "" > $m;d one);d one

Note: The parentheses above are not small.

And if you want to clear the contents of a file, and then use Xargs with echo "" >file, found that the normal effective. Such as:

Find. -name "test.*" |xargs echo "" >

Because, the find and echo are not used in this way. The simple combination of the two has another magical thing:

Find. -name "file*"-print | Xargs echo "" >/tmp/find.log

The purpose of this statement is to find all the files that begin with file in the current directory and enter their relative paths and names into the Find.log file, in the form of one per line. No changes will be made to the original document that meets the criteria.

And if you want to realize find and empty files, can't we? Obviously, this is impossible. The rookie's approach is:

#!/bin/bash
For I in ' Find./server*-name ' Test.log '
Todo
Cat/dev/null > $i
Done
Master obviously disdain to use such a long sentence, the experts are the way:

[Root@localhost log]# Find. -name "maillog*" |awk ' {print ' echo> ' $} ' |bash
Or get rid of ECHO.
[Root@localhost log]# Find. -name "maillog*" |awk ' {print ' > ' $} ' |bash

How does the statement change to come?

[Root@localhost log]# Find. -name "maillog*" |xargs-i ls-l {}
-RW-------1 root 0 11-09 05:06./maillog
-RW-------1 root 0 11-09 05:06./maillog.1

To find all the files, and then use the powerful awk, you can add echo "" > to bash Statements in front of all output files

[Root@localhost log]# Find. -name "maillog*" |awk ' {print ' echo > ' $} '
echo >/maillog
echo >/maillog.1

Find command in conjunction with RM to delete files from a day ago

Example 1: The/usr/local/backups directory is all 10 days ago with "." The file deletion

Find/usr/local/backups-mtime +10-name "*.*"-exec rm-rf {}\;

Find:linux Lookup command, the user finds the file for the specified condition

/usr/local/backups: Any directory that you want to clean

-mtime: The standard sentence formulation

+10: Find 10 days before the file, where the number of days to represent, +30 to find 30 days before the file

"*.*": the type of data you want to find, "*.jpg" means to find all the files with the extension jpg, "*" means to find all the files, this is flexible to use, extrapolate

-exec: Fixed wording

RM-RF: Force delete files, including directories

{} \; : fixed wording, a pair of curly braces + space +\

Find $1-name "*.html"-mtime +1-print0 |xargs-0 rm-v

Note: my example above is just a list of directories, and the Find lookup is also included in the statement that matches the criteria within the subdirectory below it.

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.