Get the App
SLTechnology News&Howtos  ›  Development  › 

How shell traverses directories to deal with specific directories

Shulou Source: shulou.com Published: 2022-06-03 07:41:16 10月03日 Update

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!

Tags: Catalog processing content file month article method learning help code year method easy to understand more organization knowledge structure results tape industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Microsoft macOS Redmi Linux