1

I am trying to write a script that will report back the last time a file was updated in a folder. The folders are created everyday Example: /home/user/todaysdate. Inside todaysdate folder exsist that are created when somethng is created. So for example we will have fol1 fol2 fol3. I am trying to get a script to login to each folder and display the time of the last file created.

#!/bin/bash
folder=`date +%Y%m%d`
ssh -q server "cd /home/user/$folder/ ; bash -c "ls -la | uniq" > list.txt

This will create a list of the different folders that were made. Now I need to ssh into each folder which is where I am stuck. The output of the list.txt will be: fol1 fol2 fol3

ssh -q server "bash -c \"ls -ltr /home/user/$folder/<Need List Variable Here> | tail -1\"" awk '{print $8}

The above command gives me my time. I believe I need a while loop to ssh into the server and into the variables created in list.txt but I can't get a working on. Any help?

1
  • disregard fixed it using a for loop. for LINE in $(cat list.txt) do Commented Apr 5, 2013 at 3:00

1 Answer 1

1

If list.txt is space separated (eg "fol1 fol2 fol3") try a bash for/in loop:

for folder in $(cat list.txt);
do
    echo $folder;
done

If list.txt is newline separated then try a bash while loop:

cat list.txt | while read folder;
do
    echo $folder;
done

See this page for more information about bash loops.

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately, reading from a file or from $(ls) are both fraught with perils. Filenames can have special characters that really hose you. mywiki.wooledge.org/ParsingLs

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.