Bash 脚本查找目录,列出其内容和子文件夹信息

发布于 2024-09-01 06:18:01 字数 978 浏览 2 评论 0原文

我想编写一个脚本,它将:

1- 在 *nix 文件系统上找到文件夹“store”

2- 移入该文件夹

3- 打印包含上次修改日期的内容列表

4- 计算子文件夹大小

该文件夹的绝对路径从服务器到服务器,但文件夹名称始终保持不变。

有一个配置文件包含该文件夹的正确路径,但它没有给出它的绝对路径。

示例配置:


帐户 ON

DIR-Store /hdd1

计划是


ِ根据配置文件,绝对路径将是“/hdd1/backup/store/”

我需要脚本来 grep“/hdd1” 或“DIR-”以外的任何内容Store”,添加“/backup/store/”,进入文件夹“store”,打印其内容列表,并计算子文件夹的大小。

到目前为止,我手动编辑每台服务器上的脚本以反映“store”文件夹的路径。

这是一个示例脚本:

    #!/bin/bash

echo " "

echo " "

echo "Moving Into Directory"

cd /hdd1/backup/store/

echo "Listing Directory Content"             

echo " "

ls -alh

echo "*******************************"

sleep 2

echo " "

echo "Calculating Backup Size"

echo " "

du -sh store/*

echo "**********   Done!   **********"

我知道我可以使用 grep

cat /etc/store.conf | grep DIR-Store

我只是不知道如何绕过选择路径,添加“/backup/store/”并继续前进。

任何帮助将不胜感激

I want to write a script that will:

1- locate folder "store" on a *nix filesystem

2- move into that folder

3- print list of contents with last modification date

4- calculate sub-folders size

This folder's absolute path changes from server to server, but the folder name remains the same always.

There is a config file that contains the correct path to that folder though, but it doesn't give absolute path to it.

Sample Config:


Account ON

DIR-Store /hdd1

Scheduled YES


ِAccording to the config file the absolute path would be "/hdd1/backup/store/"

I need the script to grep the "/hdd1" or anything beyond the word "DIR-Store", add "/backup/store/" to it, move into folder "store", print list of its contents, and calculate the sub-folder's size.

Until now I manually edit the script on each server to reflect the path to the "store" folder.

Here is a sample script:

    #!/bin/bash

echo " "

echo " "

echo "Moving Into Directory"

cd /hdd1/backup/store/

echo "Listing Directory Content"             

echo " "

ls -alh

echo "*******************************"

sleep 2

echo " "

echo "Calculating Backup Size"

echo " "

du -sh store/*

echo "**********   Done!   **********"

I know I could use grep

cat /etc/store.conf | grep DIR-Store

I just don't know how to get around selecting the path, adding the "/backup/store/" and moving ahead.

Any help will be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一百个冬季 2024-09-08 06:18:01

您可以使用 cut 从配置文件中提取列。使用 -d 指定字段分隔符。 Cut 只允许使用单字符分隔符(例如单个空格),并且肯定有无数其他方法来分割行。

然后只需手动将 Know 子目录附加到主干即可。

STORE=$(grep DIR-Store /etc/store.conf | cut -d" " -f2)
DIR="${STORE}/backup/store"

pushd "${DIR}"
ls -alh
sleep 2
du -sh *
popd

You can use cut to extract columns from the configuration file. Specify a field delimiter with -d. Cut only allows single-character delimiters (like e.g. a single space) and there are certainly gazillion other ways to split the line.

Then just manually append the know subdirectory to the stem.

STORE=$(grep DIR-Store /etc/store.conf | cut -d" " -f2)
DIR="${STORE}/backup/store"

pushd "${DIR}"
ls -alh
sleep 2
du -sh *
popd
季末如歌 2024-09-08 06:18:01

如果该行上除了“DIR-Store”和目录之间的空格之外没有空格:

dir=($(grep "DIR-Store" /etc/store.conf))
dir="${dir[1]}/backup/store"
cd "$dir"    # or pushd "$dir"

或者第一个斜杠上的此键而不是空格:

dir=$(grep "DIR-Store" /etc/store.conf)
dir="/${dir#*/}/backup/store"
cd "$dir"    # or pushd "$dir"

If there are no spaces on that line except for the one(s) between "DIR-Store" and the directory:

dir=($(grep "DIR-Store" /etc/store.conf))
dir="${dir[1]}/backup/store"
cd "$dir"    # or pushd "$dir"

or this keys on the first slash rather than a space:

dir=$(grep "DIR-Store" /etc/store.conf)
dir="/${dir#*/}/backup/store"
cd "$dir"    # or pushd "$dir"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文