tags:

views:

58

answers:

4

Hi all,

I am trying to write a simple bash script that will search for files with a certain extension in a directory. Then output all those files with full path in front.

For example, if I have a directory with many different file types, but I want to know the information about those with only the .txt extension. How can I get the output in a new file to look similar to this:

/home/jason/code/test1.txt
/home/jason/code/test2.txt
.
.
.

All I have right now is this, which is not really what I am trying to do, but it is just my attempt at experimentation because I am new:

ls *.txt >prog_list.txt
pwd >pwd.txt
cat pwd.txt prog_list.txt > prog_dir.txt
+1  A: 

find ~/code -name '*.txt'

Daenyth
+2  A: 
find /home/jason/code -iname "*.txt" > prog_dir.txt
Amardeep
Is there a way to do this so that it will work in any directory when the script is run? Kind of like "find pwd -iname "*.txt" > prog_dir.txt"
newbie_dev
Yep. `find \`pwd\` -iname "*.txt" > prog_dir.txt` Notice the backticks around pwd.
Amardeep
Ahh, thats why it wasn't working.. Feels good that I was close at least.Thanks Amar
newbie_dev
You can just do `find . -iname '*.txt' > prog_dir.txt`
Daenyth
@newbie_dev - I'm glad it was helpful.
Amardeep
Any idea on how I could accomplish the same in a Windows environment? Currently I have:dir /b /-p *.txt /o:n >C:\Users\jbeer\code\proglist.txtThanks!
newbie_dev
@Daenyth - That would not put the full path in the output file.
Amardeep
@newbie_dev - No habla ventanas! Seriously, though, I'm not as fluent with cmd.exe as I am with bash. Perhaps if you asked it as a separate question and tagged it Windows you'd attract some great responses.
Amardeep
@Amardeep, ah right. In that case I'd just do `locate .txt` if you need full paths... That won't anchor the search though.
Daenyth
Use `find "$PWD" -iname '*.txt'`—note the quotes, otherwise it wouldn't work with directory names containing spaces. Also note that it won't work with file names containing newlines.
Philipp
A: 

Would something like:


find . -name *.txt -print

do the trick?

carnold
The pattern must be quoted.
Philipp
+1  A: 
$>find -name "*.yourext" > myFile.txt

For more information on the find command, type:

$>man find
naikus