0

I have two directories, lets call them X and Y Within them I have 100k+ files, .jpg files in X and .txt files in Y

I want to randomly select N files from X and copy to folder Z

This should be manageable using find + shuffle.

I then want to find all of the files in Y with the same names of the files that were copied to Z, but they are .txt files and copy them to directory W

To visualize: N files from X >> Z Same N files from Y >> W

How would I go about doing that?

2
  • So you have a task and an idea how to solve it. Where are you stuck?
    – FelixJN
    Aug 2, 2022 at 7:10
  • I have no idea about how to get the file names of the files that were moved and select the from the Y directory and especially without messing something up and accidentally deleting a bunch of files. I am working on a remote system, so reuploading the files would take a lot of time. Aug 2, 2022 at 7:34

1 Answer 1

0
#!/bin/bash

X=/path/to/X
Y=/path/to/Y
Z=/path/to/Z

mapfile -d '' -t files < <(find "$X" -type f -name '*.jpg' -print0 |
                           shuf -z -n 10 -)

for f in "${files[@]}"; do
  echo cp "$f" "$Z"
  bn=$(basename "$f" ".jpg")
  echo cp "$Y/$bn.txt" "$Z"
done

This script is untested but should do a dry-run of what you want. Set the X, Y, and Z variables to the correct values, then run it to see what it would do, adjust as needed and when it works as required, remove the echo from both of the cp lines.

It works by first populating an array ($files) with 10 random .jpg filenames from directory $X. It uses NUL as the filename separator, so will work with any filenames, even those including annoying characters like spaces, tabs, and shell meta-characters.

Then it iterates over each of those filenames to 1. copy them to directory $Z, 2. extract the basename portion of the filename, 3. copy the basename + .txt from directory $Y to directory $Z.

BTW, this requires bash version 4.4-alpha (released late 2015) or newer, because that's when the -d option was added to mapfile.

6
  • Thank you, the script is missing the "W" folder which the .txt files are to be moved to, but I can add that my self. I will test it out now. Aug 2, 2022 at 8:58
  • Tested it with 10 it works, running now with 18887s now, but it seems to be working 👍🏼 Aug 2, 2022 at 9:10
  • I guess that you are either a superuser or do this stuff for a living, but it works!! Thank you so much! Aug 2, 2022 at 9:30
  • Sorry about the W folder, I missed that when reading your question and thought you wanted the .txt files copied to Z too. But you figured out how to add that yourself, so that's good. Yeah, I used to do sysadmin stuff for a living but have retired now due to ill-health...but any kind of programming is going to involve figuring out exactly what you want to do, the individual steps required, and what data is needed to enable that. Then try to write the code to do that as simple and straight-forwardly as possible.
    – cas
    Aug 4, 2022 at 1:42
  • BTW, there's a very well-known and influential book written in 1976 by Niklaus Wirth (the designer of the Pascal programming language, and other very influential languages including Algol W, Modula, Modula-2) on the topic called Algorithms + Data Structures = Programs. I haven't written anything in Pascal for decades but the book does give an excellent grounding in how to think about solving programming problems, and I still use ideas from it today.
    – cas
    Aug 4, 2022 at 1:48

You must log in to answer this question.

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