Comprehensive Guide to the Linux Directory Command

Create DirectoryCreate a subdirectory in the current directory

# Create a subdirectory named mengyuan in the current directory
mkdir ./mengyuan

Create multiple levels of subdirectories in the current directory

# Create a subdirectory named mengnianxi in the subdirectory named mengyuan in the current directory
# Note: With the -p parameter, if the subdirectory named mengyuan does not exist, it will be created automatically (implying: the -p parameter will automatically create all parent directories of the last level)
mkdir -p ./mengyuan/mengnianxi

Create a subdirectory under a specified parent directory

# Create a subdirectory named mengyuan under the subdirectory named usr in the system root directory
mkdir /usr/mengyuan

Delete DirectoryDelete an empty directory

# Delete the empty subdirectory named mengyuan in the current directory
rmdir ./mengyuan

Delete a directory with content

# Delete the subdirectory named mengyuan in the current directory, and its contents will also be deleted
rm -r ./mengyuan

Delete all contents in the current directory

# Delete all contents in the current directory
rm -r ./*

Delete all contents in the system root directory (this operation is prohibited, as it will crash the system)

# Delete all contents in the system root directory
rm -r /*

Delete a directory without manual confirmation

# After adding the -f parameter, there is no need to confirm whether to delete after executing the delete command
rm -rf ./mengyuan

Copy DirectoryCopy a directory and its contents

# Copy the subdirectory named mengyuan and its contents in the current directory to a new directory named mengnianxi in the current directory
cp -r ./mengyuan ./mengnianxi

Copy a directory and its contents to another directory

# Copy the subdirectory named mengyuan and its contents in the current directory to the subdirectory named usr in the system root directory, keeping the directory name as mengyuan
cp -r ./mengyuan /usr/

Show progress bar while copying a directory

# Show progress bar while copying a directory
cp -rv ./mengyuan ./mengnianxi

Preserve original attributes while copying a directory

# Preserve original attributes while copying a directory
# Original attributes include but are not limited to: read permissions, write permissions, execute permissions, owner, group, last modified time, etc.
cp -rp ./mengyuan ./mengnianxi

Move DirectoryRename a directory

# Rename the subdirectory named mengyuan in the current directory to mengnianxi
mv ./mengyuan ./mengnianxi

Move a directory (change directory name)

# Move the subdirectory named mengyuan in the current directory to the subdirectory named usr in the system root directory, renaming it to mengnianxi
mv ./mengyuan /usr/mengnianxi

Move a directory (without changing directory name)

# Move the subdirectory named mengyuan in the current directory to the subdirectory named usr in the system root directory
mv ./mengyuan /usr/

Show progress bar while moving a directory

# Show progress bar while moving a directory
mv -v ./mengyuan /usr/

Change DirectoryEnter a subdirectory

# Enter the subdirectory named mengyuan (can be multi-level)
# Method 1
cd ./mengyuan
# Method 2
cd mengyuan

Enter the parent directory

# Enter the parent directory of the current directory
cd ../

Enter the system root directory

# Enter the system root directory
cd /

Enter a specified directory

# Enter the subdirectory named mengyuan under the subdirectory named usr in the system root directory
cd /usr/mengyuan

Enter the user’s home directory

# Enter the user's home directory
cd ~

Switch to the last accessed directory

## Switch to the last accessed directory
## For example: if the last accessed directory path was /usr/mengyuan, and the current directory is /root. After using this command, the working directory will switch to /usr/mengyuan
cd -

Display the current directory

# Display the current directory
# For example: if the current directory is /usr/mengyuan, it will print /usr/mengyuan
pwd

Show directory and file listShow non-hidden directory and file list

# Show all non-hidden subdirectories and files in the current directory
ls ./

Show all directory and file lists

# Show all subdirectories and files in the current directory
ls -a ./

Show detailed information when displaying directory and file lists

# Show detailed information when displaying directories and files
# Detailed information includes: read permissions, write permissions, execute permissions, owner, group, file size, last modified time, file name
# Can be abbreviated as ll
ls -l

Show friendly detailed information when displaying directory and file lists

# When showing detailed information, calculate file sizes, e.g., 1.0M, 2.15G, etc., which is more human-readable
ls -lh

Display directory and file lists in descending order by last modified time

# Display directory and file lists in descending order by last modified time
ls -t

Display directory and file lists in ascending order by last modified time

# Display directory and file lists in ascending order by last modified time
ls -tr

View help information

# View help information
ls --help

Find subdirectories and filesSearch by exact name

# Search for subdirectories and files named trace.log in the current directory and its subdirectories (recursive)
find ./ -name "trace.log"

Search by name prefix

# Search for subdirectories and files whose names start with trace in the current directory and its subdirectories (recursive)
find ./ -name "trace*"

Display detailed results when searching by name

# Search for subdirectories and files named trace.log in the current directory and its subdirectories (recursive), displaying detailed information
find ./ -name "trace.log" -ls

Search by owner

# Search for subdirectories and files owned by mengyuan in the current directory and its subdirectories (recursive)
find ./ -user menyuan

Search by group

# Search for subdirectories and files belonging to the group mengyuan in the current directory and its subdirectories (recursive)
find ./ -group mengyuan

Create a symbolic link

# Create a symbolic link for the subdirectory named mengyuan under the subdirectory named root in the system root directory
# Place this symbolic link in the subdirectory named usr in the system directory, naming the symbolic link mengnianxi
ln -s /root/mengyuan /usr/mengnianxi

Leave a Comment