Renaming unzipped files with name of archive


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Renaming unzipped files with name of archive
# 1  
Old 09-19-2013
Renaming unzipped files with name of archive

I am attempting to rename files within a zipped archive with the beginning of the name of the zip file. For example unzip AAA_000.zip and rename file1.csv, file2.txt to AAA_file1.csv, AAA_file2.txt.

I am able to do this for a zip file with one file inside, but not for multiple files. This is the code I am using:
Code:
for i in *.zip
do 
n=$(unzip -lqq $i | awk '{print $NF}')
e=${n#*.}
unzip $i && mv $n ${i%%_*}".$e"
done

This is the error I get with multiple files inside the zipped archive:
mv: target `AAA_file1.csv\file2.txt' is not a directory

Thank you.
# 2  
Old 09-19-2013
You could try something like this:

Code:
for i in *.zip
do
    cmd=$(unzip -lqq $i | awk -v z=${i%%_*} '{ print "mv " $NF " " z "_" $NF }')
    unzip $i && eval "$cmd"
done

Not if you have possibility of files in sub-folders you may need this:

Code:
for i in *.zip
do
        cmd=$(unzip -lqq $i | awk -v z=${i%%_*} '
        !/\/$/ && length {
           p=f=o=$NF
           gsub("[^/]*/", "",f)
           gsub(f"$","",p)
           print "mv "o " " p z "_" f
        }')
    unzip $i && eval "$cmd"
done

It's a little more complex because of support for files being in sub-directories.
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 09-20-2013
This did it! Thank you! My case was the first, but seeing both helps me understand how this works.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check files and archive the files using sftp

Hi, I have to check the files in another server using sftp to do that, below is the code i am going with #!/bin/bash export SRC_FOLDER=$1 export ARC_FOLDER=$2 HOST=as07u3456 USER=relfag sftp ${USER}@${HOST} <<EOF cd $SRC_FOLDER/DSCOR ls bye EOF echo "done" whatever the files i... (8 Replies)
Discussion started by: ursrami
8 Replies

2. UNIX for Dummies Questions & Answers

Adding unzipped files to a Zip

Hey all, I have a zip file which I received, and I need to replace one of the files inside of it. I tried the obvious solution of unzipping the zip, replacing the file, and rezipping, but the following happened: Original Zip Size: 79MB Unzipped Size 80MB New Zip: 36MB When I feed the... (2 Replies)
Discussion started by: DeanLeitersdorf
2 Replies

3. Shell Programming and Scripting

Renaming multiple files in sftp server in a get files script

Hi, In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this? I am using #!/bin/ksh For eg: sftp user@host <<EOF cd /path get *.txt rename *.txt *.txt.done ... (7 Replies)
Discussion started by: jhilmil
7 Replies

4. Shell Programming and Scripting

Append timestamp create .trg file for all content of an unzipped archive

Hi, I have a test.zip archive that contains test.zip --> (file_1.txt, file_2.txt , file_3.txt) I need to unzip the file like this, file_1_timestamp.txt file_1_timestamp.trg file_2_timestamp.txt file_2_timestamp.trg file_3_timestamp.txt file_3_timestamp.trg Could you please let me know... (7 Replies)
Discussion started by: Shandel
7 Replies

5. UNIX for Dummies Questions & Answers

How to archive old files from the recently added 10 files?

Hi there, I am very new to unix and having trouble with a fairly simple statement: cd /user ls -t -c1 | sed -ne '11,$p' | mv xargs archive/ What I want the code to do is sort all files in a directory by timestamp, select all of the files after the 10th file, and then move those files... (3 Replies)
Discussion started by: DSIReady
3 Replies

6. Shell Programming and Scripting

renaming files or adding a name in the beginning of all files in a folder

Hi All I have a folder that contains hundreds of file with a names 3.msa 4.msa 21.msa 6.msa 345.msa 456.msa 98.msa ... ... ... I need rename each of this file by adding "core_" in the begiining of each file such as core_3.msa core_4.msa core_21.msa (4 Replies)
Discussion started by: Lucky Ali
4 Replies

7. Shell Programming and Scripting

script for to take files from FTP server to UNIX server and Unzipped that files

script for to take files from FTP server to UNIX server and Unzipped that files (1 Reply)
Discussion started by: sunilamarnadh
1 Replies

8. UNIX for Dummies Questions & Answers

See the date of file after its been zipped and unzipped?

I have some log files that have been gzipped and then compressed using cpio. There are a number of log files that have been compressed to the one file. When I extract them the date of the file when doing an ls -la is today's date (the date I extracted them). Is there anyway to see the date... (3 Replies)
Discussion started by: Sepia
3 Replies

9. UNIX for Dummies Questions & Answers

command to get count in unzipped folder

Hi All, Is there is a way to do this in a single unix command I have unzipped some files into a directory .. I want to check the count of the files in the directory and also i need to check whether the files is having read permision or not.. I know it can be done by a script isthere... (4 Replies)
Discussion started by: arunkumar_mca
4 Replies

10. UNIX for Dummies Questions & Answers

zipped or unzipped file

Is there a way you can tell if a file is still zipped or it's unzipped I have a file called ssss.zip and I would like to know if this file is still zipped or if it's unzipped? I'm on IBM AIX/RS6000 (3 Replies)
Discussion started by: ted
3 Replies
Login or Register to Ask a Question