Stuck in this shell script - please help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Stuck in this shell script - please help
# 1  
Old 03-30-2011
Stuck in this shell script - please help

hi:

I'm trying to write a shell script that recognizes all .txt files in all the subdirectories in my current directory.
Let's say that i have a directory called Applications which consists of many subdirectories on mnay levels.
i want the shell script to look for all .txt files that exist only in DOC / DOCS subdirectories. (the thing is I have many DOC / DOCS subdirectories on different levels, i cant specify the path to them)

this is what i have so far but it's not working:
the first loop is to look into all DOC /DOCS subdirectories
the 2nd loop is suppose to look for all .txt files
and i want to display the name of all .txt files that exist in DOC/DOCS
and that what's the echo is doing.
Code:
#!/bin/sh
 system='/path/to/the/main/directory/Applications'
for i in `find $(system) \( -name DOC\* \) ` ;  do
  for j in `find $(system) \( -name "\.txt" \) ` ; do
    echo $j
    echo $i
  done
done



Regards

Last edited by Scott; 03-30-2011 at 04:12 PM.. Reason: Please use code tags
# 2  
Old 03-30-2011
Code:
cd myhome
find . -type f -name '*.txt'  -exec ls -l {} \;

# 3  
Old 03-30-2011
thank u for ur response
am getting this error now: missing argument to `-exec'
# 4  
Old 03-30-2011
What exactly did you type - is the trailing semicolon there, for example.
Please post what you tried.
And - what OS (flavor of UNIX) are you running on?
# 5  
Old 03-30-2011
I am running Ubuntu 10.4

The code i executed is
Code:
find . -type f -name '*.txt' -exec ls -l {} \ ;

the code is running now but it's giving me all the .txt files in all subdirectories but i am interested in .txt file in DOC and DOCS subdirectories only

is there any why i can combine the following code to the previous one so i only get .txt files in DOC and DOCS directories:
Code:
find .  -name '*.txt' -exec ls -l {} \ ;


Regards

Last edited by Scott; 03-30-2011 at 07:44 PM.. Reason: Use code tags, please...
# 6  
Old 03-30-2011
How about this for long file listing
Code:
find DOC DOCS -type f -name '*.txt' -ls

Or this just for the names:
Code:
find DOC DOCS -type f -name '*.txt'

# 7  
Old 03-31-2011
Nope, that didn't work since the DOC DOCS exist on so many subdirectories.
this is what i came up with
Code:
find . -name DOC* | while read i
do
  find $i -type f -name '*.txt'
  echo $i
done

now the last step i want to rename the .txt files to .txtOld
when i added the code below the find command, i noted that the DOC/DOCS file were renamed not the .txt files so they became .DOC.txtOld and this is Not what i want.
Code:
mv $i $.txtOld

Moderator's Comments:
Mod Comment Please use code tags


---------- Post updated 03-31-11 at 09:04 AM ---------- Previous update was 03-30-11 at 06:42 PM ----------

the $i in the code above seems to be a pointer to the DOC DOCS folders not to the .txt files inside those folders. how can i point to the .txt files instead of the directory that contains the files
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Error in script, STUCK :(

Hi All, I am beginner in scripting. I wrote a simple script to perform some task. It seem to have some error in command line, Kindly somebody help. Thanks #!/bin/bash date cd /home/poojasaxena/Desktop/CMS/script/DataMCMatch function pause(){ read -p "$*" } FILE=$1... (22 Replies)
Discussion started by: nrjrasaxena
22 Replies

2. Shell Programming and Scripting

stuck on first script with sed issue

Hi, I'm developing my first bash script and have made good progress but stuck at this point. I've run sed on a text file to extract some data and have saved it into a variable e.g. $blah the variable contains emails as follows e.g. <a@b.com> <b@c.com> I'm now trying to edit... (2 Replies)
Discussion started by: newb1000
2 Replies

3. Shell Programming and Scripting

Script to Output Files That Have Got Stuck

Hi, I need to create a script that we will schedule to run say every 30 mins to check a directory for files that have been present for a set period of time. Situation we have is that we have an input folder where files are processed from and once processed they get moved to a done folder,... (1 Reply)
Discussion started by: everythingno
1 Replies

4. Shell Programming and Scripting

korn shell display lenght problem!!! i got stuck on this

Using the KSH, write a shell script called display_by_length, which takes an absolute pathname to a directory and displays all ordinary files in the directory ordered by their length; for each file listed, display the name of the file and its length - nothing else. Extend this script to take an... (1 Reply)
Discussion started by: babuda0059
1 Replies

5. Shell Programming and Scripting

Script Stuck In Loop

Hi all! Im trying to get this script to check for folders in a year/month/day folder structure and if the day doesnt exist then it makes the day. It will also make sure all of the days before todays date exist as well. This script assumes that the month and year folder already exist. It works... (3 Replies)
Discussion started by: Grizzly
3 Replies

6. Homework & Coursework Questions

i get stuck with this shell script code

i get stuck here . Anyone could check my work? the user type a group of upper case letters at a time with 0 at the end. Find and display the first letter in alphabetic order. For example, input of F, G, K, S, U, G, D, Q, P , the result should be D Any invalid input character (eg. #, $, 3, a,... (5 Replies)
Discussion started by: sbcvn
5 Replies

7. Shell Programming and Scripting

I am stuck in my script

Hi All I have script that find 777 dir with specific extension like .php .Now after finding all 777 directory i will place in httpd.conf using a directory directive ,Now i was not do that,if directory entry exitst in httpd.conf then script ignor it dont show me at stdout else if it dont find... (2 Replies)
Discussion started by: aliahsan81
2 Replies

8. UNIX for Dummies Questions & Answers

stuck with a script

Hi There I am pretty new to UNIX and have only been using it from a basic point of view,I now want to start using it and learning more , have got a whole lot of books and documentation from the web and am slowly learning.I have written a get script in windows :- lcd E:\MAIN\PRO\FILES\MAINDB... (1 Reply)
Discussion started by: FOCKER
1 Replies

9. Shell Programming and Scripting

simple script but am stuck

hey i am a bit stuck here. i just started work experience and i need to create a simple script which delete all files in a specify folder which are older then 2 days. :mad: i tried but it never works!!! anyone! i dont know much but unix since i mostly work on NT here but i dont wanna disapoint my... (2 Replies)
Discussion started by: GermanJulian
2 Replies

10. Shell Programming and Scripting

stuck on ksh script

hi, i need help to write script in korn shell that will display info. on system paging, system process table.system file table inf. thank you (1 Reply)
Discussion started by: neer45
1 Replies
Login or Register to Ask a Question