Common shell commands

Jun 29, 2019 am 10:09 AM

Common shell commands

下面为大家总结Linux shell的常用命令

前言

使用Linux shell是一些程序员每天的基本工作,但我们经常会忘记一些有用的shell命令和技巧。当然,命令我能记住,但我不敢说能记得如何用它执行某个特定任务。需要注意一点的是,有些用法需要在你的Linux系统里安装额外的软件。下面话不多说了,来看看详细的内容吧。

检查远程端口是否对bash开放:

echo >/dev/tcp/8.8.8.8/53 && echo "open"
Copy after login

让进程转入后台:

Ctrl + z
Copy after login

将进程转到前台:

fg
Copy after login

产生随机的十六进制数,其中n是字符数:

openssl rand -hex n
Copy after login

在当前shell里执行一个文件里的命令:

source /home/user/file.name
Copy after login

截取前5个字符:

${variable:0:5}
Copy after login

SSH debug 模式:

ssh -vvv user@ip_address
Copy after login

SSH with pem key:

ssh user@ip_address -i key.pem
Copy after login

用wget抓取完整的网站目录结构,存放到本地目录中:

wget -r --no-parent --reject "index.html*" http://hostname/ -P /home/user/dirs
Copy after login

一次创建多个目录:

mkdir -p /home/user/{test,test1,test2}
Copy after login

列出包括子进程的进程树:

ps axwef
Copy after login

创建 war 文件:

jar -cvf name.war file
Copy after login

测试硬盘写入速度:

dd if=/dev/zero of=/tmp/output.img bs=8k count=256k; rm -rf /tmp/output.img
Copy after login

测试硬盘读取速度:

hdparm -Tt /dev/sda
Copy after login

获取文本的md5 hash:

echo -n "text" | md5sum
Copy after login

检查xml格式:

xmllint --noout file.xml
Copy after login

将tar.gz提取到新目录里:

tar zxvf package.tar.gz -C new_dir
Copy after login

使用curl获取HTTP头信息:

curl -I http://www.example.com
Copy after login

修改文件或目录的时间戳(YYMMDDhhmm):

touch -t 0712250000 file
Copy after login

用wget命令执行ftp下载:

wget -m ftp://username:password@hostname
Copy after login

生成随机密码(例子里是16个字符长):

LANG=c < /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c${1:-16};echo;
Copy after login

快速备份一个文件:

cp some_file_name{,.bkp}
Copy after login

访问Windows共享目录:

smbclient -U "DOMAIN\user" //dc.domain.com/share/test/dir
Copy after login

执行历史记录里的命令(这里是第100行):

!100
Copy after login

解压:

unzip package_name.zip -d dir_name
Copy after login

输入多行文字(CTRL + d 退出):

cat > test.txt
Copy after login

创建空文件或清空一个现有文件:

\> test.txt
Copy after login

与Ubuntu NTP server同步时间:

ntpdate ntp.ubuntu.com
Copy after login

用netstat显示所有tcp4监听端口:

netstat -lnt4 | awk &#39;{print $4}&#39; | cut -f2 -d: | grep -o &#39;[0-9]*&#39;
Copy after login

qcow2镜像文件转换:

qemu-img convert -f qcow2 -O raw precise-server-cloudimg-amd64-disk1.img \precise-server-cloudimg-amd64-disk1.raw
Copy after login

重复运行文件,显示其输出(缺省是2秒一次):

watch ps -ef
Copy after login


所有用户列表:

getent passwd
Copy after login

Mount root in read/write mode:

mount -o remount,rw /
Copy after login

挂载一个目录(这是不能使用链接的情况):

mount --bind /source /destination
Copy after login

动态更新DNS server:

nsupdate < <EOF update add $HOST 86400 A $IP send EOF
Copy after login

递归grep所有目录:

grep -r "some_text" /path/to/dir
Copy after login

列出前10个最大的文件:

lsof / | awk &#39;{ if($7 > 1048576) print $7/1048576 "MB "$9 }&#39; | sort -n -u | tail
Copy after login

显示剩余内存(MB):

free -m | grep cache | awk &#39;/[0-9]/{ print $4" MB" }&#39;
Copy after login

打开Vim并跳到文件末:

vim + some_file_name
Copy after login

Git 克隆指定分支(master):

git clone git@github.com:name/app.git -b master
Copy after login

Git 切换到其它分支(develop):

git checkout develop
Copy after login

Git 删除分支(myfeature):

git branch -d myfeature
Copy after login

Git 删除远程分支

git push origin :branchName
Copy after login

Git 将新分支推送到远程服务器:

git push -u origin mynewfeature
Copy after login

打印历史记录中最后一次cat命令:

!cat:p
Copy after login

运行历史记录里最后一次cat命令:

!cat
Copy after login

找出/home/user下所有空子目录:

find /home/user -maxdepth 1 -type d -empty
Copy after login

获取test.txt文件中第50-60行内容:

< test.txt sed -n &#39;50,60p&#39;
Copy after login

运行最后一个命令(如果最后一个命令是mkdir /root/test, 下面将会运行: sudo mkdir /root/test):

sudo !!
Copy after login

创建临时RAM文件系统 – ramdisk (先创建/tmpram目录):

mount -t tmpfs tmpfs /tmpram -o size=512m
Copy after login

Grep whole words:

grep -w "name" test.txt
Copy after login

在需要提升权限的情况下往一个文件里追加文本:

echo "some text" | sudo tee -a /path/file
Copy after login

列出所有kill signal参数:

kill -l
Copy after login

在bash历史记录里禁止记录最后一次会话:

kill -9 $$
Copy after login

