Last active
August 29, 2015 13:56
-
-
Save zambon/9140388 to your computer and use it in GitHub Desktop.
Finding an arbitrary user's home directory in Linux/UNIX
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Usage: | |
javac TextExec.java | |
java TestExec | |
Output: | |
/home/username |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class TestExec { | |
public static void main(String[] args) { | |
try { | |
// Replace 'username' with the actual username | |
String[] cmd = {"/bin/sh", "-c", "echo ~username"}; | |
Process p = Runtime.getRuntime().exec(cmd); | |
BufferedReader in = new BufferedReader( | |
new InputStreamReader(p.getInputStream())); | |
String line = null; | |
while ((line = in.readLine()) != null) { | |
System.out.println(line); | |
} | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment