Linux basic file directory operation commands

Posted by maxmjessop on Sun, 02 Jan 2022 15:28:11 +0100

Linux basic file directory operation commands

Display directory or file: displays the target list. It is a command with high utilization in Linux system The output information of ls command can be highlighted in color to partition different types of files

[root@localhost ~]# ls --help

Command syntax:[ ls [option] File or directory ]

        -a              #Show all files, including hidden files, along with With The documents are also listed
        -A              #Show all files, including hidden files, but do not list them With
        -d              #List directories only
        -f              #List results directly without sorting
        -h              #The file size is displayed as B KB MB
        -i              #Lists the Inode number of the file
        -l              #Display in long format
        -n              #List uid and gid
        -S              #Sort by file size (large files first, small files last)
        -t              #Sort by time (large date first, small date last)
        -r              #Reverse output of sorting results
        -R              #Recursively display directories and subdirectories
        -Z              #List SELinux security context
        --full-time     #List file details
        --time=atime/ctime/mtime        #Lists the specified time

Use the ls -lh parameter to query the directory details

[root@localhost ~]# ls -lh
total 0
-rw-r--r--. 1 root root 0 Nov 13 09:36 admin
drwxr-xr-x. 2 root root 6 Nov 13 09:36 lyshark

Item 1:          File permission bit
 Item 2:          Reference count(file:Hard link directory:Number of subdirectories under directory)
Item 3:          Owner of the file(Owner->Here is root)
Item 4:          The group to which the file belongs(Genus group->Here is root)
Item 5:          file size(Default unit byte byte)
Item 6:          Last modification time
 Item 7:          file name

Use LS - LH -- full time to display the detailed time information of file creation

[root@localhost ~]# ls -h --full-time
total 0
-rw-r--r--. 1 root root 0 2018-11-13 09:36:25.172274787 -0500 admin
drwxr-xr-x. 2 root root 6 2018-11-13 09:36:13.292275532 -0500 lyshark

Use ls -lhS to display the file and print it from large to small

[root@localhost ~]# ls -lhS
total 0
drwxr-xr-x. 2 root root 6 Nov 13 09:36 lyshark
-rw-r--r--. 1 root root 0 Nov 13 09:36 admin

Use ls -lhrS to display the file and print it from small to large

[root@localhost ~]# ls -lhrS
total 0
-rw-r--r--. 1 root root 0 Nov 13 09:36 admin
drwxr-xr-x. 2 root root 6 Nov 13 09:36 lyshark

Use ls -lZ to display the file and display the SeLinux security context

[root@localhost ~]# ls -lZ 
-rw-r--r--. root root unconfined_u:object_r:admin_home_t:s0 admin
drwxr-xr-x. root root unconfined_u:object_r:admin_home_t:s0 lyshark

Directory jump and switch: switch the current working directory

[root@localhost ~]# cd --help

Command syntax:[ cd [option] catalogue ]

        cd              #Enter user home directory
        cd ~            #Enter user home directory
        cd -            #Returns the directory where you were before entering this directory
        cd ..           #Return to parent directory
        cd ../..        #Return to the upper two directories
        cd !$           #Use the parameters of the previous command as cd parameters

Use cd ~ or cd to switch to the user's home directory

[root@localhost etc]# pwd
/etc

[root@localhost etc]# cd

[root@localhost ~]# pwd
/root

Using cd Switch to the previous directory

[root@localhost ~]# pwd
/root

[root@localhost ~]# cd ..

[root@localhost /]# pwd
/

Using cd.. / Switch to the upper two directories

[root@localhost sysconfig]# pwd
/etc/sysconfig

[root@localhost sysconfig]# cd ../..

[root@localhost /]# pwd
/

Create new directory: the mkdir command is used to create a new directory or create some directories recursively.

[root@localhost ~]# mkdir --help

Syntax format:[ mkdir [option] New directory name ]

        -v              #Show creation process
        -p              #Create directory recursively
        -Z              #Set security context
        -m              #Set permissions when creating a directory

Create directories recursively using mkdir -p

[root@localhost ~]# mkdir -p /tmp/lyshark/lyshark

[root@localhost ~]# ls -lh /tmp/lyshark/
total 0
drwxr-xr-x. 2 root root 6 Nov 13 09:56 lyshark

Set permissions when creating a directory using mkdir -m

[root@localhost ~]# mkdir -m 000 lyshark

