Published Mar 30, 2020 - Author: zgxsin
Today I talk about how to locate and search files in Linux system. There are different commands to do that. I will discuss the most used ones .
The syntax of locate command is:
locate [OPTION] ... PATTERN...
It searches through a prebuilt local database of all files on the file-system generated by the updatedb command. Note it is essential to update the database as recent files saved over a period of fewer than 24 hours are not updated into the database by default and the database is updated once within 24 hours span. So the best practice to use locate is to run sudo updatedb && locate filename
.
In the terminal, run
echo "test locate" >> tmp_test.txt
This command does two things: create a file named tmp_test.txt
and write "test locate" into this file. Then run
sudo updatedb locate tmp_test.txt
which command in Linux is a command which is used to locate the executable file by searching it in the $PATH environment variable ((https://www.geeksforgeeks.org/which-command-in-linux-with-examples/)). If you want to change PATH variable, you can run:
# Only change PATH temporarily in current shell. $ export PATH="$PATH:/path/to/dir"
You can type info which
to check the help information for which command.
In the terminal, type which python cpp
:
And you will get
whereis is a command which only finds files in certain directories in the system.
guzhou@guzhou-laptop:~/tmp$ whereis ifconfig ifconfig: /sbin/ifconfig /usr/share/man/man8/ifconfig.8.gz guzhou@guzhou-laptop:~/tmp$ whereis --help Usage: whereis [options] [-BMS <dir>... -f] <name> Locate the binary, source, and manual-page files for a command. Options: -b search only for binaries -B <dirs> define binaries lookup path -m search only for manuals and infos -M <dirs> define man and info lookup path -s search only for sources -S <dirs> define sources lookup path -f terminate <dirs> argument list -u search for unusual entries -l output effective lookup paths -h, --help display this help -V, --version display version For more details see whereis(1).
The find command in UNIX is a command line utility for walking a file hierarchy rooted at each given starting-point. If no starting point is specified, '.' is assumed. It can be used to find files and directories and perform subsequent operations on them. It supports searching by file, folder, name, creation date, modification date, owner and permissions. By using the ‘-exec’, other UNIX commands can be executed on files or folders found ((https://www.geeksforgeeks.org/find-command-in-linux-with-examples/)). The syntax of find command ((https://man7.org/linux/man-pages/man1/find.1.html)) is:
find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]
The [expression] is a kind of query specification describing how we match files and what we do with the files that were matched. An expression is composed of a sequence of things. You need to specify finding patterns and what to find. I will take about them with the examples.
To start with, you need to first install the tool tree by
sudo apt install tree
I create a folder to do these examples.
# .: search in current directory # -name: search for files that are specified by ‘-name’. find . -name sample.txt
# .: search in current directory. # -name: search for files that are specified by ‘-name’. # Note that you need to quote the pattern. find . -name "*.txt"
# This command find all empty folders and files in the starting directory and its sub-directories. find . -empty
First we check the file permissions in our current directory:
The run
# -perm: specify the file/dir permission pattern find . -perm 755
and we will get the directories and files with permission 755. To associate permission number with ''r,w,x", you can use this tool.
Running
# Here -type f specify only looking for files. find . -type f -perm 755
gives you
find . -type f -name "*.txt" -exec grep "test" {} \;
This will give us the text containing "test" in all files in current directory (recursive) with a ".txt" suffix. Here we use -exec
:
-exec
Execute command; true if 0 status is returned. All following
arguments to find are taken to be arguments to the command
until an argument consisting of ';' is encountered. The
string '{}' is replaced by the current file name being
processed everywhere it occurs in the arguments to the
command, not just in arguments where it is alone, as in some
versions of find. Both of these constructions might need to
be escaped (with a `\') or quoted to protect them from
expansion by the shell. The specified command is run
once for each matched file. The command is executed in the
starting directory. There are unavoidable security problems
surrounding use of the -exec action; you should use the
-execdir option instead.
[thumbs-rating-buttons]