linux命令举例

  1. 文件系统
  • Ext4是常用的Linux文件系统类型,如下所示创建一个基本的Ext4文件系统:
sudo mkfs.ext4 /dev/sda1
  • 挂载一个文件系统,如将/dev/sda1挂载到/mnt目录:
sudo mount /dev/sda1 /mnt
  • 检查并修复文件系统:
sudo fsck /dev/sda1
  • 查看文件所在目录的大小:
du -h /path/to/file
  • 在已知目录下查找指定类型文件:
find /path/to/directory -type f -name "*.txt"
  • 查看当前目录所在的完整路径:
pwd
  • 查看当前文件系统的状态:
df -h
  • 使用rsync命令从源目录复制所有文件至目标目录:
rsync -av /path/to/source /path/to/destination
  • 把文件或目录镜像到另一个路径:
rsync -avz /path/to/source /path/to/destination
  • 按名称排序并显示文件和目录的详细信息:
ls -l | sort -k 9
  • 将文件从源目录同步到目标目录:
rsync -av --delete /path/to/source/ /path/to/destination/
  • 将多个文件合并为一个文件:
cat file1 file2 file3 > merged-file
  • 从文件列表中删除某个文件:
sed -i '/filename/d' filelist.txt
  • 从一个目录复制所有文件并保留时间戳:
cp -rp /path/to/source /path/to/destination
  • 显示当前目录大小:
du -sh
  • 查找文件并将结果保存到文件中:
find /path/to/directory -type f -name "*.txt" -print > filelist.txt
  • 检查目录权限:
ls -ld /path/to/directory
  • 列出当前目录下的所有文件及其大小:
ls -lh
  • 将文件夹转换为 tar 归档文件并压缩:
tar -czvf archive.tar.gz folder/
  • 查找指定目录下的文件,按文件大小排序:
find /path/to/directory -type f -exec ls -s {} \; | sort -n | cut -d" " -f2-
  • 将一个目录下的所有文件复制到另一个目录:
