Sponsored Content
Top Forums Shell Programming and Scripting Bash to add unique prefix to extracted zip folder Post 303033334 by rbatte1 on Wednesday 3rd of April 2019 12:02:00 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
zipnote(1)						      General Commands Manual							zipnote(1)

NAME
zipnote - write the comments in zipfile to stdout, edit comments and rename files in zipfile SYNOPSIS
zipnote [-w] [-b path] [-h] [-v] [-L] zipfile ARGUMENTS
zipfile Zipfile to read comments from or edit. OPTIONS
-w Write comments to a zipfile from stdin (see below). -b path Use path for the temporary zip file. -h Show a short help. -v Show version information. -L Show software license. DESCRIPTION
zipnote writes the comments in a zipfile to stdout. This is the default mode. A second mode allows updating the comments in a zipfile as well as allows changing the names of the files in the zipfile. These modes are described below. EXAMPLES
To write all comments in a zipfile to stdout use for example zipnote foo.zip > foo.tmp This writes all comments in the zipfile foo.zip to the file foo.tmp in a specific format. If desired, this file can then be edited to change the comments and then used to update the zipfile. zipnote -w foo.zip < foo.tmp The names of the files in the zipfile can also be changed in this way. This is done by following lines like "@ name" in the created temporary file (called foo.tmp here) with lines like "@=newname" and then using the -w option as above. BUGS
The temporary file format is rather specific and zipnote is rather picky about it. It should be easier to change file names in a script. Does not yet support large (> 2 GB) or split archives. SEE ALSO
zip(1), unzip(1) AUTHOR
Info-ZIP v3.0 of 8 May 2008 zipnote(1)
All times are GMT -4. The time now is 07:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy