• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

exec tar command from another directory

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to exec a tar command from a different directory.
I'm in this directory: /usr/plat/local/isa001

I want to exec following using the path: tar -xvf tarfile.tar /vtone/ndir/jsp/

It does not work.
 
Rancher
Posts: 4804
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean "does not work"? Do you get any errors?
are the file protections properly setup?
 
Ranch Hand
Posts: 256
Netbeans IDE Firefox Browser Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use tar -xvf tarfile.tar -C /vtone/ndir/jsp/

note:/vtone/ndir/jsp/ path needs to be existing one and
tarfile.tar should exist at local directory /usr/plat/local/isa001 from where you will execute the command.


Cheers!!

Ricky
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Ricky said... and if your directory doesn't already exist, you can do it all in one step (I'm assuming Bash, but similarly in other shells):That'll ensure the directory is created and that the extraction goes into it. If you know the directory already exists, you can just do the tar bit with the -C option, as stated previously.
 
Don Martino
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks so very much for the replies.
The tarfile.tar file does not reside in the local directory. How would I exec the tar command from my local directory if the tarfile.tar file resides in the other directory?
 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Netbeans IDE Firefox Browser Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use below command from your local directory i.e./usr/plat/local/isa001
tar -xvf /PATH_TO_TAR_FILE_LOCATION/file.tar -C /vtone/ndir/jsp/



Cheers!!

Ricky
 
Don Martino
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know if I should post here or in java forum so I posted in both.

Now, I want to use it in java using Runtime.exec(), but it is not actually unloading the tar file into the directory, yet it prints the names of the files on the console.

Here's my java code: Note:uploadFle is the tarfile.tar

public boolean unloadTarFle(String uploadFle){

boolean status = false;

int ch;
String changeDir = "tar -xvf /export/audio/work/isa001/"+uploadFle;
try{
Process myProcess = Runtime.getRuntime().exec(changeDir);
// Process myProcess = Runtime.getRuntime().exec(commands);
InputStreamReader myIStreamReader = new InputStreamReader
(myProcess.getInputStream());
while ((ch = myIStreamReader.read()) != -1)
{
System.out.print((char)ch);
}

}
catch(IOException anIOException) {
System.out.println(anIOException);
}

log.debug("MauFileUpdate::unloadTarFle()::Ended");
return status;
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Don Henderson:
I don't know if I should post here or in java forum so I posted in both.



The correct answer is to pick one, never both. Thanks. I deleted the other copy for you.

Should work; the files are going to go into the Java program's working directory (which may not be where you think it is; try System.out.println(System.getProperty("user.dir")) to find out what it is.)
 
Charles Lyons
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now, I want to use it in java using Runtime.exec()

If there is a better way, avoid using any functionality which interfaces with the native system (other than that provided through JNI by Sun). Not only does this make your application non-portable (e.g. it wouldn't work on Windows), but it also can introduce security vulnerabilities. This is the same reason why using such exec calls in native Linux programs is a bad idea---someone changes the environment variables and runs the program, and all of a sudden it does something you didn't expect and they've created an exploit. Particularly bad if the application is suid to root for some reason...

In your case, do you really need a Java program to do the work? In my experience, Java is very clunky. Great for platform independence, but slower than alternatives on any one specific platform. It loads at least a 20MB RAM image, usually much more, and is slow to load since the class loader imports all the core runtime files. If not, then perhaps a system-specific solution would be more appropriate---for example, a Bash script or even a C app.

If Java is what you need/want, then I'd suggest looking at Java APIs which can extract tars for you; here are a couple:

Java Tar
Ant (a collection of packaging tools)

Of course, nothing wrong with experimentation using Runtime's exec(), but it isn't ideal for production apps.
 
Any sufficiently advanced technology will be used as a cat toy. And this tiny ad contains a very small cat:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic