Skip to content

Latest commit

 

History

History
142 lines (123 loc) · 3.85 KB

04.find.files.directories.md

File metadata and controls

142 lines (123 loc) · 3.85 KB

Basic

Find files and directories with locate

Find files that contain the string file2 in their name:

user@ubuntu:/$ locate file2
/home/user/Documents/file2.txt
/home/user/Documents/file2_sorted.txt
/home/user/Documents/file2_sorted_uniq.txt

The command locate queries a database containing all list of files and directories in the system. This database is updated periodically, so when we try to locate new files, we might not find them.
In this case, we can update the database manually using the command updatedb before searching:

user@ubuntu:~$ sudo updatedb

Find files and directories using find

Find the file file2.txt in the directory Documents/:

user@ubuntu:~$ find Documents/ -name "file2.txt"
Documents/file2.txt

Ignore the case (uppercase/lowercase) when searching for a file:

user@ubuntu:~$ find Documents/ -iname "file2.txt"
Documents/File2.TXT
Documents/File2.txt
Documents/file2.txt

PS : By default, find search for both files and directories.

Advanced

Advanced searching with find

Search for files with extension .txt in the current directory:

user@ubuntu:~$ find . -iname "*.txt"        
./Documents/file2_sorted_uniq.txt
./Documents/File2.TXT
./Documents/file2_sorted.txt
./Documents/numbers.txt
./Documents/File2.txt
./Documents/file2.txt
./Documents/file.txt

Search for files that contains the string pic in their name:

user@ubuntu:~$ find . -iname "*pic*" 
./Pictures
./Pictures/picture.png
./Pictures/picture2.png

Search for directories only:

user@ubuntu:~$ find . -iname "*pic*" -type d
./Pictures

-type d can be replaced by -type f to search for files only, or -type l to search for links only.

Search for files/dirs modified during the previous 30 days:

user@ubuntu:~$ find . -mtime -30
.
./.viminfo
./.bash_history
./.vim
./.vim/.netrwhist
./Documents
./Documents/file2_sorted_uniq.txt
./Documents/File2.TXT
./Documents/file2_sorted.txt
./Documents/numbers.txt
./Documents/File2.txt
./Documents/file2.txt
./Documents/file.txt
./Pictures

Search for files/dirs that were modified before 30 days ago:

user@ubuntu:~$ find . -mtime +30
./.bashrc
./.profile
./Desktop
./.bash_logout
./Templates
./Music
./Videos
./.sudo_as_admin_successful
./Pictures/picture.png
./Pictures/picture2.png
./Downloads
./Public

To search using files/dirs using access time instead of modification time, use -atime instead of -mtime.

Search for files that are heavier than 50Mb:

user@ubuntu:~$ find . -size +50M
./Videos/video.mkv

We can remplace M (Megabyte) by k (Kilobyte) or G (Gigabyte).

Perform actions on found files with find

Delete all empty files in the current directory:

user@ubuntu:~$ find . -size 0 -delete -print
./Documents/File2.TXT
./Documents/File2.txt

Print found files using ls -dils format:

user@ubuntu:~$ find Documents/ -iname "file2.txt" -ls
 12190591      0 -rw-r--r--   1 user     user            0 Aug 26 17:04 Documents/File2.TXT
 12190598      0 -rw-r--r--   1 user     user            0 Aug 26 17:03 Documents/File2.txt
 12190588      4 -rw-r--r--   1 user     user           77 Aug 26 17:03 Documents/file2.txt

Execute a command on found files. In this example, we will move all .jpg files in the current directory to the directory Pictures:

user@ubuntu:~$ find . -iname '*.jpg' -print -exec mv {} Pictures/ \;
./Videos/pic2.jpg
./Videos/pic.jpg