Most of the file operations are done with git grep
, and I have neglected the basics, so
I decided to look it up again.
find
As the name implies, find is a way to find files in a directory tree. The basic form is as follows.
$ find (Search start directory)(Search condition) (Action)
-name
$ find . -name file-1.txt -print
./file-1.txt
Search by name as the name suggests. However, if you use a wildcard (*?), Be sure to use single quotes or double quotes because it will be read by Bash variable expansion.
-type
$ find . -type d -print
As the name suggests, the search condition (-type) is also searched by type name.
The d
of-type d
is the file type, and in the above case, it is an instruction to find only the directory from the current position
.
It is also possible to use in combination as shown below. (-a is an AND search and can be omitted.)
$ find . -type f -a -name '*.txt' -print
Specify | File type |
---|---|
-type f | regular file |
-type d | directory |
-type l | symbolic link |
Currently all are displayed with -print
, but when displayed with -ls
, they are displayed in "ls" format.
find . -type f -a -name '*.txt' -ls
671143 0 -rw-rw-r-- 1 vagrant vagrant 0 Apr 19 04:29 ./find/file-1.txt
671215 0 -rw-rw-r-- 1 vagrant vagrant 0 Apr 19 04:29 ./find/file-3.txt
671213 0 -rw-rw-r-- 1 vagrant vagrant 0 Apr 19 04:29 ./find/file-2.txt
locate Unlike find, locate is searched from the DB where the file path is stored, so it can be searched faster than find.
It's not included in Linux from the beginning, so you have to install it.
$ locate --version
If it is not included, install it.
$ sudo yum install mlocate
$ sudo apt-get install mlocate
$ updatedb
The database will be created by the above ʻupdatedb` command, and the database will be updated at regular intervals.
Database updates are set to be updated once a day during installation. Therefore, it will be one day later that the file you just created will be caught in the search. Also, the file you just deleted will be caught in the search until one day later.
`locate is good for finding files some time after they were created (deleted). ``
$ locate bash
This will speed up all the files named bash.
Options | Effects |
---|---|
-i | Ignore case |
-b | Search only file names (ignore directories) |
-A | and search (usually or search) |
Recommended Posts