How can I find a file/directory that could be anyw

2019-03-17 02:54发布

Ideally, I would be able to use a program like

find [file or directory name]

to report the paths with matching filenames/directories. Unfortunately this seems to only check the current directory, not the entire folder.

I've also tried locate and which, but none find the file, even though I know its on the computer somewhere.

5条回答
混吃等死
2楼-- · 2019-03-17 03:04

The find command will take long time, the fastest way to search for file is using locate command, which looks for file names (and path) in a indexed database (updated by command updatedb).

The result will appear immediately with a simple command:

locate {file-name-or-path}

If the command is not found, you need to install mlocate package and run updatedb command first to prepare the search database for the first time.

More detail here: https://medium.com/@thucnc/the-fastest-way-to-find-files-by-filename-mlocate-locate-commands-55bf40b297ab

查看更多
SAY GOODBYE
3楼-- · 2019-03-17 03:12

"Unfortunately this seems to only check the current directory, not the entire folder". Presumably you mean it doesn't look in subdirectories. To fix this, use find -name "filename"

If the file in question is not in the current working directory, you can search your entire machine via

find / -name "filename"

This also works with stuff like find / -name "*.pdf", etc. Sometimes I like to pipe that into a grep statement as well (since, on my machine at least, it highlights the results), so I end up with something like

find / -name "*star*wars*" | grep star

Doing this or a similar method just helps me instantly find the filename and recognize if it is in fact the file I am looking for.

查看更多
forever°为你锁心
4楼-- · 2019-03-17 03:16

To get rid of permission errors (and such), you can redirect stderr to nowhere

find / -name "something" 2>/dev/null
查看更多
做个烂人
5楼-- · 2019-03-17 03:28

Below example will help to find the specific folder in the current directory. This example only search current direct and it'll search sub directory available in the current directory

#!/bin/bash

result=$(ls -d operational)

echo $result

test="operational"

if [ "$result" == "$test" ] 
then 
   echo "TRUE"
else
   echo "FALSE"
fi
查看更多
别忘想泡老子
6楼-- · 2019-03-17 03:29

If need to find nested in some dirs:

find / -type f -wholename "*dirname/filename"

Or connected dirs:

find / -type d -wholename "*foo/bar"
查看更多
登录 后发表回答