Bash 脚本查找目录,列出其内容和子文件夹信息
我想编写一个脚本,它将:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
cut
从配置文件中提取列。使用-d
指定字段分隔符。 Cut 只允许使用单字符分隔符(例如单个空格),并且肯定有无数其他方法来分割行。然后只需手动将 Know 子目录附加到主干即可。
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.
如果该行上除了“DIR-Store”和目录之间的空格之外没有空格:
或者第一个斜杠上的此键而不是空格:
If there are no spaces on that line except for the one(s) between "DIR-Store" and the directory:
or this keys on the first slash rather than a space: