-1

In Shell Script How to copy files to a randomly generated directory path?

I need to copy files to a directory under my home directory like "/home/myproject/Gs54hs6h/images/"

but in the middle of the path there is a directory "Gs54hs6h" it gets created automatically every time with a new name; when there is a new process ran. what is the shell script notation I can use to passthrough this randomly generated directory name? cp xyz.img /home/mydotproj/xxxxxxxxx/images/

Pj1947
  • 1
  • 1
  • 3
    Why not pass the new name to your script as the first positional parameter (e.g. `$1`). So your run your script as `bash myscript.sh thenewname` and in your script you can have something like `destdir="/home/mydotproj/$1/images/"`, and then `mkdir -p "$destdir" | exit 1` to create and verify the new directory, then just `cp xyz.img "$destdir"`. – David C. Rankin Jan 11 '23 at 02:56
  • 1
    Also, welcome to Stack Overflow. Please read the [About](http://stackoverflow.com/tour) page soon and also visit the links describing [How to Ask a Question](http://stackoverflow.com/questions/how-to-ask) and [How to create a Minimal Complete Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Providing the necessary details, including your MCRE, compiler warnings and associated errors, and sample data if any, will allow everyone here to help you with your question. – David C. Rankin Jan 11 '23 at 02:57
  • If you are asking how to create a uniquely named directory, look at `mktemp` – tripleee Jan 11 '23 at 09:02
  • Did you generate this directory by yourself, or does it spring into exisitence magically? In the latter case, you could generate a list of **all** directories inside _myproject_ and choose the one with the latest modification time. – user1934428 Jan 11 '23 at 10:50
  • unique name directory gets created by default with a random name every time server get restarted. I need to access that directory in my script and and copy some files there... – Pj1947 Jan 11 '23 at 15:42

1 Answers1

-1

If you don't know the target, you can start with what you already know and filter them out. And whatever left over is hopefully your target. For an example, you can get a latest directory created/modified which might be your target.

# in your script
target="$(ls -dt * | tail -1)"
if [[ ! -z $target ]]; then
    cp xyz.img /home/mydotproj/${target}/images/
else
    # target directory not found, do something else or exit
    exit
fi

If there are multiple RANDOM (possible target) directories, you can mark them manually at first by creating empty file(e.g.: touch marker.txt) in those directories. And then any directory that doesn't have your marker, should be a good candidate as target.

Calvin Kim
  • 359
  • 2
  • 6
  • Thanks so much for the idea... but in my case, this dynamic directory gets created with a new random name when the server get started. When I try with your code idea it complains as "tail: cannot open input" – Pj1947 Jan 11 '23 at 15:34
  • My bad, that was a typo. It's `tail -1` istead of `tail 1` – Calvin Kim Jan 12 '23 at 18:40