0

Here is the files list in a given directory;

a_object_1.0.zip
file_name_conf_1.1.zip
anim_dup_1.1.zip

Iterate over files in a given directory, and check if file matching the file pattern from the list of files, unzip and rename each file with some prefix(cx_a_object_1.0.zip, cx_file_name_conf_1.1.zip, anim_anim_dup_1.1.zip )

  • 2
    It's not clear what you're asking - please edit your question to provide more detail: What is the "file pattern from the list of files" -- do you mean that you have some additional files containing these patterns? After you unzip a file, do you want to rename the original .zip file, or the file(s) extracted from it? – JigglyNaga Sep 18 '18 at 14:15
  • @JigglyNaga sorry for the confusion; file pattern =file naming, and yes we need to rename to the original .zip file. – itgeek Sep 18 '18 at 17:08
  • Your comments have not made your question any clearer. What is the exact pattern that you want to match? What have you tried so far (including the answers below) and how was the result different to what you wanted? You may want to read How do I ask a good question? – JigglyNaga Sep 19 '18 at 6:48
1
#!/bin/bash
for szFile in /var/tmp/*.zip;
do 
  unzip "$szFile";
  mv "$szFile" "Prefix_"$(basename -- "$szFile")
done

will:

  • iterate over all zip files in /var/tmp
  • extract them
  • add a prefix
  • move them to the current directory.
  • Need to check with file exists, and matching with the given file name. – itgeek Sep 18 '18 at 14:39
  • @itgeek Welcome to Unix & Linux. Please edit your question and provide what you need exactly as JigglyNaga already said as the above answer answers your question including the comment you just left. Just test it out. – Fabby Sep 18 '18 at 14:44
0

Here is an example using find.

find somedir/ -type f -name 'some.zip' -exec bash -c 'name=$1; echo unzip "$name" -d "${name%/*}"; echo  mv "$name" "${name%%/*}/cx_c_${name##*/}"' _ {} \;

Remove echo when you are satisfied about the result

Or make a function with a local argument and pass the filename as argument to the function as follows:

cfind() {
 local name=$1
 find somedir/ -type f -name "$name" -exec bash -c 'name=$1; echo unzip "$name" -d "${name%/*}"; echo  mv "$name" "${name%%/*}/cx_c_${name##*/}"' _ {} \;
    }

Call it like

cfind myfilename.zip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.