Ubuntu Linux 常用命令详解:从入门到精通
引言
Linux作为开源操作系统的代表,在服务器、云计算和开发领域占据着主导地位。Ubuntu作为最流行的Linux发行版之一,以其易用性和稳定性受到广泛欢迎。掌握Linux命令行的使用,不仅能提高工作效率,更是每位开发者和系统管理员的必备技能。
本文将详细介绍Ubuntu系统中最常用的命令,并通过实例演示它们的用法。
基础文件操作命令
1. 目录导航
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| pwd
cd /home/user/Documents cd .. cd ~ cd -
ls ls -l ls -a ls -lh
|
2. 文件操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| touch filename.txt touch file1.txt file2.txt
mkdir new_folder mkdir -p path/to/nested/folder
cp file1.txt file2.txt cp -r folder1 folder2
mv oldname.txt newname.txt mv file.txt /target/directory/
rm file.txt rm -r folder rm -rf folder
cat file.txt less file.txt head -n 10 file.txt tail -n 10 file.txt tail -f logfile.log
|
系统信息查询命令
1. 系统状态监控
1 2 3 4 5 6 7 8 9 10 11 12 13
| uname -a lsb_release -a
df -h du -sh folder/ du -h --max-depth=1
free -h top htop
|
2. 进程管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ps aux ps aux | grep nginx
kill 1234 killall process_name pkill -f pattern
sleep 100 & jobs fg %1 bg %1
|
文本处理神器
1. 文本搜索和过滤
1 2 3 4 5 6 7 8 9 10 11 12 13
| grep "pattern" file.txt grep -r "error" /var/log/ grep -i "warning" file.txt grep -v "exclude" file.txt
sed 's/old/new/g' file.txt sed -n '10,20p' file.txt
awk '{print $1}' file.txt awk -F: '{print $1}' /etc/passwd
|
2. 文本排序和统计
1 2 3 4 5 6 7 8 9
| sort file.txt sort -u file.txt uniq file.txt
wc -l file.txt wc -w file.txt wc -c file.txt
|
网络相关命令
1. 网络诊断
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| ping google.com ping -c 4 google.com
traceroute google.com tracepath google.com
netstat -tulpn ss -tulpn
nslookup google.com dig google.com
|
2. 文件传输
1 2 3 4 5 6 7 8 9 10 11
| wget https://example.com/file.zip wget -c https://example.com/file.zip
scp file.txt user@remote:/path/ scp user@remote:/path/file.txt ./
ssh user@remote_host ssh -p 2222 user@remote_host
|
包管理和软件安装
Ubuntu APT包管理器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| sudo apt update
sudo apt upgrade sudo apt full-upgrade
sudo apt install package_name sudo apt remove package_name sudo apt purge package_name
apt search keyword apt show package_name
sudo apt autoremove sudo apt clean
|
权限管理
1. 文件和目录权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| ls -l file.txt
chmod 755 script.sh chmod u+x script.sh chmod g-w file.txt
chown user:group file.txt sudo chown -R user:group folder/
chmod +s executable
|
2. 用户和组管理
1 2 3 4 5 6 7 8
| sudo adduser newuser sudo deluser username sudo passwd username
sudo addgroup newgroup sudo usermod -aG groupname username
|
实用技巧和小贴士
1. 命令行效率提升
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| history !grep Ctrl + R
Ctrl + A Ctrl + E Ctrl + U Ctrl + K
command1 && command2 command1 || command2 command1 ; command2
ls > file.txt ls >> file.txt grep "pattern" < file.txt
|
2. 压缩和解压
1 2 3 4 5 6 7 8 9 10 11 12
| tar -czvf archive.tar.gz folder/ tar -xzvf archive.tar.gz tar -cjvf archive.tar.bz2 folder/
zip -r archive.zip folder/ unzip archive.zip
7z x archive.7z unrar x archive.rar
|
系统服务管理(systemd)
1 2 3 4 5 6 7 8 9 10 11 12 13
| sudo systemctl start servicename sudo systemctl stop servicename sudo systemctl restart servicename sudo systemctl status servicename
sudo systemctl enable servicename sudo systemctl disable servicename
sudo journalctl -u servicename sudo journalctl -f
|
结语
掌握这些Linux命令将极大提高你在Ubuntu系统上的工作效率。建议在实际使用中不断练习,逐渐熟悉每个命令的选项和用法。记住,Linux命令行最强大的地方在于可以将多个命令组合使用,通过管道和重定向完成复杂的任务。
重要提醒:在使用rm -rf、chmod、chown等可能造成系统问题的命令时,务必确认操作对象,避免误操作导致数据丢失或系统故障。
希望这篇指南能帮助你更好地使用Ubuntu系统!Happy coding! 🐧