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
  • 4
  • 12

5 Answers5

6

Too much work.

ls > ../log.txt
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
1

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

ls -p | grep -v / > log.txt 
farbiondriven
  • 2,450
  • 2
  • 15
  • 31
1

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

for i in *; do echo $i >> /tmp/log.txt; done
nbari
  • 25,603
  • 10
  • 76
  • 131
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.

kaiffeetasse
  • 481
  • 1
  • 8
  • 18
  • 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