Echoing only once for each subdir


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Echoing only once for each subdir
# 15  
Old 06-11-2014
I'm lost what you are trying to achieve. On the files you are searching for, are you just after matching them on a date? I've very confused. Smilie

Your DATEN, DATE2 etc. values will be the string for the date in the format Mmm dd YYYY where dd is padded with a leading space if required. This matches the output from ls -l when a file is older than 6 months, but a truss gives me no output.

Perhaps we should start again with the logic.

Are you looking for files that match a date-stamp? There are easier ways with the find command.

How many files must you find in a particular directory to regard it as a success? I'm guessing that it could be zero, 1, 2, 3 or 4.

Is there a reason that you compare every file that matches *.txt one at a time? It reads the directory for every file you match. it would be quicker to read the directory for the files you want or read it once and scan the output. What is your matching criteria?


Smilie I'm so confused Smilie


Robin
# 16  
Old 06-11-2014
Matching criteria

The output of truss command is as follows:

Code:
DATEN=$(perl -e 'print scalar(localtime(time -  0 * 24 * 60 * 60)), "\n";' |awk '{print $2,$3,$5}')

Jun 11 2014 -this is the output of truss command

This date is supposed to be matched against files in each directory:

Dir1
file1--checks truss date against file date
file2
file3

Dir2
filea
fileb
filec

What happens is that it checks every file and so it prints out a message for every file. What I want it to do is print one message for each directory. The message should say "Not found" for any directory that does not have at least one file that matches the date in truss command.

But it prints a message for each file that does not match, because it is not written correctly.

I really don't want to use the find command, this is an experiment with truss and loops for myself .
# 17  
Old 06-11-2014
So is the date part of the file name or the last modified time?

If it's part of the file name try variations on:-
Code:
if [ -f "*${DATEN}*" ] || [ -f "*${DATE2}*" ] || [ -f "*${DATE3}*" ] || [ -f "*${DATE4}*" ]
then
    echo "Directory $PWD has at least one file name matching the dates."
else
   echo "Directory $PWD does not contain a file name matching the dates requested."
fi

The logic will be true if a file name containing the first or second or third or fourth date exists in the current directory.


If you are looking for a file modified on the correct date, then that's tricker, however you could try variations on:-
Code:
find . -type f -mtime -4 | xargs echo "Directory $PWD contains the following:-"

You may need to add a -prune clause if there are subdirectories you don't want to search down.

Using commands like ls -l to show the directory of files including their last modification date/time is problematic because the date format for files modified within the last 6 months is Mmm dd HH:Mi

If you don't want to use find then I'd have to ask why. It's probably the right tool for the job. Never screw something in with a hammer. If you want to go a bit Heath-Robinson then I'm out.

Of course, if I've missed the point then let me know.

What is the point of the truss command anyway? I'm most confused over that I think.


Am I getting anywhere yet?


Robin
# 18  
Old 06-11-2014
Quote:
Originally Posted by newbie2010
The output of truss command is as follows:

Code:
DATEN=$(perl -e 'print scalar(localtime(time -  0 * 24 * 60 * 60)), "\n";' |awk '{print $2,$3,$5}')

Jun 11 2014 -this is the output of truss command

This date is supposed to be matched against files in each directory:

Dir1
file1--checks truss date against file date
file2
file3

Dir2
filea
fileb
filec

What happens is that it checks every file and so it prints out a message for every file. What I want it to do is print one message for each directory. The message should say "Not found" for any directory that does not have at least one file that matches the date in truss command.

But it prints a message for each file that does not match, because it is not written correctly.

I really don't want to use the find command, this is an experiment with truss and loops for myself .
No! It is not! That is the output of a perl command; not the truss command that I asked for:
Code:
truss -vlstat -tlstat ls -l $f 2>&1

when f is set to the name of a text file in the current directory? PLEASE SHOW US THE OUTPUT FROM THE ABOVE COMMAND!

From the commands following truss in your pipeline we know that the output from truss has to be at least four lines.

And you still have not told us what you are trying to do! WHAT CHECK ARE YOUR TRYING TO PERFORM BETWEEN FILES IN A DIRECTORY AND YOUR FOUR DATES?
  1. Are you trying to check whether the name of a file in a directory is one of your date strings? (This is what your current code is doing! And, we know it is doomed to fail: a date string ending in "2014" can never be a filename ending in ".txt"!)
  2. Are you trying to check whether or not one of the date strings is part of the name of a file in a directory?
  3. Are you trying to check whether or not the date on which the file was last read matches one of the date strings?
  4. Are you trying to check whether or not the date on which the file was last modified matches one of the date strings?
  5. Are you trying to check whether or not the date on which the file status was last modified matches one of the date strings?
  6. Are you simply trying to determine whether or not any file in a directory has been modified since midnight four days ago?
  7. Are you simply trying to determine whether or not any file in a directory has been modified in the last 96 hours (24 hours/day * 4 days)?
