Location>code7788 >text

Linux directory management commands

Popularity:314 ℃/2025-02-14 18:18:34
# Case 1: View the directory where the cd, cd ~, cd /root commands are executed
 [root@yyds tmp]# pwd
 /tmp
 [root@yyds tmp]# cd
 [root@yyds ~]# pwd
 /root
 [root@yyds tmp]# cd ~
 [root@yyds ~]# pwd
 /root
 [root@yyds tmp]# cd /root
 [root@yyds ~]# pwd
 /root

 #Case 2: Use absolute path and switch to /etc directory
 [root@yyds ~]# cd /etc/
 [root@yyds etc]# pwd
 /etc

 # Case 3: Use the relative path and switch to the /etc directory
 [root@yyds ~]# cd /
 [root@yyds /]# cd etc/
 [root@yyds etc]# pwd
 /etc

 # Case 4: Switch to /etc/sysconfig/network-scripts/
 [root@yyds ~]# cd /etc/sysconfig/network-scripts/
 [root@yyds network-scripts]# pwd
 /etc/sysconfig/network-scripts

 # Case 5: Quick Return to Home Directory
 [root@yyds network-scripts]# cd --->>Method 1
 [root@yyds ~]# cd - --->>Return to the directory you were in last time
 /etc/sysconfig/network-scripts
 [root@yyds network-scripts]# cd ~ --->>Method 2
 [root@yyds ~]# cd - --->>Return to the directory you were in last time
 /etc/sysconfig/network-scripts
 [root@yyds network-scripts]# cd /root --->>Method Three
 [root@yyds ~]#

 # Case 6: Return to the last directory
 [root@yyds ~]# cd -
 /etc/sysconfig/network-scripts

 # Case 7: Return to the previous directory
 [root@yyds network-scripts]# cd ..
 [root@yyds sysconfig]# pwd
 /etc/sysconfig

 # Case 8: Return/Table of Contents
 [root@yyds sysconfig]# cd / --->>Method 1
 [root@yyds /]# cd - --->>Return to the directory you were in last time
 /etc/sysconfig
 [root@yyds sysconfig]# cd ../.. --->>Method 2
 [root@yyds /]# pwd
 /

3. ls: View all files and their attribute information in the current directory

3.1 Syntax Structure

ls 【parameters】【file】
  ls [File] --->> Check whether the file exists
  ls [Catalog] --->> Check whether the directory exists
  ls [File 1] [File 1] [File 1] --->>View multiple files

3.2 Parameter Options

Parameter Options

Notes

Parameter Options

Notes

-l

View detailed attribute information of the file️❤️

-S

Recursively display all subdirectories❤️

-a

Show hidden files❤️

-t

Sort the files according to the last modification time

-i

Display the inode attribute information of the file️❤️

-R

Recursively display all subfiles

-d

Display the directory's own attribute information️

-r

Sort in reverse order❤️

Case practice:

# Case 1: Check whether the /etc/hosts file exists
 [root@yyds ~]# ls -l /etc/hosts
 -rw-r--r-- 1 root root 158 ​​June 23 2020 /etc/hosts

 # Case 2: Check whether the two files /etc/hosts /etc/passwd exist
 [root@yyds ~]# ls -l /etc/hosts /etc/passwd
 -rw-r--r-- 1 root root 158 ​​June 23 2020 /etc/hosts
 -rw-r--r-- 1 root root 1809 June 30 23:08 /etc/passwd

 # Case 3: Check whether the yyds and /etc/passwd files in the current location exist
 [root@yyds ~]# ls -l yyds /etc/passwd
 ls: Unable to access 'yyds': No file or directory
 -rw-r--r-- 1 root root 1809 June 30 23:08 /etc/passwd

 # Case 4: Show all contents in /tmp directory
 [root@yyds ~]# ls /tmp --->>Method 1

 -FKCAqo
 -lfit7q
 -ZzP9XJ
 [root@yyds ~]# cd /tmp --->>Method 2
 [root@yyds tmp]# ls

 -FKCAqo
 -lfit7q
 -ZzP9XJ

 # Case 5: Show all files downloaded by /etc/sysconfig/network-scripts
 [root@yyds ~]# ls /etc/sysconfig/network-scripts
 ifcfg-ens33

 # Case 6: Show all files under /root, including hidden files
 [root@yyds ~]# ls -a /root
 .bash_history .bash_profile .gnupg .tcshrc www
 .. .bash_logout .bashrc .cshrc .viminfo

 # Case 7: Display the inode of all files under /root
 [root@yyds ~]# ls -i
 101033767 102489504 101033761 102489508 34091944 www

4. mkdir: Create directory

4.1 Syntax Structure

mkdir [Parameter options] [Directory name]

 mkdir [Catalog]
 mkdir [Catalog 1] [Catalog 2]
 mkdir [parameter options] [catalog]

4.2 Parameter Options

Parameter Options

Notes

Parameter Options

Notes

-m

Set permissions while creating a directory❤️

-v

Display execution process details

-p

Recursively create multi-level directories❤️

4.3 Case Practice

# Case 1. Create the yyds directory currently
 [root@yyds ~]# mkdir yyds
 [root@yyds ~]# ll
 Total dosage 0
 drwxr-xr-x 2 root root 6 July 2 04:40 yyds

 # Case 2. Create multiple directories at present
 #Method 1:
 [root@yyds ~]# mkdir a b c
 [root@yyds ~]# ll
 Total dosage 0
 drwxr-xr-x 2 root root 6 July 2 04:41 a
 drwxr-xr-x 2 root root 6 July 2 04:41 b
 drwxr-xr-x 2 root root 6 July 2 04:41 c
 drwxr-xr-x 2 root root 6 July 2 04:40 yyds
 #Method 2:
 [root@yyds ~]# mkdir {a..c}
 [root@yyds ~]# ll
 Total dosage 0
 drwxr-xr-x 2 root root 6 July 2 04:42 a
 drwxr-xr-x 2 root root 6 July 2 04:42 b
 drwxr-xr-x 2 root root 6 July 2 04:42 c
 drwxr-xr-x 2 root root 6 July 2 04:40 yyds

 # Case 3. Create a directory in /tmp directory
 [root@yyds ~]# mkdir /tmp/a
 [root@yyds ~]# ll /tmp/
 Total dosage 0
 drwxr-xr-x 2 root root 40 July 2 04:44 a

 # Case 4. Create yyds under /tmp/ Create oldgirl under /opt/
 [root@yyds ~]# mkdir /tmp/yyds /opt/oldgirl
 [root@yyds ~]# ll /tmp/ /opt/
 /opt/:
 Total dosage 0
 drwxr-xr-x 2 root root 6 July 2 04:45 oldgirl

 /tmp/:
 Total dosage 0
 drwxr-xr-x 2 root root 40 July 2 04:44 a
 drwxr-xr-x 2 root root 40 july 2 04:45 yyds

 # Case 5. Create the yyds/a/b/c directory currently
 [root@yyds ~]# mkdir -p yyds/a/b/c
 [root@yyds ~]# tree
 .
 └── yyds
     └── a
         └── b
             └── c

 4 directories, 0 files

5. cp: Copy files or directories

5.1 Syntax Structure

cp [parameter options] [source file] [target file]

 cp [source file] [target file]
 cp [source file] [modified file name]
 cp [File 1] [File 2] [File 3] [Catalog]
 cp [parameters] [source file] [target file]

5.2 Parameter Options

Parameter Options

Notes

Parameter Options

Notes

-a

Equivalent to pdr parameter combination

-p

Keep all properties of the source or directory file❤️

-r

Recursively copy the directory and all subfiles under it❤️

-i