cp /path/to/source/* /path/to/destination/
  1. 文件和目录管理
  • 显示文件或目录的详细信息:
ls -l /path/to/file or directory
  • 更改文件或目录的所有权:
chown username:usergroup filename
  • 为文件或目录设置权限:
chmod u+rwx filename
  • 创建一个空的文件:
touch filename
  • 递归删除目录:
rm -r directoryname
  • 查找文件并在文件中搜索字符串:
grep "string" filename
  • 创建一个软链接:
ln -s /path/to/source /path/to/link
  • 用符号连接来将目录移动到另一个位置:
ln -s /path/to/target /path/to/new_location
  • 在命令行中查看文件的前n行:
head -n 5 filename
  • 将文件夹压缩为tar.gz文件:
tar -czvf filename.tar.gz foldername/
  • 列出当前目录中可执行的文件列表:
ls -l | grep -E "^(-rwx)|(^d)"
  • 在 Finder 中显示隐藏文件:
defaults write com.apple.finder AppleShowAllFiles true && killall Finder
  • 使用 touch 命令创建具有特定时间戳的文件:
touch -t 202305272359 filename
  • 使用 find 命令查找并删除所有特定类型的文件:
find /path/to/directory -type f -name "*.log*" -delete
  • 批量更改文件后缀名:
rename 's/.html/.php/' *.html
  • 将文件从源目录拷贝到目的地并保留文件权限:
cp -p /path/to/source /path/to/destination
  • 检查一个目录中是否存在文件:
if [[ $(ls -A /path/to/directory) ]]; then
    echo "The directory is not empty"
else
    echo "The directory is empty"
fi
  • 查找当前目录下的所有文件并将它们拷贝到新目录:
find . -type f -exec cp {} /path/to/newdir/ \;
  • 使用 rsync 命令同步两个目录:
rsync -avz /path/to/source/ /path/to/destination/
  • 找出文件夹中最新的文件:
ls -t | head -n1
  • 创建一个软链接:
ln -s /path/to/original /path/to/link
  1. Shell & Shell脚本
  • for循环的示例:
for i in 1 2 3 4 5
do
    echo "Number: $i"
done
  • 查找文件扩展名并将它们打印出来的示例:
find . -type f -name "*.txt" -print
  • 创建一个可以提供需要和总体完成进度的进度条的脚本:
#!/bin/bash
echo -n "Progress: "
while true; do
    echo -n "█"
    sleep 1
done
  • 通过Shell脚本将文件备份并进行压缩:
#!/bin/bash
cp -r /path/to/source /path/to/backup
tar -czvf backup.tar.gz /path/to/backup
  • 使用条件语句编写一个简单的脚本:
#!/bin/bash
if [ $(whoami) == "root" ]; then
    echo "You are root."
else
    echo "You are not root."
fi
  • 使用循环语句将当前目录中的所有文件重命名为大写字母:
#!/bin/bash
for i in *
do
    mv $i `echo $i | tr '[a-z]' '[A-Z]'`
done
  • 替换文件中的字符串:
sed -i 's/find_string/replace_string/g' filename
  • 通过脚本批量创建用户:
#!/bin/bash
while IFS=',' read -r username password
do
    useradd -m -s /bin/bash $username
    echo $password | passwd --stdin $username
done < userlist.csv
  • 使用条件语句和命令行参数编写一个脚本:
#!/bin/bash
if [ $1 -gt 10 ]; then
    echo "The number is greater than 10."
else
    echo "The number is less than or equal to 10."
fi
  • 使用循环和条件语句在命令行中实现一个石头、剪刀、布游戏:
#!/bin/bash
declare -a moves=("rock" "paper" "scissors")
computer=${moves[$RANDOM % ${#moves[@]} ]}
echo -n "Enter your move (rock,paper or scissors): "
read user
if [[ "$user" != "rock" && "$user" != "paper" && "$user" != "scissors" ]]; then
    echo "Invalid input. The choices are: rock, paper or scissors!"
    exit 1
fi
echo "You played: $user"
echo "The computer played: $computer"
if [[ "$user" == "$computer" ]]; then
    echo "It's a tie"
elif [[ "$user" == "rock" && "$computer" == "scissors" || "$user" == "scissors" && "$computer" == "paper" || "$user" == "paper" && "$computer" == "rock" ]]; then
    echo "You win!"
else
    echo "You lose!"
fi
  • 使用 awk 将 PS 输出的进程状态信息按照 CPU 工作时间进行排序:
ps aux | awk '{print $2, $3, $4, $11}' | sort -k3 -nr
  • 使用 while 循环,定时检查某个进程是否处于运行状态:
#!/bin/bash
while true
do
    if pgrep "process_name" > /dev/null
    then
        echo "Process is running"
    else
        echo "Process is not running"
    fi
    sleep 5
done
  • 使用 Bash 数组,对目录中的文件进行排序输出:
#!/bin/bash
declare -a files
i=0
for file in *
do
    files[i]=$file
    ((i++))
done
IFS=$'\n' sorted=($(sort <<<"${files[*]}"))
unset IFS
for file in "${sorted[@]}"
do
    echo "$file"
done
  • 在 Shell 脚本中使用 for 循环和 curl 命令做数据抓取:
#!/bin/bash
for i in {1..5}
do
    url="http://example.com/page$i.html"
    contents=$(curl $url)
    echo "$contents" >> output.txt
done
  • 从CSV文件中读取行,解析并打印出内容:
#!/bin/bash
while IFS=',' read -r lastname firstname email
do
    echo "Hello $firstname $lastname"
    echo "Your email is $email"
done < input.csv
  • 使用 awk 在文件内容中查找匹配项:
awk '/pattern/ {print}’ filename
  • 使用 Bash 条件语句检查特定目录的大小:
#!/bin/bash
directory=/path/to/directory
maxsize=1000 # in KB
actualsize=$(du -k "$directory" | cut -f1)
if [ $actualsize -ge $maxsize ]; then
    echo "Directory size is greater than or equal to $maxsize KB"
else
    echo "Directory size is less than $maxsize KB"
fi
  • 使用 grep 查找文件中的特定字符串:
grep "string" filename
  • 读取文件中的行:
#!/bin/bash
cat filename.txt | while read line
do
    echo "Line: $line"
done
  • 对目录中文件的扩展名进行更改:
#!/bin/bash
for file in *.old
do
    mv "$file" "${file%.old}.new"
done
  1. 系统设置
  • 查看系统硬件信息:
sudo lshw
  • 添加一个新用户:
sudo useradd -m -s /bin/bash username
  • 启用一个服务:
sudo systemctl start servicename
  • 更改系统时区:
sudo timedatectl set-timezone America/New_York
  • 执行定时任务:
crontab -e
  • 显示系统版本信息:
uname -a
  • 查看CPU信息:
cat /proc/cpuinfo
  • 显示当前系统的DNS配置:
cat /etc/resolv.conf
  • 同步系统时间:
ntpdate time.nist.gov
  • 检查系统性能:
top
  • 检查硬件信息:
lshw -short
  • 显示系统进程树:
pstree
  • 使用系统日志来查看错误信息:
tail -f /var/log/syslog
  • 显示系统内存详细信息:
free -m
  • 按 QoS 类型分类打印网络流量信息:
iftop -F icmp
  • 显示正在运行的进程列表:
ps aux
  • 显示系统日志的最近100行:
tail -n 100 /var/log/syslog
  • 查看系统的 CPU 信息:
cat /proc/cpuinfo
  • 查看系统的文件系统信息:
df -h
  • 列出当前系统上正在运行的所有服务:
systemctl list-units --type=service
  • 显示系统的启动时间:
systemd-analyze
  1. 网络
  • 使用 ping 命令测试网络连接是否正常:
ping www.google.com
  • 通过设置IP地址,子网掩码和网关来配置网络接口:
sudo ifconfig eth0 192.168.1.2 netmask 255.255.255.0 up
sudo route add default gw 192.168.1.1 eth0
  • 测试TCP端口:
telnet hostname/ip-address port
  • 查看当前系统网络状态:
netstat -anp
  • 测试DNS解析:
nslookup hostname
  • 检查进程的连接状态:
netstat -lnp
  • 经过身份验证的SSH登录:
ssh username@remotehost
  • 使用iptables开启端口:
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
  • 设置网卡IP地址:
ifconfig eth0 192.168.1.2 netmask 255.255.255.0 up
  • 在命令行中打开Web浏览器:
lynx http://www.google.com
  • 配置网络接口:
sudo nano /etc/network/interfaces
  • 使用 cURL 命令从 Web 服务器上下载文件:
curl -O http://example.com/file.zip
  • 检查本地 IP 地址:
ifconfig | grep inet | awk '{ print $2 }'
  • 使用 SSH 登录远程主机:
ssh username@remote_host
  • 使用 traceroute 命令检查路由路径:
traceroute example.com
  • 通过 IP 指定主机名:
sudo nano /etc/hosts
  • 使用 ping 命令测试网络连通性:
ping example.com
  • 使用 netstat 命令查看网络连接:
netstat -an
  • 显示当前系统的 IP 地址:
hostname -I
  • 显示指定端口的进程:
lsof -i :port_number
  1. 软件包管理
  • 使用 apt-get 安装 vim 编辑器:
sudo apt-get update
sudo apt-get install vim
  • 使用 yum 升级所有已安装的软件包:
sudo yum update
  • 搜索软件包:
apt-cache search packagename
  • 更新软件包并重新安装所有已安装版本:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get --reinstall install packagename
  • 从软件包仓库下载软件源码并进行编译:
wget http://ftp.gnu.org/gnu/tar/tar-1.32.tar.gz
tar zxvf tar-1.32.tar.gz
cd tar-1.32/
./configure
make
make install
  • 卸载软件包:
sudo apt-get remove packagename
  • 安装Debian软件包:
wget http://ftp.debian.org/debian/pool/main/e/emacs25/emacs25_25.2.orig.tar.gz
tar xfz emacs25_25.2.orig.tar.gz
cd emacs-25.2
./configure
make
sudo make install
  • 升级已安装的软件包到最新版本:
sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
  • 构建Debian软件包:
sudo apt-get build-dep packagename
apt-get source packagename
cd packagename-version/
dpkg-buildpackage -rfakeroot -uc -b
  • 安装Java运行时环境:
sudo apt-get update
sudo apt-get install default-jre
  • 从源代码构建软件包:
wget http://ftp.gnu.org/gnu/wget/wget-1.21.tar.gz
tar zxvf wget-1.21.tar.gz
cd wget-1.21
./configure
make
sudo make install
  • 显示所有已安装的软件包:
dpkg -l
  • 搜索可用的 Linux 软件包:
apt-cache search packagename
  • 停止或启动某个服务:
sudo systemctl stop servicename
sudo systemctl start servicename
  • 安装 Python 3:
sudo apt-get update
sudo apt-get install python3
  • 安装 Node.js:
sudo apt-get update
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
sudo apt-get install -y nodejs
  • 安装 Apache web 服务器:
sudo apt update
sudo apt install apache2
  • 查找某个Linux命令所在的软件包:
sudo apt-file search command
  • 安装 MongoDB 数据库:
sudo apt update
sudo apt install mongodb
  • 搜索Ubuntu软件包并查看包的详细信息:
apt-cache search package_name
apt-cache show package_name

……

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值