If you can't clearly explain what you are trying to do in unambiguous terms, we can't help you.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 19  
Old 06-13-2014
Thanks, all!

Thanks all, for your suggestions. I finally did make this work. I still used truss, and needed some awk as well, but I got it to do what I wanted by carefully inspecting each line of the script.
# 20  
Old 06-13-2014
Quote:
Originally Posted by newbie2010
Thanks all, for your suggestions. I finally did make this work. I still used truss, and needed some awk as well, but I got it to do what I wanted by carefully inspecting each line of the script.
If you will show us the output you get from running truss and show us your current working script, there is a good chance we can help you make it more efficient.

It will also help other people reading this thread in the future figure out how you solved your problem.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Getopts not echoing correctly

Hi, When I run the the following code: #!/bin/bash if ]; then usage fi if ] then echo "Do not execute this as root, use -s instead" fi SERVERFILE="servers" function usage { echo "USAGE: ${0} COMMAND" (4 Replies)
Discussion started by: mohca2020
4 Replies

2. Shell Programming and Scripting

Echoing silently?

I know, sounds mutually exclusive :-) I have a script where I ask for a password and store it in a variable, and then use it with sudo on an array of other hosts. The password winds up being choed back to my terminal as well as to the process on the remote host, like: Attempting to update... (2 Replies)
Discussion started by: jnojr
2 Replies

3. UNIX for Dummies Questions & Answers

copying the dir/subdir structure from one server to another?

Hi All, I want to copy the dir/subdir structure from SERVER-A to SERVER-B without copying all the files in each dir. Is it possible using SCP / SFTP command? For example, SERVER-A has following two dir/subdirectories and files under each subdir. ... (1 Reply)
Discussion started by: Hangman2
1 Replies

4. Shell Programming and Scripting

Copy files from subdir

Hey, How can I copy files from subdirectories without copy the subdir and copy it to a higher dir For example: /home/test/subdir/file1 copy file1 to /home or another dir thanx (11 Replies)
Discussion started by: Eclecticaa
11 Replies

5. Shell Programming and Scripting

moving dir subdir and files

I have created a directory structure and under the directory subdirectories and files are there.I need to move the entire thing to another path.How can i write a script to do that. currently the path of files is as below : /data1/serial/mcycle/archive : under this path differnt sub dir exist ... (6 Replies)
Discussion started by: dr46014
6 Replies

6. Shell Programming and Scripting

Grep in subdir, multiple files

Hi, I m trying to have script to grep a pattern in all files under a directory. I use Sun Solaris, the below want doesnt do exactly what I want. find /home/xxx/ tagHeu | while read FILE; do grep text $FILE && echo 1 grep text $FILE || echo 0 done I also tried running: find... (2 Replies)
Discussion started by: s3rro
2 Replies

7. Shell Programming and Scripting

move files and retain subdir structure

hi: I have some files like this folder1/recording1.mp3 folder1/docs/budget.doc folder2/others/misc.mp3 folder3/others/notes.doc all this folders and files are under the mp3 folder. I would like to move just the mp3s to another folder but retain the subdir structure i have. So if... (4 Replies)
Discussion started by: jason7
4 Replies

8. Shell Programming and Scripting

Echoing

I was just wondering how you would echo out different length variables but still have them all line up. I tried putting tabs between the variables but that didn't work as planned. For example this is in some loop, with different variables in it each time: echo "$1 $2 $3 $4 $5" Appears like... (3 Replies)
Discussion started by: Okema
3 Replies

9. UNIX for Dummies Questions & Answers

echo not echoing correctly

Here is the file named tuwork.......... 209 200 WZ 6529 SKTNCA01X4X C POI LODI LODI 738 SKTNCA0127T LOD Here is the scipt....... cat tuwork | while read rva do num=`echo $rva | cut -d" " -f1-2` reg=`echo $rva | cut -c10` ocn=`echo $rva | cut -c12-15` x=`echo $rva | cut -c29`... (3 Replies)
Discussion started by: shorty
3 Replies

10. Shell Programming and Scripting

Finding patterns through out all subdir

Hi all experts, here is a problem which i would appreciate ur expertise. I need to do this: Eg. Find a number: 1234567 which i dunno which file and which folder I do know which main folder it is in but it is hidden deep within a lot of subdir. Is it possible to find the file? + output... (4 Replies)
Discussion started by: unnerdy
4 Replies
Login or Register to Ask a Question