If the target file already exists, it will be asked whether to overwrite it.

-b

Backup before overwriting the target file

-f

If the target file already exists, it will be directly overwritten.

-t

Swap the source and target files in locations❤️

5.3 Case Practice

# Case 1. Create Copy to /opt directory
 [root@yyds ~]# touch
 [root@yyds ~]# ll
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 04:54
 [root@yyds ~]# cp /opt/
 [root@yyds ~]# ll /opt/
 -rw-r--r-- 1 root root 0 July 2 04:54

 # Case 2. Copy multiple files to /opt directory
 [root@yyds ~]# touch {a..c}.txt
 [root@yyds ~]# ll
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:54
 [root@yyds ~]# cp /opt/
 [root@yyds ~]# ll /opt/
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 04:56
 -rw-r--r-- 1 root root 0 July 2 04:56
 -rw-r--r-- 1 root root 0 July 2 04:56
 -rw-r--r-- 1 root root 0 July 2 04:54

 # Case 3. Copy files /etc/hosts /tmp/ in different directories to /opt directory
 [root@yyds ~]# cp /etc/passwd /tmp/ /opt/
 [root@yyds ~]# ll /opt/
 Total dosage 4
 -rw-r--r-- 1 root root 0 July 2 05:01
 drwxr-xr-x 2 root root 6 July 2 04:45 oldgirl
 -rw-r--r-- 1 root root 1856 July 2 05:01 passwd

 # After the case is renamed, and the current backup will be
 [root@yyds ~]# cp
 [root@yyds ~]# ll
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:54
 -rw-r--r-- 1 root root 0 July 2 05:02

 # Case 5. Copy to /opt and name it
 [root@yyds ~]# cp /opt/
 [root@yyds ~]# ll /opt/
 Total dosage 4
 -rw-r--r-- 1 root root 0 July 2 05:01
 -rw-r--r-- 1 root root 0 July 2 05:02
 drwxr-xr-x 2 root root 6 July 2 04:45 oldgirl
 -rw-r--r-- 1 root root 1856 July 2 05:01 passwd

 # Case 6. Copy the /opt directory to the current directory
 #Method 1:
 [root@yyds ~]# cp /opt/ ./
 [root@yyds ~]# ll
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:54
 -rw-r--r-- 1 root root 0 July 2 05:05
 #Method 2:
 [root@yyds ~]# cp /opt/ .
 [root@yyds ~]# ll
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:54
 -rw-r--r-- 1 root root 0 July 2 05:06
 #Method Three:
 [root@yyds ~]# cp /opt/ ~
 [root@yyds ~]# ll
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:54
 -rw-r--r-- 1 root root 0 July 2 05:07
 #Method 4:
 [root@yyds ~]# cp /opt/ /root/
 [root@yyds ~]# ll
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:54
 -rw-r--r-- 1 root root 0 July 2 05:08

 # Case 7. Copy multiple files to the current location
 [root@yyds ~]# touch /tmp/{1..3}.txt
 [root@yyds ~]# ll /tmp/
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 05:09
 -rw-r--r-- 1 root root 0 July 2 05:09
 -rw-r--r-- 1 root root 0 July 2 05:09
 -rw-r--r-- 1 root root 0 July 2 04:59
 [root@yyds ~]# cp /tmp//{1..3}.txt .
 [root@yyds ~]# ll
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 05:10
 -rw-r--r-- 1 root root 0 July 2 05:10
 -rw-r--r-- 1 root root 0 July 2 05:10
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:54
 -rw-r--r-- 1 root root 0 July 2 05:08

 # Case 8. Copy the yyds directory to /opt
 [root@yyds ~]# mkdir yyds
 [root@yyds ~]# ll
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 05:10
 -rw-r--r-- 1 root root 0 July 2 05:10
 -rw-r--r-- 1 root root 0 July 2 05:10
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 -rw-r--r-- 1 root root 0 July 2 04:55
 drwxr-xr-x 2 root root 6 July 2 05:11 yyds
 [root@yyds ~]# cp -r yyds /opt/
 [root@yyds ~]# ll /opt/
 Total dosage 4
 -rw-r--r-- 1 root root 0 July 2 05:01
 drwxr-xr-x 2 root root 6 July 2 05:11 yyds
 -rw-r--r-- 1 root root 0 July 2 05:02
 drwxr-xr-x 2 root root 6 July 2 04:45 oldgirl
 -rw-r--r-- 1 root root 1856 July 2 05:01 passwd

 # Case 9. Copy multiple directories to /opt
 [root@yyds ~]# mkdir {A..C}
 [root@yyds ~]# ll
 Total dosage 0
 drwxr-xr-x 2 root root 6 July 2 05:13 A
 drwxr-xr-x 2 root root 6 July 2 05:13 B
 drwxr-xr-x 2 root root 6 July 2 05:13 C
 [root@yyds ~]# rm -rf /opt/*
 [root@yyds ~]# ll /opt/
 Total dosage 0
 [root@yyds ~]# cp -r {A..C} /opt/
 [root@yyds ~]# ll /opt/
 Total dosage 0
 drwxr-xr-x 2 root root 6 July 2 05:14 A
 drwxr-xr-x 2 root root 6 July 2 05:14 B
 drwxr-xr-x 2 root root 6 July 2 05:14 C

 # Case 10. Copy etc directory to the current location
 [root@yyds ~]# cp -r /etc/ .
 [root@yyds ~]# ll
 Total dosage 12
 drwxr-xr-x 2 root root 6 July 2 05:13 A
 drwxr-xr-x 2 root root 6 July 2 05:13 B
 drwxr-xr-x 2 root root 6 July 2 05:13 C
 drwxr-xr-x 124 root root 8192 july 2 05:15 etc
 [root@yyds ~]# ll etc/
 Total dosage 1308
 drwxr-xr-x 3 root root 101 july 2 05:15 abrt
 -rw-r--r-- 1 root root 16 July 2 05:15 adjtime
 -rw-r--r-- 1 root root 1529 July 2 05:15 aliases
 ...Omitted

