Bash script to find a directory, list it's contents and sub-folders info

Posted by lithiumion on Stack Overflow See other posts from Stack Overflow or by lithiumion
Published on 2010-05-09T16:41:13Z Indexed on 2010/05/09 16:48 UTC
Read the original article Hit count: 129

Filed under:
|
|
|
|

Hi

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 bath 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 "Config-Store", add "/backup/store/" to it, move into folder "store", print list of it's contents, and calculate sub-folders 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

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

Any help will be appreciated

© Stack Overflow or respective owner

Related posts about bash

Related posts about folder