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