【问题标题】:Command to locate a given directory定位给定目录的命令
【发布时间】:2012-05-16 05:09:57
【问题描述】:

是否有任何(unix)命令可以定位给定目录?

例如,我有一个名为“MyDir”的目录,但我不知道它在磁盘上的绝对路径。有什么命令可以提供 MyDir 的路径吗?

更具体地说,我想在 Java 程序中执行此操作(通过系统调用)。

// return the full path to the specified directory
// if there's more than once directory with the given name
// just return the first one.
static String findPath (String dirName)
{
     // CODE here

}

谢谢!

【问题讨论】:

    标签: java unix directory command


    【解决方案1】:

    如果它只是 Unix,你可以使用locate 命令。不过,不要忘记定期运行updatedb(最好是自动运行)。

    要在 Java 中实际运行命令行命令,请查看 this article。基本命令是Runtime#exec,但您需要进行一些错误检查。文章中提供的sn-p是:

    import java.io.*;
    
    public class JavaRunCommand {
    
        public static void main(String args[]) {
    
            String s = null;
    
            try {
    
            // run the Unix "ps -ef" command
                // using the Runtime exec method:
                Process p = Runtime.getRuntime().exec("ps -ef");
    
                BufferedReader stdInput = new BufferedReader(new 
                     InputStreamReader(p.getInputStream()));
    
                BufferedReader stdError = new BufferedReader(new 
                     InputStreamReader(p.getErrorStream()));
    
                // read the output from the command
                System.out.println("Here is the standard output of the command:\n");
                while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
                }
    
                // read any errors from the attempted command
                System.out.println("Here is the standard error of the command (if any):\n");
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                }
    
                System.exit(0);
            }
            catch (IOException e) {
                System.out.println("exception happened - here's what I know: ");
                e.printStackTrace();
                System.exit(-1);
            }
        }
    }
    

    否则,您可以Walk the File Tree(使用 NIO.2 的 Java 本机)。不过,这可能需要更长的时间,因为它没有被缓存。

    【讨论】:

    • 嗯,我可能选错了词,对我想做的事情产生了错误的印象。当我查看locate 的手册时,它似乎从某个数据库中找到了东西。但是我的程序需要在不依赖任何数据库的情况下运行。想象一下,当您打开搜索窗口(在 Windows 上,或在 Mac OS 上的 Finder)并进行搜索时。
    • locate 数据库可能由您的系统管理员维护(通过上述 updatedb 命令)。它是一个反映运行它的系统的文件系统的数据库。本质上,它与 Windows/Finder 搜索用于加速索引文件系统搜索的数据库相同。如果您不想依赖该索引,可以使用 find 命令 - 请参阅我的答案。
    【解决方案2】:

    locate 命令(如果可用)(某些系统可能未启用构建其索引)将对系统上所有世界可读目录中的文件路径执行子字符串匹配。

    【讨论】:

      【解决方案3】:

      作为定位命令的替代方法(例如,如果没有维护所需的数据库),您可以使用“find”命令:

      find / -type d -name Foo
      

      此调用将在文件系统上的“/”下的任何位置找到任何名为 Foo 的目录。请注意,这可能会非常非常慢 - 如果 'locate' 可用,它可能会执行得更好。

      【讨论】:

        【解决方案4】:

        确切的定位命令是,

        locate -r ~/".*"MyDir
        

        如果需要刷新数据库,

        sudo updatedb
        

        【讨论】:

          猜你喜欢
          • 2011-09-06
          • 1970-01-01
          • 1970-01-01
          • 2011-01-29
          • 2016-04-21
          • 2019-12-04
          • 1970-01-01
          • 2023-04-02
          • 2021-10-01
          相关资源
          最近更新 更多