扫描网络寻找开放的端口:

nmap -p 8081 172.20.0.0/16
Copy after login

设置git email:

git config --global user.email "me@example.com"
Copy after login

To sync with master if you have unpublished commits:

git pull --rebase origin master
Copy after login

将所有文件名中含有”txt”的文件移入/home/user目录:

find -iname "*txt*" -exec mv -v {} /home/user \;
Copy after login

将文件按行并列显示:

paste test.txt test1.txt
Copy after login

shell里的进度条:

pv data.log
Copy after login

使用netcat将数据发送到Graphite server:

echo "hosts.sampleHost 10 `date +%s`" | nc 192.168.200.2 3000
Copy after login

将tabs转换成空格:

expand test.txt > test1.txt
Copy after login

Skip bash history:

< space >cmd
Copy after login

去之前的工作目录:

cd -
Copy after login

拆分大体积的tar.gz文件(每个100MB),然后合并回去:

split –b 100m /path/to/large/archive /path/to/output/files cat files* > archive
Copy after login

使用curl获取HTTP status code:

curl -sL -w "%{http_code}\\n" www.example.com -o /dev/null
Copy after login

设置root密码,强化MySQL安全安装:

/usr/bin/mysql_secure_installation
Copy after login

当Ctrl + c不好使时:

Ctrl + \
Copy after login

获取文件owner:

stat -c %U file.txt
Copy after login

block设备列表:

lsblk -f
Copy after login

找出文件名结尾有空格的文件:

find . -type f -exec egrep -l " +$" {} \;
Copy after login

找出文件名有tab缩进符的文件

find . -type f -exec egrep -l $&#39;\t&#39; {} \;
Copy after login

用”=”打印出横线:全选复制放进笔记

printf &#39;%100s\n&#39; | tr &#39; &#39; =
Copy after login

The above is the detailed content of Common shell commands. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1258
29
C# Tutorial
1232
24
Where to view the logs of Tigervnc on Debian Where to view the logs of Tigervnc on Debian Apr 13, 2025 am 07:24 AM

In Debian systems, the log files of the Tigervnc server are usually stored in the .vnc folder in the user's home directory. If you run Tigervnc as a specific user, the log file name is usually similar to xf:1.log, where xf:1 represents the username. To view these logs, you can use the following command: cat~/.vnc/xf:1.log Or, you can open the log file using a text editor: nano~/.vnc/xf:1.log Please note that accessing and viewing log files may require root permissions, depending on the security settings of the system.

How debian readdir integrates with other tools How debian readdir integrates with other tools Apr 13, 2025 am 09:42 AM

The readdir function in the Debian system is a system call used to read directory contents and is often used in C programming. This article will explain how to integrate readdir with other tools to enhance its functionality. Method 1: Combining C language program and pipeline First, write a C program to call the readdir function and output the result: #include#include#include#includeintmain(intargc,char*argv[]){DIR*dir;structdirent*entry;if(argc!=2){

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

How to interpret the output results of Debian Sniffer How to interpret the output results of Debian Sniffer Apr 12, 2025 pm 11:00 PM

DebianSniffer is a network sniffer tool used to capture and analyze network packet timestamps: displays the time for packet capture, usually in seconds. Source IP address (SourceIP): The network address of the device that sent the packet. Destination IP address (DestinationIP): The network address of the device receiving the data packet. SourcePort: The port number used by the device sending the packet. Destinatio

How to monitor Nginx SSL performance on Debian How to monitor Nginx SSL performance on Debian Apr 12, 2025 pm 10:18 PM

This article describes how to effectively monitor the SSL performance of Nginx servers on Debian systems. We will use NginxExporter to export Nginx status data to Prometheus and then visually display it through Grafana. Step 1: Configuring Nginx First, we need to enable the stub_status module in the Nginx configuration file to obtain the status information of Nginx. Add the following snippet in your Nginx configuration file (usually located in /etc/nginx/nginx.conf or its include file): location/nginx_status{stub_status

How to recycle packages that are no longer used How to recycle packages that are no longer used Apr 13, 2025 am 08:51 AM

This article describes how to clean useless software packages and free up disk space in the Debian system. Step 1: Update the package list Make sure your package list is up to date: sudoaptupdate Step 2: View installed packages Use the following command to view all installed packages: dpkg--get-selections|grep-vdeinstall Step 3: Identify redundant packages Use the aptitude tool to find packages that are no longer needed. aptitude will provide suggestions to help you safely delete packages: sudoaptitudesearch '~pimportant' This command lists the tags

Key Linux Operations: A Beginner's Guide Key Linux Operations: A Beginner's Guide Apr 09, 2025 pm 04:09 PM

Linux beginners should master basic operations such as file management, user management and network configuration. 1) File management: Use mkdir, touch, ls, rm, mv, and CP commands. 2) User management: Use useradd, passwd, userdel, and usermod commands. 3) Network configuration: Use ifconfig, echo, and ufw commands. These operations are the basis of Linux system management, and mastering them can effectively manage the system.

How to install PHPStorm in Debian system How to install PHPStorm in Debian system Apr 13, 2025 am 06:03 AM

Install PHPStorm on the Debian system to easily solve your PHP development environment! The following steps will guide you through the entire installation process. Installation steps: Download PHPStorm: Visit the official website of JetBrains and download the latest version of PHPStorm. Unzip the installation package: After downloading using wget or curl, unzip it to the specified directory (for example /opt). Command example: wgethttps://download.jetbrains.com/phpstorm/phpstorm-2024.3.5.tar.gztar-xzfphpstorm-2024.3.5.tar.gz

See all articles