Linux commands - find, file, export, yum, ln, pstree, stat, timedatectl, locale, cp, mv, dirname, and basename



Used to find files in the specified directory. Any string that precedes the parameter is treated as the directory name you want to find. If you use this command without setting any parameters, the find command will find subdirectories and files in the current directory. And display all the subdirectories and files found.


find   path   -option   [   -print ]   [ -exec   -ok   command ]   {} \;

Path: the directory path to find

        ~  Represents the home directory

        .  Represents the current directory

        /  Represents the root directory

        ..  Represents the upper level directory

Options: common options

        - name lianxi         Find a file named Lianxi

        - perm           Find by execution permission

        - group   groupname         Find by group

        - size   n[c]         Find a file with a length of N bytes

        - mtime -t +t           Find files by file change time, - N refers to within n days and + n refers to before n days

        - atime -t +t           Find files by file access time, - N refers to within n days and + n refers to before n days

        - ctime   - t   + t          Find files by file creation time, - N refers to within n days and + n refers to before n days

print: indicates the standard output of the result

ok: give a prompt before execution to let the user determine whether to execute

options: indicates the search method


Find files ending in. jpg

[root@localhost ~]# find / -name "*.jpg"


Used to identify file types


File [- bclvz] [- f < namefile >] [- M < magicfiles >...] [file or directory...]

Command parameters

-b         The file identification results are listed, and the file name is not displayed

[root@localhost lianxi]# file /etc/passwd -b
ASCII text

-i         Output string of mime type·

[root@localhost lianxi]# file /etc/passwd -i
/etc/passwd: text/plain; charset=us-ascii

-F         Replace the default after the output file name with the specified separator:

[root@localhost lianxi]# file /etc/passwd -F "--"
/etc/passwd-- ASCII text

-z         Try to interpret the contents of the compressed file

--help         Get help for the command

[root@localhost lianxi]# file --help


Used to set or display environment variables


export [ -fnp ]     [variable name] = [variable setting value]


-f         Represents the name of the function in [variable name]

-n         Delete the specified variable. The variable is not actually deleted, but will not be output to the execution environment of subsequent instructions

-p         Lists the environment variables assigned to the program by the shell used


Promote local variables to global variables

[root@localhost ~]# echo $a

[root@localhost ~]# a=1
[root@localhost ~]# echo $a
[root@localhost ~]# bash
[root@localhost ~]# echo $a

[root@localhost ~]# exit
[root@localhost ~]# export a
[root@localhost ~]# echo $a
[root@localhost ~]# bash
[root@localhost ~]# echo $a
[root@localhost ~]# 

Lists the current environment variable values

[root@localhost ~]# export -p
declare -x HISTCONTROL="ignoredups"
declare -x HISTSIZE="1000"
declare -x HOME="/root"
declare -x HOSTNAME="localhost.localdomain"
declare -x LANG="zh_CN.UTF-8"
declare -x LESSOPEN="||/usr/bin/ %s"
declare -x LOGNAME="root"
declare -x LS_COLORS="rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:"
declare -x MAIL="/var/spool/mail/root"
declare -x OLDPWD
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
declare -x PWD="/root"
declare -x SHELL="/bin/bash"
declare -x SHLVL="2"
declare -x SSH_CLIENT=" 7481 22"
declare -x SSH_CONNECTION=" 7481 22"
declare -x SSH_TTY="/dev/pts/0"
declare -x TERM="xterm"
declare -x USER="root"
declare -x XDG_RUNTIME_DIR="/run/user/0"
declare -x XDG_SESSION_ID="11"
declare -x a="1"


yum provides commands to find, install, and delete one, a group, or even all of the software installation packages


yum [options] [command] [package ...]

options: optional. options include - h (help), - y (yes when prompted during installation), - q (do not display installation process), etc.

command: the operation to be performed.

Package: name of the installed package.

yum common commands

1. List the updatable software used

[root@localhost ~]# yum check-update

2. Update all software

[root@localhost ~]# yum update  -y

3. Delete the software installation package

[ root@localhost ~]# yum remove lrzsz -y     # Delete lrzsz

4. Install software

[ root@localhost ~]# yum install lrzsz -y     # Install lrzsz

5. Find out what software package the software is installed from

[ root@localhost ~]# yum provides rz       # Find out which package RZ is installed from

6. List all installable software

[root@localhost ~]# yum list

[ root@localhost ~]# yum list pam*        # List the installable software starting with PAM


The link file used to create synchronization is the shortcut in windows

Links can be divided into two types: hard link and symbolic link:

Hard link means that a file can have multiple names, while soft link means to generate a special file whose content points to the location of another file.

Soft link:

