Bash to add unique prefix to extracted zip folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash to add unique prefix to extracted zip folder
# 1  
Old 03-22-2019
Bash to add unique prefix to extracted zip folder

In the bash below in each .zip there is a folder to be extracted Variants that I am trying to make unique by adding the prefix, before the _ from the .zip. The script does execute, but the prefix is not added to the extracted folder. Rather the Variants folder is added to each file within it. Thank you Smilie.

bash
Code:
for z in /home/cmccabe/tmp/*.zip; do ## start loop
    unzip "$z";  ## unzip all .zip files
    mv "$(unzip -Z1 $z)" "${z%%_*}";  ## grab prefix before _ and add to the extraced .zip
rm "$z"  ## remove original .zip
done  ## end processing


/home/cmccabe/tmp
Code:
19-0000-LastName-FirstName_v1_c91a684a-0a9c-4632-9b60-91f0d337f51d.zip --- top level ---
     Variants   --- folder to be extracted ---
19-0002-L-F_v1_d870b844-b72e-49c4-89c4-471f90dd8724.zip   --- top level --
     Variants   --- folder to be extracted ---

desired output --- zip extracted ---
Code:
     19-0000-LastName-FirstName-Variants   --- extracted folder w/ prefix ---
     19-0002-L-F-Variants   --- extracted folder w/ prefix ---

example after executing currently --- one of 5 files, all extated in the same way ---
Code:
 inflating: Variants/19-0000-LastName-FirstName_v1/19-0000-LastName-FirstName_v1_Non-Filtered_2019-03-21_08:12:52-xxxx.tsv


Last edited by cmccabe; 03-23-2019 at 11:40 AM..
# 2  
Old 04-03-2019
Hello cmccabe,

I think that you are trying to do this in a single step, but actually you need to have more. Your attempt also has to be certain that you only have one file in each .zip but it's not clear if this is the case.

The problem is that unzip probably doesn't just give you the extracted filename(s) as output. You might well be better to make a temporary directory to extract the files to, then process all the files you find there. If the files are large, make the temporary directory in the same filesystem as the desired target. The default for mktemp is to make a directory with pattern /tmp/tmp.XXXXXXXX (might vary depending on OS) but you can set a template for the name to use so I have used it to set this all up in /data. Obviously change this to something meaningful for yourself.

There are a few variations on what you are asking for, but I've guessed at this one:-
Code:
target_dir=/data
for zipfile in /home/cmccabe/tmp/*.zip
do
   workdir=$(mktemp -d "${target_dir}"/tmp.XXXXX)                          # Creates the temporary directory and catches the name
   unzip -d "${workdir}" "${zipfile}"
   prefix="${zipfile%%_*}"                                                     # Grab the interesting bit for this input file
   for extracted_file in ${workdir}                                     
   do
      mv "${extracted_file}" "${target_dir}/${prefix}"                              # Move to desired location and rename in one step
   done
done

Does that do what you need or have I misunderstood your requirement?

I you are extracting multiple files, we might need to add a find into the loop to update the files safely.


Kind regards,
Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add coordinates to line of output extracted from input file

I am trying to compare/confirm the output of an script using the perl below, which does execute. However I can not seem to print $1 and $2 in each line of the input separated by a tab in each line of the output. The input file is quite large so I have only included a portion, but the format is the... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

Bash to add folder to exsisting folders in directory

I am trying to create subdirectories in each folder in /home/cmccabe/Desktop/NGS/test/*. When I use echo I can see each folder in the directory, but I can not seem to add the commented out portion in bold. These are the sub-directories and sub-folders I am trying to add to each folder in... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Find all images, append unique prefix to name and move to different directory

Hi, I have a directory with Multiple subdirectories and 1000s of pictures (jpg) in each directory. The problem is that each directory has a 001.jpg in them. I want to append a unique name (the directory_name)would be fine. and then move them to one main backup directory once they have been... (1 Reply)
Discussion started by: kmaq7621
1 Replies

4. UNIX and Linux Applications

Zip command for large folder

I am trying to zip a folder, its size is more than 3 GB. I used below command zip -r foo middleware -x "*path*" This command fails every time with message "Filesize exceeded limit" Can some one help me how to over come this problem and zip the entire folder without splitting it. ... (2 Replies)
Discussion started by: ariram
2 Replies

5. UNIX for Dummies Questions & Answers

isolate unique files in a folder

Hello, I have two folders. One has 1183 text files (folder A). The other (folder B) has 1160 of those 1183 files (the contents in these 1160 files are identical to the contents in the corresponding files in the other folder, but the names of the files in this folder are completely different... (2 Replies)
Discussion started by: juliette salexa
2 Replies

6. UNIX for Dummies Questions & Answers

Zip command (zip folder doesn't include a folder of the same name)

Hi guys, I have a question about the zip command. Right now I have a directory with some files and folders on it that I want to compress. When I run the zip command: zip foo -r I am getting a foo.zip file that once I unzip it contains a foo folder. I want to create the foo.zip, but that... (1 Reply)
Discussion started by: elioncho
1 Replies

7. Windows & DOS: Issues & Discussions

How can I upload a zip folder on a unix path from my windows folder?

Hello, I am an amature at UNIX commands and functionality. Please could you all assist me by replying to my below mentioned querry : How can I upload a zip folder on a unix path from my windows folder? Thanks guys Cheers (2 Replies)
Discussion started by: ajit.yadav83
2 Replies

8. Shell Programming and Scripting

Zip the folder

i want the scripts or unix command to zip the folder. (3 Replies)
Discussion started by: kingganesh04
3 Replies

9. UNIX for Dummies Questions & Answers

Zip a folder including its sub-folders.

Hi, I have a folder that contains a few sub-folders. I would like to zip that folder and KEEP the subfolders. What it does at the moment is taking all the files within the subfolders and zipping them into one big file (zip -r ...). Does anyone know the UNIX command to keep the subfolders in the... (3 Replies)
Discussion started by: gdog
3 Replies

10. UNIX for Dummies Questions & Answers

zip a folder

is it possible to zip a whole folder instead of each file individually? (3 Replies)
Discussion started by: rprules
3 Replies
Login or Register to Ask a Question