Conundrum - Flexible way to strip extension


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conundrum - Flexible way to strip extension
# 1  
Old 07-14-2012
Conundrum - Flexible way to strip extension

Hi,

First post here. I have something that may prove to be difficult.

I have the following files:

Code:
Example1.0.0.tar.gz
Example2.tar
Example3.zip
Example4.0.0.0.0.0.bzip2

I need to remove the file extensions and store as a variable so they look like this:

Code:
Example1.0.0
Example2
Example3
Example4.0.0.0.0.0

The way I see it, there are only two ways of doing it. First, is to check the name for certain terms, say ".tar",".gz",".zip" and then have it removed from the name. If this is the best way, is there an efficient and clean way to do this?

The second way I see, is that I get the name without the extension after I unzip it, then store as a variable. So, with my command: tar xzf $FILE_VAR, is there a way to grab the folder that is spat out?

Code:
$File_VAR = Example1.0.0.tar.gz
tar xzf $FILE_VAR 
<code to find out resulting folder name and store as variable>

Output = Example1.0.0

I would really appreciate any help and advice you can give,

Thanks,

James

---------- Post updated at 03:33 PM ---------- Previous update was at 03:26 PM ----------

Sorry, second post. The whole reason I want to do this is because I need to CD into the untarred directory without knowing what it is actually called.

Thing is because I cant get the name without the extension, I dont know the name of the folder that will be output from the untar operation, then I cant CD into it.

I dont want to specify the folder name that it will be untarred into because I have thousands of these to do.

Last edited by Scrutinizer; 07-14-2012 at 05:36 PM.. Reason: quote tags changed to code tags
# 2  
Old 07-14-2012
Personally I don't understand your post or the reasoning behind changing a filename of an archive from one which exists to one which does not exist.

Suggest looking at the -t parameter to tar which when used instead of the -x parameter just lists the files in the archive under their original paths. Thus you can predict the paths in advance.

Please post what Operating System and version you have and what Shell you use.
The space characters around the equals signs in the sample code are not normal syntax in mainstream unix Shell.
Is this pseudo-code (i.e. to explain the process)?
# 3  
Old 07-14-2012
Hi,

Thank you for the reply. This is the situation. Its all variable driven, but after I extract, I dont know what my folder will be called. I cant CD into it if I dont know what its called.

Code:
$DL_DIR = /opt/
$URL = http://nginx.org/download/nginx-1.3.3.tar.gz
$FILE=${URL##*/}
$CONFIG = "-- core"

cd "$DL_DIR"
wget $URL
tar xzf $FILE
cd <HOW DO I GO INTO IT?>
./configure "$CONFIG"
make
make install
rm $FILE

If this doesnt explain it please say. I really want to get past this problem but Im having a hard time explaining it.

Since I want this to function for any set of URL's which may have two formats like ".tar.gz" or one format ".zip" and may have .'s in the filename like "Python2.3.4" or may not "Nginx", it makes it a bit tricky.
# 4  
Old 07-15-2012
Take a look at the output from this tar command which just lists the contents of the archive.
Code:
tar tzf filename.tar.gz
# Or possilbly with the verbose switch
tar tvzf filename.tar.gz

If you run this and save the output, before running the tar xzf you have a file containing the name of the directory.
This is a general answer because we don't know anything about your Operating System or your Shell. All I can say is that the code posted is a syntax error in every Bourne-type Shell. There is much variation in the tar command.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Display the .csv extension files based on .done extension fine

Hi All, I want to fetch the files based on .done file and display the .csv files and Wil take .csv files for processing. 1.I need to display the .done files from the directory. 2.next i need to search for the .Csv files based on .done file.then move .csv files for the one directory ... (2 Replies)
Discussion started by: girija.g6
2 Replies

2. IP Networking

iptables conundrum

Ok, if youre reading this prepare yourself.(debian based os) so im trying to do this routing with ip tables, i need to forward/SNAT traffic from 192.168.111.1 to 10.10.10.250, the 192.x.x.x ips are being shoved into a honeyd like program called inetsim so its offline, 10.10.10.125 is connected... (3 Replies)
Discussion started by: Shocco
3 Replies

3. Shell Programming and Scripting

Bash command line to strip tar.gz file extension?

What's the command syntax for stripping out the tar.gz file extension in a bash command line (not script file). Thanks! prompt/> ls *.tar.gz | <what comes here?> (3 Replies)
Discussion started by: ZillaG
3 Replies

4. Shell Programming and Scripting

Perl:Regex for Search and Replace that has a flexible match

Hi, I'm trying to match the front and back of a sequence. It works when there is an exact match (obviously), but I need the regex to be more flexible. When we get strings of nucleotides sometimes their prefixes and suffixes aren't exact matches. Sometimes there will be an extra letter and... (2 Replies)
Discussion started by: jdilts
2 Replies

5. UNIX for Dummies Questions & Answers

umask conundrum

Hi All, i was reading up on a umask question on this forum and have a question on this. the umask value on my home PC running on cygwin is 022. when i create a dir it defaults to permission 755, when i create a file it defaults to 644. Now it starts at 777 for dirs and 666 for files and... (1 Reply)
Discussion started by: Irishboy24
1 Replies
Login or Register to Ask a Question