[root@localhost ~]# ls -lh
total 0
d---------. 2 root root 6 Nov 13 10:02 lyshark

Create empty file: the touch command is used to create an empty file, press, or update the time tag of the existing file to the current time of the system.

[root@localhost ~]# touch --help

Syntax format:[ touch [option] file name ]

        -a              #Modify access time
        -c              #Modify the file time. If the file does not exist, no new file will be created
        -m              #Modify mtime only
        -t              #File modification time (touch -t 1806101012)

Use the touch command to create an empty file lysark. In the current directory txt.

[root@localhost ~]# ls -lh
total 0
[root@localhost ~]# touch lyshark.txt
[root@localhost ~]# ls -lh
total 0
-rw-r--r--. 1 root root 0 Nov 13 10:32 lyshark.txt

Use the touch -t command to modify the file at 11:59 on November 3, 2015

[root@localhost ~]# ls -lh --full-time
total 0
-rw-r--r--. 1 root root 0 2018-11-13 10:35:50.639051120 -0500 lyshark.txt

[root@localhost ~]# touch -t 1511031159 lyshark.txt 

[root@localhost ~]# ls -lh --full-time
total 0
-rw-r--r--. 1 root root 0 2015-11-03 11:59:00.000000000 -0500 lyshark.txt

Copy file or directory: the cp command is used to copy one or more source files or directories to the specified destination file or directory.

[root@localhost ~]# cp --help

Syntax format:[ cp [option] Source file or directory destination directory ]

        -a              #Equivalent to pdr
        -d              #Copy along with the properties of the linked file
        -f              #Force replication
        -i              #If the target file already exists, ask when overwriting
        -p              #Copy with file attributes (backup common)
        -r              #Recursive replication, used for directory replication
        -s              #Create soft connection on copy
        -u              #Copying is not prompted until the source file is updated

Use the cp -a command to copy the file, and copy the file of / etc / to the / tmp / directory

[root@localhost ~]# cp -a /etc/* /tmp/

[root@localhost ~]# ls -lh /tmp/
total 1.1M
-rw-r--r--.  1 root root     16 Oct 13 12:37 adjtime
-rw-r--r--.  1 root root   1.5K Jun  7  2013 aliases
-rw-r--r--.  1 root root    12K Oct 13 12:39 aliases.db
drwxr-xr-x.  2 root root    236 Oct 13 12:34 alternatives
....ellipsis....

Use the cp -s command to copy files and create a soft link from / etc/passwd to the / tmp / directory

[root@localhost ~]# cp -s /etc/passwd /tmp/

[root@localhost ~]# ls -lh /tmp/
total 0
lrwxrwxrwx. 1 root root 11 Nov 13 10:09 passwd -> /etc/passwd

Use cp -a command to copy multiple files at a time to the / tmp / directory

[root@localhost ~]# cp -a /etc/passwd /etc/shadow /tmp/

[root@localhost ~]# ls -lh /tmp/
total 8.0K
-rw-r--r--. 1 root root 898 Oct 13 12:37 passwd
----------. 1 root root 714 Oct 13 12:37 shadow

Move file or directory: the mv command is used to rename the file or directory. If the file exists, it will be forced to update.

[root@localhost ~]# mv --help

Syntax format:[ mv [option] Source file or directory destination directory ]

        -f              #Forced movement
        -i              #Move interactively
        -u              #Source files are not moved until they are updated

Use the mv command to move / etc/passwd to the / tmp directory

[root@localhost ~]# mv /etc/passwd /tmp/

[root@localhost ~]# ls -lh /tmp/
total 4.0K
-rw-r--r--. 1 0 root 898 Oct 13 12:37 passwd

Use the mv command to move multiple files at a time. Next, move / etc/passwd and / etc/shadow to the / tmp directory

[root@localhost ~]# mv /etc/passwd /etc/shadow /tmp/

[root@localhost ~]# ls -lh /tmp/
total 8.0K
-rw-r--r--. 1 0 root 898 Oct 13 12:37 passwd
----------. 1 0 root 714 Oct 13 12:37 shadow

Use the mv command to rename the lysark in the current directory to linux

[root@localhost ~]# ls -lh
total 0
drwxr-xr-x. 2 root root 6 Nov 13 10:19 lyshark

[root@localhost ~]# mv lyshark/ linux

[root@localhost ~]# ls -lh
total 0
drwxr-xr-x. 2 root root 6 Nov 13 10:19 linux

