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.

#!/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

cd myhome
find . -type f -name '*.txt'  -exec ls -l {} \;

thank u for ur response
am getting this error now: missing argument to `-exec'

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?

I am running Ubuntu 10.4

The code i executed is

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:

find .  -name '*.txt' -exec ls -l {} \ ;

Regards

How about this for long file listing

find DOC DOCS -type f -name '*.txt' -ls

Or this just for the names:

find DOC DOCS -type f -name '*.txt'

Nope, that didn't work since the DOC DOCS exist on so many subdirectories.
this is what i came up with

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.

mv $i $.txtOld

---------- 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

First find the directories, then look for ".txt" files in each directory. We have also avoided the "for" construct which is unsuitable for open-ended lists or where filenames might contain space characters.

#!/bin/sh
system='/path/to/the/main/directory/Applications'

find "${system}" -type d -name DOC\* -print | while read DIR
do
        find "${DIR}" -maxdepth 0 -type f -name \*\.txt -print
done
1 Like

It can be done just with find.

find . -type f \( -path "*/DOC/*" -o -path "*/DOCS/*" \) -name "*.txt" -exec rename .txt .txtOld {} \; 
1 Like

Only if find supports -path which isn't POSIX so many OS's may not have it.

Try the following modification

find . -name DOC* | while read i
do
  find $i -type f -name '*.txt' -exec mv {} {}old \
  echo $i
done

This is the only script that worked for me, in fact i want to do a further refinment to this code if it's possible, i would like to leave any *en*.txt files inside the DOC DOCS directories, i dont want to rename them. is this possible?

Just corrected my typo in the "-type" line.

#!/bin/sh
system='/path/to/the/main/directory/Applications'
 
find "${system}" -type d -name DOC\* -print | while read DIR
do
        find "${DIR}" -maxdepth 0 -type f -name \*\.txt -print
done

I don't have the Linux "find" command handy to test. The general answer is to build a condition into the command line using "find" syntax. There is an example earlier in this thread.
Totally untested, for illustration of the idea: "-a !" means boolean AND NOT .

find "${DIR}" -maxdepth 0 -type f \( -name '*.txt' -a ! -name '*en*.txt' \) -print

I was mistaken, the cose below was the only thing that worked for me, but when i added

 \( -name "*.txt" -a ! -name "*/en/*" \)

it didnt work
and i got this error

find: paths must precede expression: (-name
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

find ${StartingDir} -name "*.txt" | grep -e "/DOC/" -e "/DOCS/" |
while read FullPath
do
 mv ${FullPath} ${FullPath}Old
done