1. Soft link, which exists in the form of path. It is similar to shortcut in Windows operating system

2. Soft links can cross file systems, but hard links cannot

3. Soft link can link a nonexistent file name

4. Soft link can link the directory

Hard links exist in the same file system, while soft links can span different file systems.

Hard link:

1. Hard link, which exists in the form of file copy, but does not occupy the actual space.

2. Hard links to directories are not allowed

3. Hard links can only be created in the same file system

Both hard links and soft links occupy very little memory


 ln [parameter] [source file or directory] [target file or directory]


-b delete and overwrite previously established links

-d allow super users to make hard links to directories

-f enforcement

-i interactive mode. If the file exists, the user will be prompted whether to overwrite it

-n treat symbolic links as general directories

-s soft link (symbolic link)

-v displays the detailed process


[root@localhost lianxi]# ln -s hunan changsan
[root@localhost lianxi]# ls  changsan  guangdong  hubei  hunan
[root@localhost lianxi]# ll
 Total consumption 0
-rw-r--r--. 1 root root  0 11 June 27-13:51
-rw-r--r--. 1 root root  0 11 June 27-13:54
lrwxrwxrwx. 1 root root  5 11 June 28-14:08 changsan -> hunan
-rw-r--r--. 1 root root  0 11 June 27-13:54
drwxr-xr-x. 2 root root  6 11 December 28:38
drwxr-xr-x. 4 root root 32 11 June 27-14:12 guangdong
drwxr-xr-x. 3 root root 19 11 June 27-14:08 hubei
drwxr-xr-x. 4 root root 34 11 June 27-14:54 hunan
[root@localhost lianxi]# 


View the process tree in linux system to describe the relationship between processes and the process genealogy


pstree   (options)

Common options

-h         Display the process tree, especially the program being executed

-p         Display pid

-a         Display the details of all processes. If you encounter the same process name, you can compress the display


[root@localhost lianxi]# pstree -p
           │                     ├─{NetworkManager}(848)
           │                     └─{NetworkManager}(851)
           │              └─qmgr(1425)
           │              ├─{polkitd}(792)
           │              ├─{polkitd}(793)
           │              ├─{polkitd}(794)
           │              ├─{polkitd}(795)
           │              └─{polkitd}(798)
           │                └─{rsyslogd}(1193)
           │            └─sshd(23933)───bash(23938)
           │             ├─{tuned}(1588)
           │             ├─{tuned}(1590)
           │             └─{tuned}(1591)


Used to display file or system details


stat [OPTION]... FILE...


-f         Displays information about the file system where the file resides

[root@localhost ~]# stat lianxi -f
    ID: fd0000000000 File name length: 255     Type: xfs
 Block size: 4096       Basic block size: 4096
    Block: Total: 4452864    Idle: 4049957    Available: 4049957
Inodes: Total: 8910848    Idle: 8875109

-t         Output information in a concise manner

[root@localhost ~]# stat lianxi -t
lianxi 121 0 41ed 0 0 fd00 51139261 6 0 0 1638079730 1638079729 1638079729 0 4096 unconfined_u:object_r:admin_home_t:s0

Meaning of some fields

