Facebook Twitter Instagram
    WiredRevolution.com
    • Home
    • About
    • Contact Us
    • Essential Linux Commands
    • Sitemap
    Facebook Twitter Instagram
    WiredRevolution.com
    commands

    Search for files with the find command

    RyanBy RyanOctober 20, 2008Updated:December 1, 20083 Mins Read
    Facebook Twitter Pinterest LinkedIn Reddit WhatsApp Email

    The find command allows you to recursively search and locate files on your system based on specific criteria. You can search by name, owner, group, type, permissions, date, as well as many others.

    The find command uses the following format:

    find [search_path(s)] [search_criteria]
    

    The following is the most basic way to run the find command. It will list every file and directory within your current working directory.

    $ find
    

    This produces the same output but you are specifically listing the current directory as the search path.

    $ find .
    

    To search the entire filesystem, specify root as the search path (this could take a long time).

    $ find /
    

    You may want to redirect stderr to “/dev/null” to separate the valid output from the errors you might encounter from searching in directories where you don’t have the correct permissions.

    $ find / 2> /dev/null
    

    To search for a file with a specific name. This will find all files named “myfile” within your home directory.

    $ find ~/ -name myfile
    

    This will find all files with a “.jpg” extension in your home directory. The double quotes are needed so the shell will not expand the wildcards in the search string before it passes it to the find command.

    $ find ~/ -name "*.jpg"
    

    You can limit the depth level of the search as well. Set the value to 1 to search within the specified directory without recursing into any subdirectories. Raising the value will extend the search into that many directory levels.

    $ find ~/ -name "*.jpg" -maxdepth 1
    

    Search for files by permissions. This will find all files in your home directory that have rwx permissions for everyone.

    $ find ~/ -perm 777
    

    Search for executables.

    $ find ~/ -executable
    

    Search for files in your home directory which have been modified in the last twenty-four hours.

    $ find ~/ -mtime 0
    

    Search for files that are owned by the user “ryan”.

    $ find / -user ryan
    

    The find command is great for locating files but you can also execute commands on those files that it finds. To execute commands use the ‘-exec‘ option. The command below will perform an ‘ls -l‘ on each jpeg file it finds in your home directory.

    $ find ~/ -name "*.jpg" -exec ls -l {} \;
    

    The ‘-ok‘ option is similar to ‘-exec‘ but it will prompt you for confirmation before executing each command. This is helpful when you want to execute a potentially dangerous command such as removing files with rm like the example below.

    $ find ~/ -name "*.jpg" -ok rm {} \;
    
    < rm ... /home/ryan/pic1.jpg > ? n
    < rm ... /home/ryan/pic2.jpg > ? n
    < rm ... /home/ryan/pic3.jpg > ? n
    
    Share. Facebook Twitter Pinterest LinkedIn WhatsApp Reddit Email
    Previous ArticleCustomize the BASH PS1 command prompt
    Next Article How to correctly use LD_LIBRARY_PATH

    Related Posts

    Google Chrome Text Highlight Searching and Navigation

    Find the exit status of a previous command in Bash

    Find the parent PID of a Bash Script

    Most Commented
    March 12, 2009

    Fix blue tinted video in Ubuntu

    September 10, 2010

    Setup SSH access between VirtualBox Host and Guest VMs

    March 8, 2011

    Install GNOME Shell in Ubuntu 10.10 Maverick

    April 4, 2009

    Setup the PS3 Bluetooth Controller on Ubuntu

    October 22, 2008

    How to correctly use LD_LIBRARY_PATH

    Recent Comments
    • Execute command on linux virtual machine (or server) from windows commandline on Setup SSH access between VirtualBox Host and Guest VMs
    • Solved: How to SSH to a VirtualBox guest externally through a host? - Daily Developer Blog on Setup SSH access between VirtualBox Host and Guest VMs
    • How to SSH to a VirtualBox guest externally through a host? [closed] – Code D3 on Setup SSH access between VirtualBox Host and Guest VMs
    • How to copy and paste from VirtualBox? [duplicate] on Setup SSH access between VirtualBox Host and Guest VMs
    • Jackie Laguna on Fix OpenGL: ChoosePixelFormat SketchUp error in WINE
    © 2025 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.