logo

Home page
Articles for Windows, Linux, OS X
Mac tips and articles
Mac tips
Windows 8 tips and articles
Windows 7 tips and articles
Vista Tips
XP Tips
Linux tips and articles
Read the blog
Online store
Windows, Linux, OS X programs
Links
About

Linux tips and tweaks

Print file and folder/directory listings

Most of the time, browsing folders and drives and viewing the contents on the screen using Nautilus, Dolphin, Knonqueror, or whatever file browser you use, is just what you need, but there may be occasions when you need a hard copy of the contents of a folder, disc or drive. For example, you might want a printed listing of the contents of a CD or DVD and you may want to include this in the box insert or cover. That's just one posisble use and there are many more. How can we print file and folder listings in Linux? There are several different solutions to this and they are quite straightforward. Let's take a look at some of the ways we can do this.

One simple way to print a file and directory listing is using copy and paste. For example, click Place, Home Folder in Ubuntu to open a Nautilus file browser window. Press Ctrl+A to select all the files, then open Gedit text editor on the Applications, Accessories menu and press Ctrl+V. The full paths of the files and folders selected in the browser window are pasted in. You can then print the listing in Gedit by clicking the printer icon in the toolbar.

The best method of printing directory listings is to go to the command prompt and use the ls command. Open a Terminal window and enter ls to display a directory listing on the screen. So how do we print it? The answer is to send the output to a file. If we enter:

ls >file.txt

then the output that is normally sent to the screen is redirected to the file. There's no need to create it beforehand because it is created automatically. We can then open the text file in a text editor or word processor and print it from there. (Using a word processor or text editor gives you the option to add headings, formatting and so on.)

The ls command has many options and we'll look at a few of the more useful ones here:

-aPrint all files, including hidden ones
-lProduces a long and detailed listing
-pPrint a / in front of directories
-rReverse the order of the files
-RRecurse - print the contents of subdirectories too
-1Use one column instead of multi-column

There are more command line switches and you can look them up if you need them, but these will do for printing directory listings. You can use one or several, so

ls -l >file.txt

will send a long listing to file.text, which can then be loaded into a text editor and printed. To include all files and directories and those in subdirectories and to print one file per line you would use

ls -aR1 >file.txt

We just combine the switches we want. Load the file into your favourite text editor afterwards and print it out.

Go to Linux tips index...

copyright