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 o开发者_运维知识库n 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
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
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"
精彩评论