Print file names into a text file with Bash

Here's how to join lines into a .txt or .csv file.

Let’s imagine a directory full of files that we want to print into a file:


cd into the directory and issue:

$ ls -1 | paste -sd "," -

That will list the file names separated by comma. Just change the comma to whatever you like. The following will use new line (\n) as a character and print the result straight into a file:

$ ls -1 | paste -sd "n" - > my-files.txt

If you want to limit the results, globs can be used in normal fashion. This lists only jpg files:

$ ls -1 *.jpg | paste -sd "n" - > my-files.txt

Or jpgs and pngs:

$ ls -1 *.{jpg,png} | paste -sd "n" - > my-files.txt

Use case

I have a super primitive accounting system, where I just keep my receipts in a directory, but have a CSV friendly naming convention.


A receipt would look like this:


At the end of the year when I need to give my receipts to the accountant, I just print them out as a CVS list:

$ ls -1 *.pdf | paste -sd "n" - > receipts-2015.csv

Then it can opened in Numbers or Excel for further modification.

Screenshot 2015-03-03 09.47.13

Club-Mate, the beverage →