Find file and zip without including directory path


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find file and zip without including directory path
# 1  
Old 09-19-2012
Find file and zip without including directory path

Does anyone know of a way to zip the resulting file from a find command?
My approach below finds the file and zips the entire directory path, which is not what I need.
After scanning the web, it seems to be much easier to perform gzip, but unfortunately the approach must use zip.
Code:
find `$DIR` -name 'CONTACTS_*.txt' -exec zip '{}'.zip '{}' \; -delete


Last edited by Franklin52; 09-19-2012 at 04:58 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 09-19-2012
try with this....


Code:
find -type f -name "CONTACTS_*.txt" -exec zip  '{}'.zip '{}' \; -delete


Last edited by pamu; 09-19-2012 at 02:37 AM..
# 3  
Old 09-19-2012
Thanks for the quick reply, Pamu. Getting this error:
Code:
line 29: /my/dir: is a directory


Last edited by Franklin52; 09-19-2012 at 04:58 AM.. Reason: Please use code tags
# 4  
Old 09-19-2012
Quote:
Originally Posted by koeji
line 29: /my/dir: is a directory
I have tried this on my machine. works perfect here....

Code:
find -type f -name "*.txt" -exec zip  '{}'.zip '{}' \;

try giving your directory path
Code:
find /Directory_path/ -type f -name "*.txt" -exec zip  '{}'.zip '{}' \;

# 5  
Old 09-19-2012
While it does now zip by explicitly providing the directory path, when I unzip the file again as a check, I get ./path/to/directory/CONTACTS_1.txt. I should also mention that $DIR is in a different directory than from where the script is run.
# 6  
Old 09-19-2012
Quote:
Originally Posted by koeji
While it does now zip by explicitly providing the directory path, when I unzip the file again as a check, I get ./path/to/directory/CONTACTS_1.txt. I should also mention that $DIR is in a different directory than from where the script is run.
If you are providing directory path then you can run script anywhere you want.
But if you are not providing path then you should run your script where you want to run.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Convert Relative path to Absolute path, without changing directory to the file location.

Hello, I am creating a file with all the source folders included in my git branch, when i grep for the used source, i found source included as relative path instead of absolute path, how can convert relative path to absolute path without changing directory to that folder and using readlink -f ? ... (4 Replies)
Discussion started by: Sekhar419
4 Replies

2. Shell Programming and Scripting

Zip a file with path

zip /var/log/mylog.log mylog.1.log but its not working (5 Replies)
Discussion started by: Froob
5 Replies

3. Shell Programming and Scripting

Zip all the files including directories - subdirectories

Hi, Is is possible to zip a folder and all its contents within the folder ( including sub-directories and files) into a zip file? and can regain the same structure if unzipped? Thanks (6 Replies)
Discussion started by: rudoraj
6 Replies

4. Shell Programming and Scripting

Find all the files under a specific directory and zip them into a single file.

Hi, Is there a way to find all the files from a specific location and then zip them into a single file, even if they are in multiple directories? (3 Replies)
Discussion started by: rudoraj
3 Replies

5. UNIX for Advanced & Expert Users

Find exact path of a file/directory

Hello Folks, A wrapper takes an argument of file or directory name. I want to allow paths that reside within the current directory only. Can simply discard the paths like "/A" & "../" as they go outside the current by looking at the path beginning. How to validate this one: A/../../../b... (4 Replies)
Discussion started by: vibhor_agarwali
4 Replies

6. Linux

How to create zip file without path?

Hello, I have generated a PHP script that creates files needed for EPUB file. I have a temp directory where these files are created into and then needs to be zipped. The directory structure is: mimetype content.opf index.html stylesheet.css toc.ncx META-INF META-INF/container.xml ... (4 Replies)
Discussion started by: spaze
4 Replies

7. UNIX for Dummies Questions & Answers

Create zip without including directories

Hi guys, I'm trying to do the following: zip -r /tmp/foo.zip public/accounts/foo But the zip that's been made has the whole "public/accounts/foo" path. I want only the foo folder to be zipped. How can I do this? Thanks, Elías (2 Replies)
Discussion started by: elioncho
2 Replies

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

9. Shell Programming and Scripting

Reading a path (including ref to shell variable) from file

Hi! 1. I have a parameter file containing path to log files. For this example both paths are the same, one is stated directly and the second using env variables. /oracle/admin/orcl/bdump/:atlas:trc:N ${ORACLE_BASE}/admin/${ORACLE_SID}/bdump/:${ORACLE_SID}:trc:N 2. I try to parse the path... (1 Reply)
Discussion started by: lojzev
1 Replies

10. UNIX for Dummies Questions & Answers

find directory not including current

Using Solaris 8, I've forgotten how to exclude the current directory in the find results. find . -type d ! -name "*.CAP" I want every directory that does not match the *.CAP pattern, except the current directory. (2 Replies)
Discussion started by: dangral
2 Replies
Login or Register to Ask a Question