2

I am sure there is a simple answer but I have been struggling to create a txt file of all the file names in a directory.

I am currently in the directory of where I need to collect all the file names of. This is what I have tried.

for i in ls; $i |../log.txt ; done

The log.txt file exists in the directory above.

spicy burrito
  • 187
  • 2
  • 12

5 Answers5

5

Too much work.

ls > ../log.txt
Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
1

This will exclude the directories from the list, leaving only the files.

ls -p | grep -v / > log.txt 
farbiondriven
  • 2,273
  • 2
  • 17
  • 29
1

Try this in case you would like to use a for:

for i in *; do echo $i >> /tmp/log.txt; done
nbari
  • 23,059
  • 8
  • 65
  • 110
1

Do this ls >> ../log.txt to append to an existing file.
If you only use '>' the file will be truncated.

Nilkun
  • 51
  • 5
0

simply run ls * > log.txt in your terminal.

karottenbunker
  • 333
  • 7
  • 16
  • 1
    The wildcard `*` does nothing useful here, and potentially causes problems (without `-d` it will descend into directories). http://mywiki.wooledge.org/ParsingLs explains a number of additional failure scenarios. – tripleee Oct 10 '17 at 10:45