Results 1 to 2 of 2
I have a problem I am not sure if there is even a way to solve this from the command line. I work for a company and we receive files ...
- 02-23-2009 #1Just Joined!
- Join Date
- Feb 2009
- Posts
- 1
Rename files while Unzipping
I have a problem I am not sure if there is even a way to solve this from the command line. I work for a company and we receive files from clients they usualy send tar files. When you extract the tar file it will have as many as 50 zip files within the tar file. I will write a basic for loop in order to unzip all of these. The problem is, is that some how the client manages to zip several files with the same name into each zip file even though they are not the same file so when unzipping the files I have to sit there and rename hundreds of files as they are unzipped. Does anyone know if there is a way from the command line to unzip these files and rename them as they are unzipped like with a _1.pdf, _2.pdf, _3.pdf . . . . . This may not be possible but it would be a huge help if anyone had any ideas.
- 02-23-2009 #2Just Joined!
- Join Date
- Feb 2009
- Posts
- 45
The GNU «cp» and «mv» commandsʼ CLI includes the --backup switch that offers the functionality you might be asking for. To put it simply, the result of the commands (assuming the source-files exist and the current directory is empty)
Originally Posted by oklahomabigdog
would be:Code:mv "$dir1/file" . --backup=numbered; mv "$dir2/file" . --backup=numbered; mv "$dir3/file" . --backup=numbered;
Thus a script that helped you to achieve your goal might look like the following one:Code:$> ls -1 file file.~1~ file.~2~
However this approach does not conserve file endings (as a “~$NUMBER~” is put behind the file name) so you might need to append a rename script (using «sed» or the likes) to better suit the filesʼ names to your needs.Code:#! /bin/bash tarfile="TARFILENAME"; tar -xf "$tarfile"; for zipfile in *.zip; do mkdir "$zipfile.dir"; unzip "$zipfile" -d "$zipfile.dir"; mv "$zipfile.dir/"* . --backup=numbered; rmdir "$zipfile.dir"; done


Reply With Quote