0

The following code creates a list of the files in the directory and then shuffles them in a random order then spits out the file in a text document called list.txt

find *.EXT | sed 's:\ :\\\ :g' | sed 's/^/file /' | sort -R  > list.txt

Contents of the file would look something like this.

file filename_2.EXT
file filename_3.EXT
file filename_1.EXT
file filename_4.EXT
file filename_5.EXT

I would really like to know if it is possible to (very easily within terminal, on the command line) specify which filenames I want to see come first within list.txt, and then rest be random as normal, as the code above intends.

How would I be able to do this very easily on the command line and hopefully within this same exact line of code, or something else, doesn't matter, as long as it does what I am trying to achieve.

find *.EXT | sed 's:\ :\\\ :g' | sed 's/^/file /' | sort -R  > list.txt

It would maybe look something like:

( this is just to understand what I am trying to achieve )

find *.EXT | xsort --comefirstfiles=filename_3.EXT,filename_1.EXT | xsort --therest random | sed 's:\ :\\\ :g' | sed 's/^/file /' > list.txt

Is this possible? If not, what is the closest option to doing this within the same line as the "find" code?

1

In zsh:

files=( **/*.EXT(oe'{REPLY=$RANDOM}') )
first=(input3.EXT input5.EXT)
printf 'file %s\n' ${first[@]} ${files:|first} > list.txt

The first line assigns an array of all the files you're looking for (recursively), but orders them randomly.

The second line assigns an array of filenames that you'd like to have first in the resulting list.

The third line prints the filenames out to list.txt (prepended with "file "; the two arguments are the first files followed by the rest of the files -- but the latter uses the array removal syntax :| to remove entries from "files" that exist in the array "first".

On this sample input:

$ tree .
.
├── input1.EXT
├── input2.EXT
├── input3.EXT
├── input4.EXT
├── input5.EXT
└── subdir
    └── subdirfile1.EXT

The first sample run is:

$ files=( **/*.EXT(oe'{REPLY=$RANDOM}') )
$ first=(input3.EXT input5.EXT)
$ printf 'file %s\n' ${first[@]} ${files:|first}
file input3.EXT
file input5.EXT
file subdir/subdirfile1.EXT
file input4.EXT
file input1.EXT
file input2.EXT

and a subsequent sample run is:

$ files=( **/*.EXT(oe'{REPLY=$RANDOM}') )
$ first=(input3.EXT input5.EXT)
$ printf 'file %s\n' ${first[@]} ${files:|first}
file input3.EXT
file input5.EXT
file input1.EXT
file input2.EXT
file subdir/subdirfile1.EXT
file input4.EXT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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