I have this folder structure:


enter image description here

Each ProductCode## folder contains many JPG files, like

image01.jpg, image02.jpg and so on

I need to create a file that contains a single line for each ProductCode that lists all the files that exist in the folder "ProductCode##", like:

/Brand/ProductCode01/image01.jpg /Brand/ProductCode01/image02.jpg /Brand/ProductCode01/image03.jpg
/Brand/ProductCode02/image01.jpg /Brand/ProductCode02/image02.jpg

All the files need to be listed on a single line, separated by space and ended with newline at the last filename.

Any suggestions?

Thank You.

  • Do you want linux newline or windows {carriage return + newline} at the end? Or something else?
    – sudodus
    Commented Apr 17, 2023 at 16:47
  • I need Linux newline Commented Apr 17, 2023 at 17:44
  • Better use tree command than taking pictures... My post is edited accordingly Commented Apr 17, 2023 at 20:23
  • Advice to newcomers: If an answer solves your problem, please accept it by clicking the large check mark (✓) next to it and optionally also up-vote it (up-voting requires at least 15 reputation points). If you found other answers helpful, please up-vote them. Accepting and up-voting helps future readers. Commented Apr 19, 2023 at 15:51

4 Answers 4


Don't confuse CR-LF and LF! AKA Carriage Return and Line Feed

Like this:

$ tree
└── Brand
    └── Ducati
        ├── A 1060
        │   ├── image01.jpg
        │   └── image02.jpg
        ├── A 1061
        │   ├── image01.jpg
        │   └── image02.jpg
        ├── A 1062
        │   ├── image01.jpg
        │   └── image02.jpg
        └── A 1063
            ├── image01.jpg
            └── image02.jpg

6 directories, 8 files
$ for d in ./Brand/Ducati/A*/; do echo "$d"image*.jpg; done | tee file.txt
./Brand/Ducati/A 1060/image01.jpg ./Brand/Ducati/A 1060/image02.jpg
./Brand/Ducati/A 1061/image01.jpg ./Brand/Ducati/A 1061/image02.jpg
./Brand/Ducati/A 1062/image01.jpg ./Brand/Ducati/A 1062/image02.jpg
./Brand/Ducati/A 1063/image01.jpg ./Brand/Ducati/A 1063/image02.jpg

Works both in bash and zsh and probably ksh and bourne like shells.


In zsh:

  for dir ( /Brand/ProductCode<->(Nn/) )
    () {print -rC1 -- "$*"} $dir/image<->.jpg(Nn)
} > your-file.txt

Or for directories (/ glob qualifier) with any names inside /Brand, and all regular files (. qualifier) within them recursively (still sorted numerically with the n glob qualifier):

  for dir ( /Brand/*(Nn/) )
    () {printf -rC1 -- "$*"} $dir/**/*(Nn.)
} > your-file.txt

Not that since newline (or space which you use as separator here) is as valid a character as any in a file paths, you could still get more than one line per brand.


It should work with

for i in /Brand/Ducati/*/; do ( find "$i" -type f | sed -e "s/^/'/" -e "s/$/'/"| tr '\n' ' '|sed "s/ $/\n/" ); done
  • All file names (in the directory trees of /Brand/Ducati/subdirs/ ) will be listed (one set of lines for each subdir)
  • a quote will be put before and after each file name (in order to manage file names with spaces and other special characters later on)
  • newlines will be converted to spaces
  • the last space in each line will be converted to newline
  • Is there any way to use a single command line for all folders inside /Brand? I tried /Brand/*/ but it doesn't works, it generate a single line only Commented Apr 18, 2023 at 17:07
  • I tested the current command line on a directory structure that I think matches what you show in the picture in the question, and it works there. You must explain with enough detail what you have and what you want, otherwise it will be difficult to help you. - I think that it would work if you replace 'Ducati' with an asterisk: for i in /Brand/*/*/; do ... for the case that you ask for now.
    – sudodus
    Commented Apr 18, 2023 at 20:38
ls -1 | tr '\n' ' '

See also the -q and -b flags.

  • This will only show the files in the current directory, not the variously-located files the question is looking for. Commented Apr 18, 2023 at 7:10

You must log in to answer this question.

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