Delete file or directory: the rm command can delete one or more files or directories in a directory, or delete a directory and all its subordinate files and subdirectories

[root@localhost ~]# rm --help

Syntax format:[ rm [option] File or directory ]

        -f              #Delete without warning
        -r              #Recursive deletion
        -i              #Prompt before deleting
        -fr             #Common collocation

Use the rm -fr command to delete the linux directory under the current directory

[root@localhost ~]# ls
linux
[root@localhost ~]# rm -fr linux/
[root@localhost ~]# ls

Use the rm -fr * command to delete all files in the specified folder using wildcards

[root@localhost lyshark]# ls
1  10  2  3  4  5  6  7  8  9

[root@localhost lyshark]# rm -fr *
[root@localhost lyshark]# ls

Judge the type of file: the file command is used to detect the type of a given file. The file command checks the file in three processes: file system check, magic magic number check and language check

[root@localhost ~]# file --help

Syntax format:[ file [option] File or directory ]

        -b              #When the identification results are listed, the file name is not displayed
        -c              #Detailed display of instruction execution process
        -f file name        #Read and determine the format in the file at one time
        -z              #Query compressed package information

Use the file command to determine the format of / etc/passwd and / bin/bash files

[root@localhost ~]# file /etc/passwd
/etc/passwd: ASCII text

[root@localhost ~]# file /bin/bash
/bin/bash: ELF 64-bit LSB executable, x86-64, version 1 (SYSV),
dynamically linked (uses shared libs), for GNU/Linux 2.6.32,
BuildID[sha1]=9a57e086388119ecd285c4d5c66823f3f3b68ab5, stripped

Use the file -z command to read lysark tar. GZ compressed package file properties

[root@localhost ~]# ls -lh
total 9.4M
-rw-r--r--. 1 root root 9.4M Nov 13 10:51 lyshark.tar.gz

[root@localhost ~]# file -z lyshark.tar.gz 
lyshark.tar.gz: POSIX tar archive (GNU) ,
(gzip compressed data, from Unix, last modified: Tue Nov 13 10:51:13 2018)

Use the file -f command to read the specified line in the file and judge the file in turn

[root@localhost ~]# cat temp 
/etc/passwd
/etc/shadow
/bin/bash
/bin/ls

[root@localhost ~]# file -f temp 
/etc/passwd: ASCII text
/etc/shadow: ASCII text

/bin/bash:   ELF 64-bit LSB executable, x86-64, version 1 (SYSV), 
dynamically linked (uses shared libs), for GNU/Linux 2.6.32, 
BuildID[sha1]=9a57e086388119ecd285c4d5c66823f3f3b68ab5, stripped

/bin/ls:     ELF 64-bit LSB executable, x86-64, version 1 (SYSV), 
dynamically linked (uses shared libs), for GNU/Linux 2.6.32, 
BuildID[sha1]=ec3f3e5e8160c9917e8a4b896fe9044748472991, stripped

Query file status: the stat command is used to display file status information. The output information of the stat command is more detailed than that of the ls command.

[root@localhost ~]# stat --help

Syntax format:[ stat [option] File or directory ]

        -L              #Support symbolic connection
        -f              #Displays file system status instead of file status
        -t              #Output information in a concise manner

Use the stat command to query the information of / bin/bash file

[root@localhost ~]# stat /bin/bash
  File: '/bin/bash'
  Size: 964544    	Blocks: 1888       IO Block: 4096   regular file
Device: fd00h/64768d	Inode: 50340311    Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:shell_exec_t:s0
Access: 2018-11-13 09:33:12.197999579 -0500
Modify: 2017-09-26 09:14:20.000000000 -0400
Change: 2018-10-13 12:32:43.377997461 -0400
 Birth: -

Use the stat -f command to display system status information

[root@localhost ~]# stat -f /bin/bash
  File: "/bin/bash"
    ID: fd0000000000 Namelen: 255     Type: xfs
Block size: 4096       Fundamental block size: 4096
Blocks: Total: 4452864    Free: 4155020    Available: 4155020
Inodes: Total: 8910848    Free: 8877953

Create linked file: the ln command is used to create a connection for a file. You can create either a soft connection or a hard link.

[root@localhost ~]# ln --help

Syntax format:[ stat [option] File or directory ]

        -L              #Support symbolic connection
        -d              #Establish hard links to directories
        -f              #Force link
        -i              #Ask before overwrite
        -s              #Create a soft link

