Search for files in Linux with find
Introduction
find is a powerful tool for working with files.
With it you can set various compound conditions for further actions on files.
It is often used as the first step before copying, moving, deleting or changing files that match certain conditions.
Search
Simply find
find /usr/share/doc -name '*.pdf'
Find and copy straight into the current directory
find /usr/share/doc -name '*.pdf' -exec cp {} . \;
Find in the current directory
find -name '*.pdf'
Delete from the current directory
find -name '*.pdf' -delete
To find only files of a certain type run find with the type option.
For example, to find all links in the directory /etc
find /etc -type l
You can read more about files in Linux in the article «File types in Linux»
Nesting levels
Find all links only at the top nesting level
find /etc -maxdepth 1 -type l
Searching by file size
df -h /boot
Filesystem Size Used Avail Use% Mounted on /dev/sda1 1014M 194M 821M 20% /boot
Find regular files of a certain size
To find regular files you have to use
-type f
find /boot -size +20000k -type f
find: ‘/boot/efi/EFI/centos’: Permission denied find: ‘/boot/grub2’: Permission denied /boot/initramfs-0-rescue-389ee10be1b38d4281b9720fabd80a37.img /boot/initramfs-3.10.0-1160.el7.x86_64.img /boot/initramfs-3.10.0-1160.2.2.el7.x86_64.img
Files are of the following types:
- : regular file
d : directory
c : character device file
b : block device file
s : local socket file
p : named pipe
l : symbolic link
The details are in the article:
«Files in Linux»
find /boot -size +10000k -type f
find: ‘/boot/efi/EFI/centos’: Permission denied find: ‘/boot/grub2’: Permission denied /boot/initramfs-0-rescue-389ee10be1b38d4281b9720fabd80a37.img /boot/initramfs-3.10.0-1160.el7.x86_64.img /boot/initramfs-3.10.0-1160.el7.x86_64kdump.img /boot/initramfs-3.10.0-1160.2.2.el7.x86_64.img /boot/initramfs-3.10.0-1160.2.2.el7.x86_64kdump.img
The same plus show the size of the files
find /boot -size +10000k -type f -exec du -h {} \;
find: ‘/boot/efi/EFI/centos’: Permission denied find: ‘/boot/grub2’: Permission denied 60M /boot/initramfs-0-rescue-389ee10be1b38d4281b9720fabd80a37.img 21M /boot/initramfs-3.10.0-1160.el7.x86_64.img 13M /boot/initramfs-3.10.0-1160.el7.x86_64kdump.img 21M /boot/initramfs-3.10.0-1160.2.2.el7.x86_64.img 14M /boot/initramfs-3.10.0-1160.2.2.el7.x86_64kdump.img
Searching by the beginning of a file name
Note that in find, unlike grep , you do not have to put any symbols before the beginning of the name.
find -name 'topb*'
Searching by part of a file name
Find in the project topbicyle all directories with qa in the name
find topbicycle/ -name '*qa*' -type d
Move certain files but not directories
Move files that start with certain characters to another directory, but do not touch directories with similar names
find -maxdepth 1 -type f -name 'sub*' -exec mv {} subprocess \;
find + sed
You can apply straight to the results of the find command sed
find . -type f -name '*.php' -exec sed -i 's@python/old_file_name\.php@python/new_directory/new_name\.php@' {} \;
. means that we search in the current directory (and the nested ones)
This command replaces everywhere
python/old_file_name.php
with
python/new_directory/new_name.php
The next example
find . -type f -name '*.php' -exec sed -n '/\/i\/aws\/oauth.php/ p' {} \;
This command searches all .php files for the string /i/aws/oauth.php
If you do not need to find or replace a string but to show the name of the file that contains certain characters, it is convenient to use find + grep
Add to the function my_function an extra argument, straight in all .php files.
find -type f -name "*.php" -exec sed 's/my_function(\$old_arg1,\ \$old_arg2)/my_function(\$old_arg1,\ \$old_arg2,\ \$new_arg1/' {} \;
find -type f -name "*.php" -exec sed 's/my\_function(\$old\_arg1,\ \$old\_arg2)/my\_function(\$old\_arg1,\ \$old\_arg2,\ \$new\_arg1/' {} \;
find + grep
You can apply straight to the results of the find command grep
find . -type f -name '*.php' -exec grep -l '/aws/oauth.php' {} \;
This command searches all .php files for the string /i/aws/oauth.php and shows in which file this string occurs
Article author: Andrei Olegovich