6. rm: delete files or directories

6.1 Syntax Structure

rm 【parameter options】【file name】

6.2 Parameter Options

Parameter Options

Notes

Parameter Options

Notes

-d

Only delete empty directories without sub-files

-f

Forced deletion without asking

-r

Recursively delete the directory and all subfiles inside it

6.3 Case Practice

# Case 1. Delete the current yyds
 [root@yyds ~]# ll
 Total dosage 0
 drwxr-xr-x 3 root root 15 july 2 05:56 yyds
 [root@yyds ~]# rm -rf yyds/
 [root@yyds ~]# ll
 Total dosage 0

 # Case 2. Delete the opt directory
 [root@yyds ~]# ll /opt/
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 05:52
 -rw-r--r-- 1 root root 0 July 2 05:50
 -rw-r--r-- 1 root root 0 July 2 05:50
 -rw-r--r-- 1 root root 0 July 2 05:50
 drwxr-xr-x 2 root root 6 July 2 05:53 yyds
 -rw-r--r-- 1 root root 0 July 2 05:48
 [root@yyds ~]# rm /opt/ /opt/
 rm: Should I delete the normal empty file '/opt/'?  y
 rm: Should I delete the normal empty file '/opt/'?  y
 [root@yyds ~]# ll /opt/
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 05:52
 -rw-r--r-- 1 root root 0 July 2 05:50
 drwxr-xr-x 2 root root 6 July 2 05:53 yyds
 -rw-r--r-- 1 root root 0 July 2 05:48

7. mv: Move files or directories

7.1 Syntax Structure

mv [parameter options] [source file] [target file]

 mv [source file] [target file]
 mv [source file] [modified file name]
 mv [File 1] [File 2] [File 3] [Catalog]
 mv [parameters] [source file] [target file]