Use the ln command to establish a hard link

[root@localhost ~]# ls -lhi
total 944K
33844798 -rwxr-xr-x. 1 root root 942K Sep 26  2017 bash

[root@localhost ~]# ln bash bash_ln
[root@localhost ~]# ls -lhi
total 1.9M
33844798 -rwxr-xr-x. 2 root root 942K Sep 26  2017 bash
33844798 -rwxr-xr-x. 2 root root 942K Sep 26  2017 bash_ln

Use the ln -s command to create a soft link

[root@localhost ~]# ls -lhi
total 944K
33844798 -rwxr-xr-x. 1 root root 942K Sep 26  2017 bash

[root@localhost ~]# ln -s bash bash_link
[root@localhost ~]# ls -lhi
total 944K
33844798 -rwxr-xr-x. 1 root root 942K Sep 26  2017 bash
33959316 lrwxrwxrwx. 1 root root    4 Nov 13 11:06 bash_link -> bash

Text print command: cat file print scroll command, control parameter output.

[root@localhost ~]# cat --help

Syntax format:[ cat [option] file name ]

        -b              #Line numbers are listed, and blank lines are not labeled
        -E              #Display the closing line character $
        -T              #Show Tab key as ^ I
        -n              #Print out the line number

Use the cat -n command to label the text and print it

[root@localhost ~]# cat -n /etc/passwd
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync
....ellipsis....

Use the cat -E command to display the text terminator $

[root@localhost ~]# cat -E /etc/passwd
root:x:0:0:root:/root:/bin/bash$
bin:x:1:1:bin:/bin:/sbin/nologin$
daemon:x:2:2:daemon:/sbin:/sbin/nologin$
adm:x:3:4:adm:/var/adm:/sbin/nologin$
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin$
sync:x:5:0:sync:/sbin:/bin/sync$
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown$
....ellipsis....

Display opening text: the head command is used to display the contents at the beginning of the file. By default, the head command displays the first 10 lines of the file

[root@localhost ~]# head --help

Syntax format:[ head [option] file name ]

        -c 10           #Display the first 10 characters
        -n 10           #Show first 10 lines
        -v              #Always display header information of file name
        -q              #Do not display header information for file names

Use the head -c command to display the first 20 characters of the text

[root@localhost ~]# head -c 20 /etc/passwd
root:x:0:0:root:/roo

[root@localhost ~]# 

Use the head -n command to display the first 3 lines of text

[root@localhost ~]# head -n 3 /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

Display end text: the tail command is used to input the end content of the file. The tail command displays the end 10 lines of the specified file on the screen by default

[root@localhost ~]# tail --help

Syntax format:[ tail [option] file name ]

        -c 10           #Last 10 characters displayed
        -n 10           #Display the last 10 lines of the file
        -f              #Continuously monitor changes behind documents
        --pid=PID       #Used with - f to indicate that it ends after the process ID dies

Use the tail -n command to display the last 3 lines of text

[root@localhost ~]# tail -n 3 /etc/passwd
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
lyshark:x:1000:1000:lyshark:/home/lyshark:/bin/bash

Use the tail -f command to dynamically monitor a file

[root@localhost ~]# tail -f /var/log/messages 
Nov 13 10:01:01 localhost systemd: Starting Session 3 of user root.
Nov 13 10:10:54 localhost kernel: e1000: ens32 NIC Link is Down
Nov 13 10:10:58 localhost kernel: e1000: ens32 NIC Link is Up 
Nov 13 10:10:58 localhost NetworkManager[772]: <info>  
....ellipsis....

Use the tail --pid=PID command to monitor a process

[root@localhost ~]# ps
   PID TTY          TIME CMD
  1404 pts/0    00:00:00 bash
 11441 pts/0    00:00:00 ps
[root@localhost ~]# 
[root@localhost ~]# tail --pid=1401
tail: warning: PID ignored; --pid=PID is useful only when following
....ellipsis....

Use the tail -n +5 command to start printing after the fifth line

[root@localhost ~]# tail -n +5 /etc/passwd
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
....ellipsis....

Use the tail -n +5 | head -n 3 command to start printing from the fifth line and print down 2 lines

[root@localhost ~]# tail -n +5 /etc/passwd |head -n 3
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown

Output text in specified format: the nl command reads the file parameter. By default, the standard input calculates the line number in the input, and writes the calculated line number to the standard output

[root@localhost ~]# nl --help

Syntax format:[ nl [option] file name ]

        -c 10           #Last 10 characters displayed
        -b a            #List line numbers, including spaces (similar to cat -n)
        -b t            #List line numbers, excluding spaces (similar to cat -n)
        -n ln           #The line number is displayed on the far left
        -n rn           #The line number is displayed on the far right
        -n rz           #The line number is displayed on the far right and supplemented with 0
        -w              #Specify the number of complement 0

Use the nl -b a command to print text labels (similar to cat -n)

[root@localhost ~]# nl -b a /etc/passwd
     1  root:x:0:0:root:/root:/bin/bash
     2  bin:x:1:1:bin:/bin:/sbin/nologin
     3  daemon:x:2:2:daemon:/sbin:/sbin/nologin
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
     6  sync:x:5:0:sync:/sbin:/bin/sync
....ellipsis....

Use the nl -b a -n rz command to list and number the text, and fill in the space 0

[root@localhost ~]# nl -b a -n rz /etc/passwd
000001  root:x:0:0:root:/root:/bin/bash
000002  bin:x:1:1:bin:/bin:/sbin/nologin
000003  daemon:x:2:2:daemon:/sbin:/sbin/nologin
000004  adm:x:3:4:adm:/var/adm:/sbin/nologin
000005  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
....ellipsis....

Use the nl -b a -n rz -w 3 command to list the text and give it a number, which is filled with 2 lines of 0

[root@localhost ~]# nl -b a -n rz -w 3 /etc/passwd
001 root:x:0:0:root:/root:/bin/bash
002 bin:x:1:1:bin:/bin:/sbin/nologin
003 daemon:x:2:2:daemon:/sbin:/sbin/nologin
004 adm:x:3:4:adm:/var/adm:/sbin/nologin
005 lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
....ellipsis....

Non plain text file printing: od used to output octal, hexadecimal or other format encoded bytes of the file. This command is mainly used to view the values saved in binary files

[root@localhost ~]# od --help

Syntax format:[ od [option] file name ]

        -t a            #Output using default characters
        -t c            #Output using ASCII characters
        -t d            #Use decimal output
        -t f            #Use floating point output
        -t o            #Use octal output
        -t x            #Use hexadecimal output

Use the od -t a command to output in the default format

[root@localhost ~]# od -t a /bin/cd 
0000000   #   !   /   b   i   n   /   s   h  nl   b   u   i   l   t   i
0000020   n  sp   c   d  sp   "   $   @   "  nl
0000032

Use the od -t c command to use ASCII output

[root@localhost ~]# od -t c /bin/cd 
0000000   #   !   /   b   i   n   /   s   h  \n   b   u   i   l   t   i
0000020   n       c   d       "   $   @   "  \n
0000032

Use the od -t x command to output the hex of the binary file.

[root@localhost ~]# od -t x /bin/cd
0000000 622f2123 732f6e69 75620a68 69746c69
0000020 6463206e 40242220 00000a22

Page through text: the command more/less is a text through view command.

[root@localhost ~]# more --help

Syntax format:[ more file name ]

        Space            #Like turning the next page
        B               #Turn up one page
        Enter           #scroll up one line
        /character string          #Find string down
        :f              #Show line numbers now
        q or Q            #sign out
[root@localhost ~]# less --help

Syntax format:[ less [option] file name ]

        -e              #Automatically exit after file display
        -f              #Force display file
        -l              #Ignore case differences when searching
        -N              #The line number is displayed at the beginning of each line

        Space             #Turn down one page
        [pageup]        #Turn up a page
        /character string          #Find string down
        ?character string          #Find string up
        q               #sign out

File patching: patch the specified configuration file, generate the difference file through comparison, and enter the patch

[root@localhost ~]# diff --help

Syntax format:[ diff [option] Source file new file > *.patch ]

        -a          #Treat any document as a text document
        -b          #Ignore the difference caused by spaces
        -B          #Ignore the difference caused by blank lines
        -I          #Ignore case differences
        -N          #When comparing directories, if a file is only in one directory, it will be regarded as an empty file in the other directory
        -r          #When comparing directories, recursively compare subdirectories
        -u          #Use the same output format

[root@localhost ~]# diff -Naur /root/old /root/new > lyshark.patch #Generate patch file
[root@localhost ~]# patch -p2 old < lyshark.patch                  #Additional patching