How to Search for Files on a FreeBSD System

For those of you who are used to Windows and are just starting out with FreeBSD, you will definitely have difficulty finding files or programs. How can you not be confused, the FreeBSD server has no images, only text and pictures. This is different from Windows which has a GUI/image display that can make things easier for users.

Don't worry, this article will guide you in learning how to search for files and how to search for programs on the FreeBSD ports system.

1. which

The which utility allows you to search for programs in the directory specified in the PATH (path) environment variable. In general, the PATH variable includes directories, program calls that allow simple instructions from command names. In other words, programs called from the command line are first searched through this directory.

Searching with the which command is very fast. If you want to find out where the program is it is better to use the which command. Below is cpntph usage which.

root@ns1:~ # which cp

The script above will search for the "cp" program which is usually used to copy files or directories. Search results show the "cp" program is in the /bin directory.

root@ns1:/bin # which gmake

The description of the script above is used to search for the "gmake" program, and the "gmake" program is in the /usr/local/bin folder.

2. whereis

The whereis command is used to find the source/binary file location of commands and manual sections for a particular file on a FreeBSD system. If we compare whereis command with find command, they will look similar to each other as both can be used for the same purpose but whereis command produces more accurate results while taking less time comparatively. whereis does not require any root privileges to execute.

Below is an example of using the whereis program.

root@ns1:~ # whereis squid
squid: /usr/ports/www/squid

In the example script above it will search for a program called squid, after searching with the whereis command, the squid program is found in the /usr/ports/www folder.

root@ns1:~ # whereis cp
cp: /bin/cp /usr/share/man/man1/cp.1.gz /usr/ports/emulators/linux_base-c7/work/linux_base-c7-7.9.2009_1/usr/bin/cp

The script above is used to search for the "cp" program and the "cp" program is found in the /bin, /usr/share/man/man1, /usr/ports/emulators/linux_base-c7/work/linux_base-c7-7.9.2009_1 folder /usr/bin.

Another example of using the whereis command.

root@ns1:~ # whereis -s date
date: /usr/ports/devel/date

root@ns1:~ # whereis -b gunzip
gunzip: /usr/bin/gunzip

root@ns1:~ # whereis -B /bin -f ls gcc
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/ports/emulators/linux_base-c7/work/linux_base-c7-7.9.2009_1/usr/bin/ls
gcc: /usr/ports/lang/gcc

3. locate

The locate command is a Unix utility used to quickly find files and directories. The command is a more convenient and efficient alternative to the whereis command, which is more aggressive and takes longer to complete the search.

The following is an example of using the "locate" command.

root@ns1:~ # locate mysql

root@ns1:~ # locate mysql | less
tekan enter

root@ns1:~ # locate -c mysql

4. find

The find utility is used to recursively search for various files in a file system directory. The "find" command capability can select files or directories based on keys and parameters you specify. It works slower than other search commands, but its capabilities are amazing.

How to use the "find" command is a very basic but important FreeBSD lesson. The "find" command is very useful and can help your work. Find doesn't just look for certain files, but for example files or directories that match certain criteria such as size, permissions and type.

Although "find" does not have a special command to search the contents of a file, and there is no special key to search a file, but you can apply its construction.

In this short guide, we will use the "find" command with several examples. We will look for files or directories with a certain string in the name, for the type either file or directory. We'll also look for files that are larger, smaller, or within a certain size, but we'll also look for files with specific permissions.

Take a look at the following example to search for files.

root@ns1:~ # find / -type f -name "apache24"
root@ns1:~ # find / -type f -name "index.php"

Search directory

root@ns1:~ # find / -type d -name "mysql"
root@ns1:~ # find / -type d -name "phpmyadmin"
root@ns1:~ # find / -type d -perm -1000 -ls

Search for files in a specific directory.

root@ns1:~ # find /usr/local/etc -name "php.ini"
root@ns1:~ # find /usr/local/bin -name "php"

In this article we have discussed which, whereis and locate commands which are valuable utilities for quickly finding files and directories. It uses a database that allows fast and efficient searches, making it an ideal choice for finding files in large file systems. Overall, we can say that by mastering these commands with their various options, we can save time and effort when searching for a particular file or directory in the FreeBSD system.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post