How to work with one directory at a time?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to work with one directory at a time?
# 1  
Old 05-07-2011
How to work with one day at a time?

I have a backup folder that has sub folders created daily. Each sub-folder contains files for that specific day. I need to search all those files to see which ones contains a specific phrase. I also need to take that phrase and output to a text file for that day. This can be done with a one liner, but due to space issues on the box, all the files are gzipped. So, I need to work with a days worth of files before moving the next day.

1. Find the files for say april 1st (this is in the file name as 110401)
3. Gunzip the files
4. Grep the files and send output to a text file
5. Gzip the files
6. Move on to the next day

I know how to find, gunzip, grep, redirect to another file, gzip the files. But how do I get it to do it per day?

Code:
find . -type f -name "*.110401*" -exec gunzip {} \;
 grep <string1> *.110401* > ../110401.txt
 gzip "*.110401*"

After this, the script should go to the next day. How do I accomplish this?

---------- Post updated at 04:38 PM ---------- Previous update was at 11:17 AM ----------

I found that zgrep allows me to read into the gz file without having to uncompress it.

So it's pretty easy to do from here.

Last edited by bbbngowc; 05-07-2011 at 01:25 PM..
# 2  
Old 05-07-2011
You mean like putting it into a loop? Try
Code:
for p in dir1104*/*1104[0-9][0-9]*.gz ; do 
   tar --to-stdout -xf $p | grep  Amethyst >> ${p%%.*}.log 
done

The above will loop through all .gz files containing 1104 and two digits inside the dir1104* directories (please adjust to your particular dir structure); grep needs to be redirected with >> otherwise you'll have in the log only the matches of the last file; no need for find(1) in this construct. You can use 'grep -A3' to get 3 lines after the matched line. ${p%%.*} will substitute only the part of filename before the first period (in some modern shells).

I tried zgrep, but it only reports match/no match, and not the actual content matched....

--------------------------------
I just noticed you're not dealing with tarballs, just gzipped files... zgrep works fine there:
Code:
for p in dir1104*/*1104[0-9][0-9]*.gz ; do 
   zgrep  Amethyst "$p" >> ${p%%.*}.log 
done


Last edited by mirni; 05-08-2011 at 12:49 AM.. Reason: tar comment
# 3  
Old 05-07-2011
# 4  
Old 05-08-2011
For grepping within the zip you need not unzip it. Use the command zgrep.
eg:-
Code:
zgrep "your phrase" log.gz

regards,
Ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. OS X (Apple)

'time' does NOT work on a function in 'dash'.

Hi guys and gals... I am writing a piece of code that is dash compliant and came across this error. I have put it in the OSX section as that is what I am using. I have no idea what the 'dash' version is but was installed about 6 months ago. MBP, OSX 10.12.6, default terminal running dash on... (4 Replies)
Discussion started by: wisecracker
4 Replies

2. Shell Programming and Scripting

awk work with time change in csv file

Hi, i have csv input file looks like below 3rd field is date and time field i want to change it with user supplied date and time says year=2011 month=09 day=05 hour=11 count=2 when count is say 10 then first ten records should pick and it should increment the... (2 Replies)
Discussion started by: raghavendra.nsn
2 Replies

3. Shell Programming and Scripting

Crontab - wrote Simple Script but i cant work out how to play it at a certain time.

Hi everyone. Silly might be silly be I'm still new to bash. I'm trying to make an Alarm Clock for in the morning using my laptop i have wrote this Simple Script but i cant work out how to play it at a certain time. #!/bin/bash cd /home/josh/Music/Bruno_Mars/Hooligans/ cvlc... (8 Replies)
Discussion started by: jtsmith90
8 Replies

4. Shell Programming and Scripting

Why doesn't piping ls to cd work if there's only one directory?

Hey guys. If I'm in a directory with only one directory, for example: foo@bar:/$ ls really-complicated-or-long-dir-name Why wouldn't piping this single line of output into cd work? foo@bar:/$ ls | cd I'm just getting started with the shell and I was trying to navigate into a directory with a... (1 Reply)
Discussion started by: Hipster
1 Replies

5. Shell Programming and Scripting

work area directory jumping script?

hi all, Ive been trying to find some way of doing this for ages but i have is a shell script that activates a python search and there it no tab completion. more familliar with tcsh at the moment but i'm a newbe. i was trying to make an easy way to browse to a list of working directories. ... (0 Replies)
Discussion started by: jvan
0 Replies

6. Red Hat

How do sa1/sar time intervals work?

Hi, I have set up sar on my RedHat and Fedora Linux systems. I am running sa1 from cron: 0 8-17 * * 1-5 /usr/lib/sa/sa1 1200 3 & The 1200 and 3 parameters tell sa1 to save data every 1200 seconds (== 20 minutes) and to write 3 times. When I run sar to observe my data, I'll see... (1 Reply)
Discussion started by: mschwage
1 Replies

7. UNIX for Dummies Questions & Answers

Copying one file at a time from one directory to another directory.

Hi All i want to write a script which could copy one file at a time from one directory to another directory. Scenerio: Let's say i have 100 file in a dirctory,so i want to copy one file at a time to another directory with a sleep statement in between that of 30 secs. please help me... (1 Reply)
Discussion started by: Nikhilindurkar
1 Replies

8. UNIX for Dummies Questions & Answers

Getting current work directory in Command Prompt

How to get the current working directory as part of the command prompt? Every time I chage the folder, my command prompt path shoud change. I am using Korn Shell. Any help is greatly appreciated. (3 Replies)
Discussion started by: MeganP
3 Replies

9. UNIX for Dummies Questions & Answers

Files in work directory reverting to root ownership

Hi, I have a problem with a Unix server we do not adminster but have an application running on. The problem is that overnight, files in the /user4/work directory revert to root ownership. This causes problems as we cannot process the files. 1) What would be causing files to revert to root... (1 Reply)
Discussion started by: canman
1 Replies
Login or Register to Ask a Question