How to Search for Files in Current Directory Ubuntu
This article details the methods for locating files strictly within the current directory level on Ubuntu. It focuses on using the terminal to perform non-recursive searches, ensuring subfolders are ignored. You will learn the specific flags and commands required to isolate files in your working directory efficiently.
Using the Find Command
The most reliable method is using the find utility with
the -maxdepth flag. This prevents the command from
descending into subdirectories. Open your terminal and navigate to the
target folder.
To search for a specific file name, use the following syntax:
find . -maxdepth 1 -name "filename"
Replace "filename" with the actual name or wildcard
pattern you are seeking. The dot (.) represents the current
directory, and -maxdepth 1 limits the search to this level
only. If you need a case-insensitive search, replace -name
with -iname.
Using LS and Grep
For a quicker, less complex search, you can combine ls
with grep. This lists the contents of the current directory
and filters the results based on your text input.
Execute the following command:
ls | grep "search_term"
This method displays only the files in the current folder that match
the text string. Note that this does not search hidden files by default.
To include hidden files, use
ls -a | grep "search_term".
Verifying Results
After running either command, review the output list. Any file path
returned should not contain slashes (/) indicating
subfolders if the commands were executed correctly. This confirms the
search remained confined to the current directory level.