处理文件和目录
在这一步中,你将学习在 Linux 中管理文件和目录的基本命令。
创建和导航目录
mkdir
命令用于创建新目录,而 cd
命令允许你切换目录。
- 让我们为练习创建一个新目录:
cd ~/project
mkdir linux_practice
- 进入新目录:
cd linux_practice
- 确认你已进入正确的目录:
pwd
你应该会看到:
/home/labex/project/linux_practice
创建和查看文件
现在,让我们在这个目录中处理一些文件。
- 使用
echo
命令和输出重定向创建一个简单的文本文件:
echo "Linux is a powerful operating system." > about_linux.txt
- 使用追加重定向运算符 (
>>
) 向文件中添加另一行内容:
echo "It is open-source and widely used in servers and embedded systems." >> about_linux.txt
- 使用
cat
命令查看文件内容:
cat about_linux.txt
你应该会看到:
Linux is a powerful operating system.
It is open-source and widely used in servers and embedded systems.
复制和移动文件
cp
命令用于复制文件,而 mv
命令用于移动或重命名文件。
- 让我们复制我们的文件:
cp about_linux.txt linux_copy.txt
- 验证两个文件是否都存在:
ls
你应该会看到:
about_linux.txt linux_copy.txt
- 现在,让我们重命名复制的文件:
mv linux_copy.txt linux_benefits.txt
- 修改重命名后文件的内容:
echo "Benefits of Linux include security, stability, and flexibility." > linux_benefits.txt
- 验证两个文件的内容:
echo "Content of about_linux.txt:"
cat about_linux.txt
echo -e "\nContent of linux_benefits.txt:"
cat linux_benefits.txt
你应该会看到:
Content of about_linux.txt:
Linux is a powerful operating system.
It is open-source and widely used in servers and embedded systems.
Content of linux_benefits.txt:
Benefits of Linux include security, stability, and flexibility.
- 返回项目目录:
cd ~/project