Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-26-2012
Registered User
 
Join Date: May 2011
Posts: 39
Thanks: 6
Thanked 0 Times in 0 Posts
gzip of a file in the same folder

I want gzip of a file in the same folder where it is kept now
$filename = '/var/dev/test.txt'
/opt/home/>>
Code:
gzip -c $filename > test.txt.gz

however command creates it in the folder in /opt/home/
How to gzip a file in the same directory where it is now , no matter from where we execute and also the file path and folder is selected from variable
and hence cant hardcode in gzip command
Sponsored Links
    #2  
Old 04-26-2012
Registered User
 
Join Date: Apr 2012
Posts: 39
Thanks: 0
Thanked 6 Times in 6 Posts
Use regex to extract the path (everything to the left of the last slash) from $filename into $filepath, then use that in your command:


Code:
gzip -c $filename > $filepath/test.txt.gz


Last edited by Scrutinizer; 04-26-2012 at 09:02 AM.. Reason: code tags
Sponsored Links
    #3  
Old 04-26-2012
neutronscott's Avatar
script kiddie
 
Join Date: Jun 2011
Location: Charleston, SC
Posts: 649
Thanks: 18
Thanked 189 Times in 179 Posts
since the path is already given in the filename, and you are only adding .gz suffix, you shouldn't have to strip the path to add it back again...


Code:
gzip -c "$filename" > "$filename.gz"

this way /var/dev/test.txt goes to /var/dev/test.txt.gz
Sponsored Links
Closed Thread

Tags
gzip a file in same folder, parameter expansion

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
gzip vs pipe gzip: produce different file size hanfresco UNIX for Advanced & Expert Users 1 06-03-2011 10:48 PM
gzip a file and append creation date stamp to file jacktravine Solaris 3 09-22-2009 10:09 AM
File Management: How do I move all JPGS in a folder structure to a single folder? guptaxpn Shell Programming and Scripting 4 06-11-2009 02:41 AM
Parse the .txt file for folder name and FTP to the corrsponding folder. MeganP Shell Programming and Scripting 3 07-03-2007 01:54 PM
How do I send a file as an attachment (gzip file) on a Unix system lacca UNIX for Dummies Questions & Answers 3 07-03-2002 01:04 PM



All times are GMT -4. The time now is 05:39 PM.