How can I find a file/directory that could be anywhere on linux command line?
"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.
Fast way to find file names in Linux and specify directory
The first /
in your command is the base directory from which find will begin searching. You can specify any directory you like, so if you know, for example, that program.c is somewhere in your home directory you could do find ~ -name 'program.c'
or if it's in, say, /usr/src do find /usr/src -name 'program.c'
That should help with both 1 and 2.
If you want a command that's not find that can be faster you can check out the mlocate stuff. If you've done a recent updatedb (or had cron do it for you overnight) you can do locate <pattern>
and it will show you everywhere that matches that pattern in a file/directory name, and that's usually quite fast.
How to get full path of a file?
Use readlink:
readlink -f file.txt
How can I find a specific file from a Linux terminal?
Find from root path find / -name "index.html"
Find from current path find . -name "index.html"
Equivalent of 'find' for finding directories in the linux terminal?
You can use find
for that plus the option -type d
(d
= only directory, f
would return only files).
Use -name "pattern"
to search case sensitive. pattern
can contain *
(any number of unknown characters, including none) or ?
(exactly one arbitrary character).
Use -iname "pattern"
to search case insensitive.
Example:
find /home -type d -iname "public*html"
will find public_html
, publichtml
or PublicHtml
anywhere under /home
Related:
- 25 simple examples of Linux find command
- 35 Practical Examples of Linux Find Command
Find all files with name containing string
Use find
:
find . -maxdepth 1 -name "*string*" -print
It will find all files in the current directory (delete maxdepth 1
if you want it recursive) containing "string" and will print it on the screen.
If you want to avoid file containing ':', you can type:
find . -maxdepth 1 -name "*string*" ! -name "*:*" -print
If you want to use grep
(but I think it's not necessary as far as you don't want to check file content) you can use:
ls | grep touch
But, I repeat, find
is a better and cleaner solution for your task.
Find files with a specific folder name anywhere in their path
To have FOO
anywhere in their full path name:
find . -type f -wholename "*FOO*"
additionally to end with .tif
and exclude *v.tif
:
find . -type f -wholename "*FOO*[^v].tif"
but the above one excludes the edge case of *FOO.tif
, so this is better:
find . -type f -wholename "*FOO*.tif" ! -name "*v.tif"
How to search for a file in the CentOS command line
Try this command:
find / -name file.look
how to search for a directory from the terminal in ubuntu
you can search for directory by using find
with flag -name
you should use
find /user -name "sdk" -type d
meaning find directories named sdk in or below the directory /user
or if you want to be case-insensitive
find /user -iname "sdk" -type d
Related Topics
Move Window Between Tmux Clients
How to Clean Caches Used by the Linux Kernel
Best Distributed Filesystem for Commodity Linux Storage Farm
How to Count Number of Unique Values of a Field in a Tab-Delimited Text File
Install Zsh Without Root Access
Mongo: Couldn't Connect to Server 127.0.0.1:27017 at Src/Mongo/Shell/Mongo.Js:145
Pass Parameter to an Awk Script File
Creating an Installer for Linux Application
How Does the Linux Kernel Determine the Order of _Init Calls
What Does the Mkdir -P Mean in a Script File
How to Delete All Files Older Than 3 Days When "Argument List Too Long"
How to Specify a Editor to Open Crontab File? "Export Editor=Vi" Does Not Work
How to Set Nginx Max Open Files
Difference Between Vm.Dirty_Ratio and Vm.Dirty_Background_Ratio
What Is the Advantage of Using Supervisord Over Monit
How to Write a Linux Daemon with .Net Core
/Lib64/Ld-Linux-X86-64.So.2: No Such File or Directory Error