7.2 Parameter Options

Parameter Options

Notes

Parameter Options

Notes

-b

Create a backup for the target file before overwriting

-f

Force overwrite the file without asking

-n

Do not overwrite existing files

7.3 Case Practice

# Case 1. Move the current to /opt directory
 [root@yyds ~]# touch
 [root@yyds ~]# ll
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 05:48
 [root@yyds ~]# mv /opt/
 [root@yyds ~]# ll /opt/
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 05:48

 # Case 2. Move multiple files
 [root@yyds ~]# touch {a..c}.txt
 [root@yyds ~]# ll
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 05:50
 -rw-r--r-- 1 root root 0 July 2 05:50
 -rw-r--r-- 1 root root 0 July 2 05:50
 [root@yyds ~]# mv {a..c}.txt /opt/
 [root@yyds ~]# ll /opt/
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 05:50
 -rw-r--r-- 1 root root 0 July 2 05:50
 -rw-r--r-- 1 root root 0 July 2 05:50
 -rw-r--r-- 1 root root 0 July 2 05:48

 # Case 3. Move to /opt/ directory and name it
 [root@yyds ~]# touch
 [root@yyds ~]# mv /opt/
 [root@yyds ~]# ll /opt/
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 05:52
 -rw-r--r-- 1 root root 0 July 2 05:50
 -rw-r--r-- 1 root root 0 July 2 05:50
 -rw-r--r-- 1 root root 0 July 2 05:50
 -rw-r--r-- 1 root root 0 July 2 05:48

 # Case 4. Move the directory yyds to /opt
 [root@yyds ~]# mkdir yyds
 [root@yyds ~]# ll
 Total dosage 0
 drwxr-xr-x 2 root root 6 July 2 05:53 yyds
 [root@yyds ~]# mv yyds/ /opt/
 [root@yyds ~]# ll /opt/
 Total dosage 0
 -rw-r--r-- 1 root root 0 July 2 05:52
 -rw-r--r-- 1 root root 0 July 2 05:50
 -rw-r--r-- 1 root root 0 July 2 05:50
 -rw-r--r-- 1 root root 0 July 2 05:50
 drwxr-xr-x 2 root root 6 July 2 05:53 yyds
 -rw-r--r-- 1 root root 0 July 2 05:48

⚠️⚠️⚠️Use mv instead of rm in enterprise

8. tree: a tree structure lists the contents of the directory

8.1 Syntax Structure

tree 【parameter options】【Catalog】

8.2 Parameter Options

Parameter Options

Notes

Parameter Options

Notes

-L

Use hierarchy to display content❤️

-F

Show the complete path of the file

-d

Show directory names only

8.3 Case Practice

# Case 1.
 [root@yyds ~]# mkdir -p yyds/a/b/c
 [root@yyds ~]# tree
 .
 └── yyds
     └── a
         └── b
             └── c

 4 directories, 0 files

 # Case 2.
 [root@yyds ~]# tree -L 1 /
 /
 ├──backup
 ├── bin -> usr/bin
 ├── boot
 ├── box
 ├── dev
 ├── etc
 ├── home
 ├── lib -> usr/lib
 ├── lib64 -> usr/lib64
 ├── media
 ├── mnt
 ├── opt
 ├── proc
 ├── root
 ├── run
 ├── sbin -> usr/sbin
 ├── srv
 ├── sys
 ├── tmp
 ├── usr
 └── var

 21 directories, 0 files

9. du: Check the directory size

9.1 Syntax format

du [parameters] [file name]

9.2 Parameter Options

Parameter Options

Notes

Parameter Options

Notes

-a

Displays the size of all files in the directory

-s

Show the total size of the subdirectory❤️

-h

Display in a readable way❤️

9.3 Practical Cases

[root@linux:~]# du -sh /etc
29M	/etc