I've got this directory full of images, and I can do this:

echo *.jpg
image1.jpg image2.jpg image3.jpg # and so on

How can I get the output in a plain text file in this format?


2 Answers 2


Avoid using ls, bash globs can do it better

printf '%s\n' *.jpg >output_file
  • Why is bash globbing better than using ls?
    – Sjoerd
    Jan 26, 2012 at 8:18
  • If you just want to print the list, ls is good. But if you want to process the files, glob is better: for file in * will work for files with spaces in their names, while ls | while read file will not.
    – choroba
    Jan 26, 2012 at 8:58
  • @choroba. I am completely in favour of using bash globbing here, but you can easily get ls to handle multiple spaces in a filename by using IFS=$'\n'. Yes, it is more of a hassle, but it isn't a show-stopper for ls. The main restriction with output from ls is that it can't easily handle \n in filenames other than via the -b option (print octal escapes for nongraphic characters) which makes it rather impractical when compared to using globbing... Come to think of it: when a filename does have \n in it, then perhaps ls -b is the easiset way to hanlde it in an output file.
    – Peter.O
    Jan 26, 2012 at 11:01
  • Ok, ls writes non-printable characters 'as is' when the output is sent to a pipe (I was misled from the terminal output). However, ls is a further process to start, and I try to avoid it whenever possible.
    – enzotib
    Jan 26, 2012 at 11:28
  • Can this be done recursively?
    – Oxwivi
    Jan 26, 2012 at 16:35

I'd prefer find over ls. With find you can specify a lot of options, such as directory depth (min and max), access/modified time, use regular expressions and a lot more. Have a look at the find manpage.

runejuhl@lapaz:~/Pictures$ find . -iname '*.jpg'|head

runejuhl@lapaz:~/Pictures$ find . -maxdepth 2 -iname '*.jpg' |head

To get only the filename, you can use the -printf argument:

-printf format

True; print format on the standard output, interpreting \' escapes and%' directives. Field widths and precisions can be specified as with the printf' C function. Please note that many of the fields are printed as %s rather than %d, and this may mean that flags don't work as you might expect. This also means that the-' flag does work (it forces fields to be left-aligned). Unlike -print, -printf does not add a newline at the end of the string.

(look at the man page for more information)

To print only the filename, you'd use printf with "%f":

runejuhl@lapaz:~/Pictures$ find . -maxdepth 2 -iname '*.jpg' -printf '%f\n' |head

If using xargs, and passing the filename in this way, it might be a good idea to use "%f\0" and xargs -0 to use null delimiters.

When you feel like it, have a look at the -print0 argument and a real good look at xargs or parallel

  • Is it possible to only retrieve and save the names of the files minus the directory?
    – Oxwivi
    Jan 27, 2012 at 7:38
  • Yes, no problem. You can use the <code>-type</code> argument to specify which types. (I just read the question again, this isn't what you're looking for. I'll update my reply later today.)
    – runejuhl
    Jan 28, 2012 at 9:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .