1

How can I create a number of text files, which include the filenames of the files in a specific directory, up to a maximum of 999 rows per text file?

I started from this:

find ./J0902-405/*.evt -maxdepth 1 -type f -fprintf files_xselect.list %f\\n

And it writes the filenames properly in the textfile. But afterwords, I need to put the 999 rows limit, and after that limit, create another text file with the following 999 names, and so on, until all the *.evt files are listed.

3 Answers 3

5

find ./J0902-405/*.evt -maxdepth 1 -type f | split -l999 -

From the manual page:

NAME
   split - split a file into pieces

SYNOPSIS
   split [OPTION]... [INPUT [PREFIX]]

DESCRIPTION
   Output  fixed-size pieces of INPUT to PREFIXaa, PREFIXab, ...; default size is 1000 
   lines, and default PREFIX is `x'.  With no INPUT, or when INPUT is -, read standard 
   input.

   -l, --lines=NUMBER
          put NUMBER lines per output file
2
  • 2
    +1 Never heard of split before, learning something knew every day. Commented Jan 24, 2014 at 4:09
  • It's very much a truism in POSIX, that most of the time, there's already the tool for the job, for simple enough values of $JOB. (:
    – DopeGhoti
    Commented Jan 24, 2014 at 4:10
1

@DopeGhoti's answer is the right approach, but let me flesh it out a bit, for those new to split (like me):

find ./J0902-405 -maxdepth 1 -name '*.evt' -type f -printf '%f\n' | \
  split -l 999 -d - files_xselect.list.
  • -name ... with a quoted filename pattern lets find do the pathname expansion (as opposed to the shell - no point in letting both the shell and find do the work).
  • -printf '%f\n' ensures that only filenames (no path components) are output, as in the OP.
  • -l 999 specifies the split size in lines; default is 1000.
  • -d causes numerical suffixes to be used for the output files (00, 01, ...) rather than the default letters (aa, ab, ...) [note: won't work on OSX]; default suffix length is 2; to control the number of digits/chars. in the suffix, use -a {length}.
  • - causes split to read from stdin - in this case, the output from find.
  • files_xselect.list. is the output-file prefix; thus, we get files files_xselect.list.00, files_xselect.list.01, ...

If you want more control over the output filename - e.g., to move the suffix data to a different part of the filename - you can use the --filter option (note: won't work on OS X), which accepts a shell command to which the output data for each file is piped, along with variable $FILE containing the name of the respective output filename; this gives you the chance to modify the output filename based on it:

For instance, to create output files named files_xselect.00.list, ... - i.e., to place the suffix data before the filename extension, you'd use:

... | split -l 999 -d --filter='> ${FILE}.list' - 'files_xselect.'
1

Something like

#!/bin/bash

for file in ./J0902-405/*.evt; do
  [[ $i > 999 ]] && i=0 && j=$((j+1))
  [[ -f $file ]] && i=$((i+1)) && echo "${file##*/}" >> "fileofnames$j.txt"
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.