0

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
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
newbie_dev
  • 57
  • 1
  • 6
  • asked a different way, but a nearly a dupe of this: http://stackoverflow.com/questions/246215/how-can-i-list-files-with-their-absolute-path-in-linux – hometoast Jul 30 '10 at 19:26

4 Answers4

2
find /home/jason/code -iname "*.txt" > prog_dir.txt
Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
  • 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 Jul 30 '10 at 19:23
  • Yep. `find \`pwd\` -iname "*.txt" > prog_dir.txt` Notice the backticks around pwd. – Amardeep AC9MF Jul 30 '10 at 19:25
  • Ahh, thats why it wasn't working.. Feels good that I was close at least. Thanks Amar – newbie_dev Jul 30 '10 at 19:28
  • 1
    You can just do `find . -iname '*.txt' > prog_dir.txt` – Daenyth Jul 30 '10 at 19:28
  • 1
    @Daenyth - That would not put the full path in the output file. – Amardeep AC9MF Jul 30 '10 at 19:32
  • @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 AC9MF Jul 30 '10 at 19:34
  • @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 Jul 30 '10 at 20:09
  • 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 Jul 30 '10 at 21:59
1

find ~/code -name '*.txt'

Daenyth
  • 35,856
  • 13
  • 85
  • 124
1
$>find -name "*.yourext" > myFile.txt

For more information on the find command, type:

$>man find
naikus
  • 24,302
  • 4
  • 42
  • 43
0

Would something like:


find . -name *.txt -print

do the trick?

Kiersten Arnold
  • 1,840
  • 1
  • 13
  • 17