Conundrum - Flexible way to strip extension

Hi,

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

I have the following files:

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:

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?

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

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)?

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.

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

Take a look at the output from this tar command which just lists the contents of the archive.

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.