• Post Reply Bookmark Topic Watch Topic
  • New Topic

exec tar command from another directory  RSS feed

 
Don Martino
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • 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.
 
Pat Farrell
Rancher
Posts: 4686
7
Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean "does not work"? Do you get any errors?
are the file protections properly setup?
 
Ramakanta Sahoo
Ranch Hand
Posts: 256
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • 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
  • 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
Fedora Firefox Browser Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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;
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!