[root@localhost ~]# stat lianxi
  Size: 121       	Block: 0          IO Block: 4096 catalog
 Equipment: fd00h/64768d	Inode: 51139261    Hard link: 6
 jurisdiction:(0755/drwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Environmental Science: unconfined_u:object_r:admin_home_t:s0
 Recent visit: 2021-11-28 14:08:50.450981770 +0800
 Recent changes: 2021-11-28 14:08:49.422975899 +0800
 Recent changes: 2021-11-28 14:08:49.422975899 +0800
 Created on:-

Access time: access time -- atime   Is the time to read the file   cat  
Modify time: modify time -- mtime   Is to really modify the contents of the file         --> recommend
Change time: change time -- ctime   Time to change the attributes of the file (file name, time, size,)


It is used to control the system time and date, query and change the system time, and set and modify the time zone information


1. View the current time / date / time zone:

[root@localhost ~]# timedatectl

[root@localhost ~]# timedatectl status

2. View all available time zones

[root@localhost ~]# timedatectl list-timezones

3. Set time zone

[ root@localhost ~]# timedatectl set-timezones   ” Time zone information“

4. Set UTC (coordinated universal time, also known as world unified time, world standard time and international coordinated time. Due to the different abbreviations of English (CUT) and French (TUC), it is referred to as UTC as a compromise.)

[root@localhost ~]# timedatectl set-timezones  UTC

5. Set time

[root@localhost ~]# timedatectl set-timezones  XX:XX:XX

6. Set date

[root@localhost ~]# timedatectl set-timezones  XXXX:XX:XX

7. Set date and time

[root@localhost ~]# timedatectl set-timezones  "XXXX:XX:XX  XX:XX"

8. Set the hardware clock to local time

[root@localhost ~]# timedatectl set-local-rtc 1

9. Set the hardware clock to UTC time

[root@localhost ~]# timedatectl set-local-rtc 0

10. Start NTP time synchronization

[root@localhost ~]# timedate set-ntp true

11. Turn off NTP time synchronization

[root@localhost ~]# timedate set-ntp false


It is an interface for setting multilingual environment in Linux

It is used to set up different locales for program runtime, which is supported by ANSIC

Naming conventions

< language >_< Region >. < character set encoding >


zh stands for Chinese

CN stands for mainland China

UTF-8 represents character encoding

Syntax format

locale [parameter]

Common parameters

-a         Write the name of the available locale

-m         Write the name of the available character map

-c         Writes the name of the selected category

-k         Writes the name of the selected keyword


Show default character set

[root@localhost ~]# locale

View available locales

[root@localhost ~]# locale -a


Used to copy files or directories

Syntax format

       cp [OPTION]... [-T] SOURCE DEST
       cp [OPTION]... -t DIRECTORY SOURCE...


-a          This option is usually used when copying a directory. It retains links, file properties, and copies everything under the directory. Its function is equal to dpR parameter combination.

-d          Keep links when copying. The links mentioned here are equivalent to shortcuts in Windows system.

-f          Overwrite the existing target file without prompting.

-i          And  - f   On the contrary, a prompt is given before overwriting the target file, asking the user to confirm whether to overwrite and answer   y   The target file will be overwritten.

-p          In addition to copying the contents of the file, the modification time and access rights are also copied to the new file.

-r          If the given source file is a directory file, all subdirectories and files in the directory will be copied.

-l          Do not copy files, just generate linked files.

When the copied file or directory exists at the destination, it will be reminded because an alias is defined

[root@localhost ~]# alias cp
alias cp='cp -i'


     1. Cancel alias
    unalias  cp
     2. Use the absolute path of the cp command to bypass the use of aliases  ---> Recommended methods
    [root@localhost lianxi]# which cp
    alias cp='cp -i'
    [root@localhost lianxi]# /usr/bin/cp  *.txt hunan

     \        The escape character also bypasses the alias and uses its original meaning
    [root@localhost lianxi]# \cp *.txt hunan


Copy a single file

[root@localhost lianxi]# cp  songyuxiao.txt  changsha
                              source file           destination
                              source           destination

Copy multiple files

[root@localhost lianxi]# cp  feng.txt  songyuxiao.txt  weijunlin.txt   hunan
                               The first three files are source files                   destination

Use Wildcards

[root@localhost lianxi]# Cp *. txt hunan copies all files ending in. txt to hunan

Copy and rename

#Copy songyuxiao.txt to the hunan directory and rename it syx.txt
[root@localhost lianxi]# cp songyuxiao.txt  hunan/syx.txt
[root@localhost lianxi]# cp feng.txt  hunan/fengdeyong.txt

And the use of absolute paths

[root@localhost lianxi]# CP / etc/passwd. Copy the / etc/passwd file to the current directory
[root@localhost lianxi]# cp  /etc/passwd  /lianxi/hunan

Replication of folders    --> plus   - r

[root@localhost lianxi]# cp -r hunan hubei  guangdong
                               Source folder     destination

mv (operation is similar to cp)

Used to move or rename the location of files and directories

[root@localhost lianxi]# mv hunan guangxi

      1. Move files or folders   --> If the following folder exists, it will be moved

      2. Rename  --> If the following file does not exist, it will be renamed


       mv [OPTION]... [-T] SOURCE DEST
       mv [OPTION]... -t DIRECTORY SOURCE...


-b          When a target file or directory exists, a backup is created for it before performing an overwrite.

-i           If the specified source directory or file has the same name as the target directory or file, you will be asked whether to overwrite the old file. Enter y to directly overwrite, and enter n to cancel the operation.

-f           If the specified source directory or file has the same name as the target directory or file, the old file will be overwritten without asking.

-n:          Do not overwrite any existing files or directories.

-u          The move operation is performed only when the source file is newer than the target file or the target file does not exist.


Move multiple files

[root@localhost lianxi]# mv gui changsha hubei guangdong/  china
                              source file                       destination

Use Wildcards

[root@localhost lianxi]# MV *. txt copy the file ending in. txt to the directory of china


dirname  + route   The path to the folder where the absolute path is obtained


basename  + route   Get the name of the last file

Tags: Linux

Posted on Sun, 28 Nov 2021 19:12:31 -0500 by psy