Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How shell traverses directories to deal with specific directories

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "shell how to traverse the directory to deal with a specific directory", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "shell how to traverse the directory to deal with a specific directory" this article.

Requirements: delete files, directory structure is: ip/ year / month, everyone has a directory that now needs to delete all files before 2012, and delete specific months after 2012.

I originally wanted to use find, but I found that these files time (a _ dint _ m _ c) records were no longer generated at the beginning.

Have no choice but to think of a local way to traverse, who has a good way, welcome to let me know.

The code is as follows:

#! / bin/bash

Cd / home/wanggy/rmfile/

Ls-l | grep ^ d | awk'{print $NF}'> / home/wanggy/tmp

Cat / home/wanggy/tmp | while read dir1

Do

Cd $dir1

Echo "first level directory $dir1"

Ls-l | grep ^ d | awk'{print $NF}'> / home/wanggy/tmp1

Cat / home/wanggy/tmp1 | while read dir2

Do

Echo "second level directory $dir2"

Cd $dir2

# rejudge the directories that are greater than 2012, and delete them directly if they are less than 2012.

If [$dir2-gt 2011]; then

Ls-l | grep ^ d | awk'{print $NF}'> / home/wanggy/tmp2

Cat / home/wanggy/tmp2 | while read dir3

Do

# deletions before September "

If [$dir3-lt 09]; then

Rm-rf $dir3

Fi

Done

Else

Echo "Delete directories before 2012: $dir2"

Rm-rf $dir2

Fi

# return to the previous level

Cd..

Done

# return to the previous level

Cd..

Done

The above is all the content of the article "how shell traverses directories to deal with specific directories". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report