Create text or csv file containing only file names from ‘ls -l’ command

What I want to achieve is the creation of a simple text file myfiles.txt that contains the name of each file in one directory. File names should be separated by a line break. 

The content of myfiles.txt should contain file name that look like this:

create file listing only form ls - animated gif

Step 1 – basic listing


Step 2 – remove first line from listing

We remove the first line that says “total 8”

That returns

Step 3 – remove directories

Now all lines that start with “d” (directories) will be removed.

Step 4 – extract file names

awk will return only the 9th block of each line. Blocks are separated by spaces in its default operating mode. Thus returning:

Step 5 – write the file

This will finally create our file!


(170 Posts)

20 years of coding and working as software engineer but I am still eager to learn more. I am very passionate when it comes to open source, Linux and Java. But I made my peace with Windows long ago to fully enjoy my PC gaming hobby. I have a soft spot for 90s electronic music and Babylon 5. In the evenings you will find me roaming the endless space in Warframe (IGN k05h).

One thought on “Create text or csv file containing only file names from ‘ls -l’ command

  1. Lars Händler

    ls -p | grep -v /
    does the same but way shorter … thanks to Sven for the supplying this solution

Comments are closed.