A quick guide to the find command to find files and folders on the file system
Thisfind
Commands can be used to find files or folders that match a specific search pattern. It searches recursively.
Let us learn it by example.
Find all files with the following content under the current tree:.js
Extend the name and print the relative path of each matching file:
find . -name '*.js'
It is important to use quotes around special characters (for example)*
Avoid the shell interpreting them.
Find a directory matching the name "src" under the current tree:
find . -type d -name src
use-type f
Search files only, or-type l
Search only for symbolic links.
-name
case sensitive. use-iname
Perform a case-insensitive search.
You can search under multiple root trees:
find folder1 folder2 -name filename.txt
Find the directory that matches the name "node_modules" or "public" under the current tree:
find . -type d -name node_modules -or -name public
You can also exclude the path using the following method-not -path
:
find . -type d -name '*.md' -not -path 'node_modules/*'
You can search for files containing more than 100 characters (bytes):
find . -type f -size +100c
Search for files larger than 100KB but smaller than 1MB:
find . -type f -size +100k -size -1M
The search file was modified 3 days ago
find . -type f -mtime +3
Search for files edited in the last 24 hours
find . -type f -mtime -1
You can delete all files matching the search by adding the following:-delete
Options. This will delete all files edited in the last 24 hours:
find . -type f -mtime -1 -delete
You can execute commands for each search result. In this example, we runcat
Print file content:
find . -type f -exec cat {} \;
Note termination\;
.{}
Fill in with the file name during execution.
Download mine for freeLinux Command Manual
More cli tutorials:
- Bash shell
- Introduction to Bash Shell Script
- Fish shell
- Shell, monitor file content
- How to exit Vim
- UNIX editor
- UNIX file system commands
- Unix Shell Tutorial
- How to set an alias in macOS or Linux Shell
- Homemade practical guide
- How to fix XCRUN invalid active developer path error in MacOS
- Getting Started
- Introduction to Linux
- How to find the process that is using the port
- Linux command: mkdir
- Linux command: cd
- Linux command: pwd
- Linux command: rmdir
- Linux command: ls
- Linux command: mv
- Linux command: cp
- Linux commands: less
- Linux command: tail
- Linux command: touch
- Linux command: cat
- Linux command: find
- Linux command: ln
- Linux command: ps
- Linux command: echo
- Linux command: top
- Linux command: kill
- Linux command: killall
- Linux command: alias
- Linux command: job
- Linux command: bg
- Linux command: fg
- Linux command: Type
- Linux command: where
- Linux command: whoami
- Linux command: who
- Linux command: clear
- Linux command: su
- Linux command: sudo
- Linux command: chown
- Linux command: chmod
- Linux command: passwd
- Linux command: open
- Linux command: wc
- Linux commands: history
- Linux command: du
- Linux command: umask
- Linux command: grep
- Linux command: man
- Linux command: uname
- Linux commands: sort
- Linux command: uniq
- Linux command: diff
- Linux command: nohup
- Linux command: df
- Linux command: xargs
- Linux command: gzip
- Linux command: gunzip
- Linux command: ping
- Linux command: traceroute
- Linux command: tar
- Linux command: export
- Linux command: crontab
- Linux command: dirname
- Linux command: base name
- Linux command: printenv
- Linux command: env
- A short guide to the ed editor
- vim short guide
- A brief guide to emacs
- A brief guide to Nano
- Linux, no space left on the device
- How to use Netcat