shell scrips loop a dir


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell scrips loop a dir
# 1  
Old 02-16-2010
shell scrips loop a dir

hi

i need to create a shell script.
>this needs to run on a directory
> the directory has many sub-directories
>get the newly created directory
>get the file *.xyz from that directory
>find an occurence of word "abc" in that file
# 2  
Old 02-16-2010
What have you tried so far?
# 3  
Old 02-16-2010
zero progress.

---------- Post updated at 05:04 PM ---------- Previous update was at 05:01 PM ----------

infact i have tried a couple of things, but i am stuck on how to loop through a dir and take the latest dir Smilie
# 4  
Old 02-16-2010
Can't assume a commercial environment here where directories could come and go in seconds.

If we assume that there is only one new directory in the last 24 hours the problem becomes manageable.
The "tail" in this gash solution ensures that we have only one answer to "find" and can give the most recent (not guaranteed at all) directory.

Code:
NEW_DIR="`find . -type d -ctime -1 -print|tail -1`"

echo "New directory is : ${NEW_DIR}"

The full solution is possible.


Beware: My commercial backup software updates the directory creation time. This timestamp is not a reliable parameter at all.

Last edited by methyl; 02-16-2010 at 07:02 PM.. Reason: Code tags,warning, allow space characters in directory name
# 5  
Old 02-16-2010
can u explain the script .. thanks
# 6  
Old 02-16-2010
Quote:
NEW_DIR="`find . -type d -ctime -1 -print|tail -1`"

echo "New directory is : ${NEW_DIR}"
The unix "find" statement above looks for directories (-type d) with a creation time of under 24 hours (-ctime -1) and outputs the result of the find (-print) to standard output. In this case standard output channel from find is intercepted by the pipeline (|) and passed to the unix tail command which has been asked to provide the last line only from the pipeline (tail -1). The whole command sequence (find . -type d -ctime -1 -print|tail -1) is enclosed in backtick characters which in ksh an most modern shells cause the command sequence to execute. The result of the command execution which we expect to yield a directory name is enclosed in double quote characters to preserve space characters. The end product of the command sequence is placed into the environment variable NEW_DIR which we display on the screen with the unix "echo" command to show out result.

Beware: The unix "find" command has more varieties than Heinz and must be checked against your exact Operating System version.
# 7  
Old 02-16-2010
once in that dir , methyl can u pls complete the below 2 steps

>get the file *.xyz from that directory
>find an occurence of word "abc" in that file

@ methyl : thanks. infact i am so excited to see the scripts you have written; thanks

---------- Post updated at 06:36 PM ---------- Previous update was at 06:21 PM ----------

i gave it a try .. i landed up with the following code

cd "${NEW_DIR}"
grep "abc" *.abc > abc.txt

maybe this looks simple, but it did my job ; let me know if script can be more enhanced.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Assign read write permission to the user for specific dir and it's sub dir and files in AIX

I have searched this quite a long time but couldn't find the right method for me to use. I need to assign read write permission to the user for specific directories and it's sub directories and files. I do not want to use ACL. I do not want to assign user the same group of that directories too.... (0 Replies)
Discussion started by: blinkingdan
0 Replies

2. Shell Programming and Scripting

Loop through the dir and Rename zip files and their underlying text file.

I have files in the ABC_YYYYMMDD.zip format under a directory. Each zip file contains A text file in the ABC_YYYYMMDD.txt format. I am trying to create a script that will Rename the zip files and their underlying text file replacing the datepart in them with . For eg: in the case of... (1 Reply)
Discussion started by: bash987
1 Replies

3. Shell Programming and Scripting

Shell script to find files in dir and updation

Hi, I have the shell script requirement mentioned below : List all java and c files or all files in directory and sub directories' in folder structure in current dir. then search for pattren1 in all files globally and replace with other string . And also check the date... (3 Replies)
Discussion started by: ammulu
3 Replies

4. Shell Programming and Scripting

Issue with copying files into dir inside for loop

Hi , I'm trying to move/copy the files inside the loop into a directory . I tried the below code and the issue is the data is not copying into the created directory but the files are copying into another file file_path="/home/etc" Last_Day=20130930 mkdir $file_path/ARC_${Last_Day} ... (3 Replies)
Discussion started by: smile689
3 Replies

5. Shell Programming and Scripting

KSH - Find paths of multiple files in CC (dir and sub-dir))

Dear Members, I have a list of xml files like abc.xml.table prq.xml.table ... .. . in a txt file. Now I have to search the file(s) in all directories and sub-directories and print the full path of file in a output txt file. Please help me with the script or command to do so. ... (11 Replies)
Discussion started by: Yoodit
11 Replies

6. UNIX for Dummies Questions & Answers

How to list all files in dir and sub-dir's recursively along with file size?

I am very new to unix as well as shell scripting. I have to write a script for the following requirement. In have to list all the files in directory and its sub directories along with file path and size of the file Please help me in this regard and many thanks in advance. (3 Replies)
Discussion started by: nmakkena
3 Replies

7. Shell Programming and Scripting

A script to find dir, delete files in, and then del dir?

Hello!! I have directories from 2008, with files in them. I want to create a script that will find the directoried from 2008 (example directory: drwxr-xr-x 2 isplan users 1024 Nov 21 2008 FILES_112108), delete the files within those directories and then delete the directories... (3 Replies)
Discussion started by: bigben1220
3 Replies

8. UNIX and Linux Applications

CPIO Problem, copy to the root dir / instead of current dir

HI all, I got a CPIO archive that contains a unix filesystem that I try to extract, but it extract to the root dir / unstead of current dir, and happily it detects my file are newer otherwise it would have overwrited my system's file! I tried all these commands cpio -i --make-directories <... (2 Replies)
Discussion started by: nekkro-kvlt
2 Replies

9. Shell Programming and Scripting

Loop through files in dir, omit file with latest date

I want to loop through files in a directory but omit the file with the latest date in my list of files. How would I accomplish this? Thanks (2 Replies)
Discussion started by: stringzz
2 Replies

10. Shell Programming and Scripting

a script to clone a dir tree, & overwrite the dir struct elsewhere?

hi all, i'm looking for a bash or tcsh script that will clone an empty dir tree 'over' another tree ... specifically, i'd like to: (1) specify a src directory (2) list the directory tree/hiearchy beneath that src dir, w/o files -- just the dirs (3) clone that same, empty dir hierarchy to... (2 Replies)
Discussion started by: OpenMacNews
2 Replies
Login or Register to Ask a Question