Linux find and locate file command

1.pwd command

The pwd command is used to display the current working directory of the user, and its full English name is "print working directory".

[root@localhost etc]# pwd
/etc

2. cd command

The cd command is used to switch the current working path, the English full name is "change directory", and the syntax format is "cd [parameter] [directory]".

cd -: Return to the last directory.

cd..: enter the parent directory.

cd ~: Switch to the current user's home directory.

cd~username: switch to other user's home directory.

[root@localhost ~]# cd /etc/
[root@localhost etc]# cd /bin
[root@localhost bin]# cd -
/etc
[root@localhost etc]# cd ~
[root@localhost ~]# 

 3.ls command

The .ls command is used to display the file information in the directory. The English full name is "list", and the syntax format is "ls [parameter] [file name]".

-a, --all: List all files, including hidden files starting with (.).

-l: Single-column output (one file per line).

-d: List directory names like other files instead of their contents.

-t: Sort by time information.

parameter effect
-a,-all List all files, including hidden files starting with (.)
-l Output one file per line (single-column output)
-d List directory names like other files instead of their contents
-t Sort by time information

 Print all information about the current directory in a single column:

[root@localhost ~]# ls -al
total 22860
dr-xr-x---. 17 root root     4096 Oct 20 10:57 .
dr-xr-xr-x. 17 root root      224 Oct 18 23:55 ..
-rw-------.  1 root root     1392 Oct 19 00:02 anaconda-ks.cfg
-rw-------.  1 root root        1 Oct 20 10:57 .bash_history
-rw-r--r--.  1 root root       18 Aug 13  2018 .bash_logout
-rw-r--r--.  1 root root      176 Aug 13  2018 .bash_profile
-rw-r--r--.  1 root root      176 Aug 13  2018 .bashrc
drwx------. 16 root root     4096 Nov 11  2023 .cache
drwx------. 15 root root     4096 Oct 19 19:31 .config
drwxr-xr-x.  2 root root       24 Oct 19 15:42 csdn.net
-rw-r--r--.  1 root root      100 Aug 13  2018 .cshrc
drwx------.  3 root root       25 Oct 19 00:03 .dbus
drwxr-xr-x.  2 root root        6 Oct 19 00:04 Desktop

View the attribute information of the /etc directory:

[root@localhost ~]# ls -ld /etc/
drwxr-xr-x. 135 root root 8192 Nov 11  2023 /etc/

4. tree command

The tree command is used to list the contents and structure of the directory in the format of a tree diagram

Use ls to view files in a directory:

[root@localhost ~]# ls
A  anaconda-ks.cfg  csdn.net  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  Pictures  Public  Templates  Videos

Use tree to view files in a directory:

[root@localhost ~]# tree
.
├── A
│   └── B
│       └── C
├── anaconda-ks.cfg
├── csdn.net
│   └── index.html
├── Desktop
├── Documents
├── Downloads
├── initial-setup-ks.cfg
├── Music
├── Pictures
├── Public
├── Templates
└── Videos

12 directories, 3 files

5. find command

The find command is used to search for the location corresponding to the file according to the specified conditions, and the syntax format is "find [search range] search conditions".

In Linux, the search work is generally done through the find command, which can use different file characteristics as search conditions (such as file name, size, modification time, permission information, etc.), and once the match is successful, the information will be displayed by default in on the screen.

The parameters and functions of the find command are as follows:

parameter effect
-name match name
-perm Match permissions (mode is exact match, -mode is included)

-user 

match owner
 -group match all groups
-mtime -n +n Match the time of modification (-n means within n days, +n means n days ago)
-atime -n +n Match the time when the file was accessed (-n means within n days, +n means n days ago)
-ctime -n +n Match the time of modifying file permissions (-n means within n days, +n means n days ago)
-nouser matches files with no owner
-nogroup matches files with no owning group
-nwer f1 !f2 matches files newer than f1 but older than f2
--type b/ d/ c/ p/ l/ f

Match the file type (the following letters represent block devices, directories, character devices,

pipes, linked files, text files)

-size Match the size of the file (+50KB to find files larger than 50KB, and -50KB to find files smaller than 50KB)
-prune ignore a directory
-exec.... { } \; Can be followed by commands for further processing of the search results

The -exec parameter is used to further process the search result of the find command by the following command. Since the find command has special requirements for parameters, although exec is in the long form, it still only needs a minus sign in front of it ( -)

Example demo:

The configuration file of the Linux system will be saved in the /etc directory, and a list of all files starting with host in this directory can be obtained:

[root@localhost ~]# find /etc -name "host*"
/etc/host.conf
/etc/hosts
/etc/hosts.allow
/etc/hosts.deny
/etc/avahi/hosts
/etc/hostname

 6. locate command

The locate command is used to quickly search for the location corresponding to the file according to the name, and the syntax format is "locate file name".

Using the find command to perform a full-disk search is more accurate, but less efficient. The locate command can find some common files with approximate names.

When using the locate command, first use the updatedb command to generate an index file. The name of this library file is /var/lib/mlocate/mlocate.db, and then use the locate command to search for files in this library. The speed It will be a lot.

Before using the locate command for the first time, execute the updatedb command to generate an index database and then search:

[root@localhost home]# updatedb
[root@localhost home]# ls -l /var/lib/mlocate/mlocate.db 
-rw-r-----. 1 root slocate 2974385 Oct 20 16:15 /var/lib/mlocate/mlocate.db

Use the locate command to search for the location of the file containing the name "whereis":

[root@localhost home]# locate whereis
/usr/bin/whereis
/usr/share/bash-completion/completions/whereis
/usr/share/man/man1/whereis.1.gz

7. whereis command

The whereis command is used to quickly search for the corresponding locations of binary programs (commands), source codes, and help files according to names, and the syntax format is "whereis command name".

The whereis command is also based on the index library file generated by the updatedb command. The difference between it and the locate command is that it does not care about those files with the same name, but only quickly finds the location of the corresponding command file and its help file.

Use the whereis command to find out where the ls and pwd commands are located:

[root@localhost home]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz
[root@localhost home]# whereis pwd
pwd: /usr/bin/pwd /usr/share/man/man1/pwd.1.gz /usr/share/man/man1p/pwd.1p.gz

8. which command

The which command is used to quickly search for the location corresponding to the binary program (command) according to the specified name, and the syntax format is "which command name".

The which command is to search the path where the command is located in the path specified by the PATH variable according to the specified conditions. In other words, if we don't care about the file with the same name (find and locate), nor the source code and help file (whereis) corresponding to the command, but just want to find the path where the command itself is located, then the which command is perfect.

Find the path corresponding to the locate and whereis commands:

[root@localhost home]# which locate
/usr/bin/locate
[root@localhost home]# which whereis
/usr/bin/whereis

Guess you like

Origin blog.csdn.net/qq_41255880/article/details/127426333