Bash script - Finding files and manipulating paths

1

So I'm attempting to write a script that achieves the following:

  1. Reads file UID from a text file
  2. Searches for file location based on the UID
  3. Copies the file, including it's path structure to a new location.

We have medias that are currently being transferred manually, which I want to automate. Each media has a UID, which are altogether in a single file. The media is in a specific location, I know where the parent directory is, but not the individual directory of each media item.

Currently my approach is the following:

  • While loop until end of file with UID
  • Find matching media's path based on the UID
  • Output path to temp file as the structure of the media directory matters, an example of the path structure '../Medias/Shirts/shirtA.jpg', '../Medias/Shirts/shirtA_1.jpg', or '../Medias/Pants/pantsA.jpg'
  • Use temp file to copy Media to new location, i.e. 'cp $MEDIAPATH/$UID* $TARGET'

So I have two questions:

  1. There are a lot of media items, I am using find and dirname and I am testing with some smaller sample data, I'm am certain there is a more efficient way to do this than mine? FILEDIR="$(dirname "$(find ${MEDIADIR} -name "$UID" )")"
  2. Now for the copying the media to it's new location, I am unsure how to do this because I am not interested in the entire path, i.e. the path for the media item might be '/home/user/media/pants/pantsA.jpg' whereas I only need the /pants/, because when copying I want to maintain this stucture, i.e. '../targetDIR/pants/', etc?

Grinch91

Posted 2017-02-28T16:01:13.950

Reputation: 111

So let me get this stright, you have a file with a list of UID that you know are attached to a file name, for example UID of "1" is attached to shirtA.jpg and UID of "2" is attached to pantA.jpg? UID 1=shirtA.jpg or is the $UID actually the name of the file? – asmith – 2017-02-28T18:59:59.833

Sorry I can see the confusion and you are correct in your reasoning. A mistake on my part. So to clarify the UID is say 123, in the pants directory there could be 100 media files, I want to take the media file that matches the UID, i.e. UID = 123 so we take 123.jpg – Grinch91 – 2017-03-02T11:22:12.077

No answers