search

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"

让进程转入后台:

Ctrl + z

将进程转到前台:

fg

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

openssl rand -hex n

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

source /home/user/file.name

截取前5个字符:

${variable:0:5}

SSH debug 模式:

ssh -vvv user@ip_address

SSH with pem key:

ssh user@ip_address -i key.pem

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

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

一次创建多个目录:

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

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

ps axwef

创建 war 文件:

jar -cvf name.war file

测试硬盘写入速度:

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

测试硬盘读取速度:

hdparm -Tt /dev/sda

获取文本的md5 hash:

echo -n "text" | md5sum

检查xml格式:

xmllint --noout file.xml

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

tar zxvf package.tar.gz -C new_dir

使用curl获取HTTP头信息:

curl -I http://www.example.com

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

touch -t 0712250000 file

用wget命令执行ftp下载:

wget -m ftp://username:password@hostname

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

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

快速备份一个文件:

cp some_file_name{,.bkp}

访问Windows共享目录:

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

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

!100

解压:

unzip package_name.zip -d dir_name

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

cat > test.txt

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

\> test.txt

与Ubuntu NTP server同步时间:

ntpdate ntp.ubuntu.com

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

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

qcow2镜像文件转换:

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

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

watch ps -ef


所有用户列表:

getent passwd

Mount root in read/write mode:

mount -o remount,rw /

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

mount --bind /source /destination

动态更新DNS server:

nsupdate < <EOF update add $HOST 86400 A $IP send EOF

递归grep所有目录:

grep -r "some_text" /path/to/dir

列出前10个最大的文件:

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

显示剩余内存(MB):

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

打开Vim并跳到文件末:

vim + some_file_name

Git 克隆指定分支(master):

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

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

git checkout develop

Git 删除分支(myfeature):

git branch -d myfeature

Git 删除远程分支

git push origin :branchName

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

git push -u origin mynewfeature

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

!cat:p

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

!cat

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

find /home/user -maxdepth 1 -type d -empty

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

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

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

sudo !!

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

mount -t tmpfs tmpfs /tmpram -o size=512m

Grep whole words:

grep -w "name" test.txt

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

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

列出所有kill signal参数:

kill -l

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

kill -9 $$

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

nmap -p 8081 172.20.0.0/16

设置git email:

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

To sync with master if you have unpublished commits:

git pull --rebase origin master

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

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

将文件按行并列显示:

paste test.txt test1.txt

shell里的进度条:

pv data.log

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

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

将tabs转换成空格:

expand test.txt > test1.txt

Skip bash history:

< space >cmd

去之前的工作目录:

cd -

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

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

使用curl获取HTTP status code:

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

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

/usr/bin/mysql_secure_installation

当Ctrl + c不好使时:

Ctrl + \

获取文件owner:

stat -c %U file.txt

block设备列表:

lsblk -f

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

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

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

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

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

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

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

Statement
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
Linux Operations: Security and User ManagementLinux Operations: Security and User ManagementMay 06, 2025 am 12:04 AM

Linux user management and security can be achieved through the following steps: 1. Create users and groups, using commands such as sudouseradd-m-gdevelopers-s/bin/bashjohn. 2. Bulkly create users and set password policies, using the for loop and chpasswd commands. 3. Check and fix common errors, home directory and shell settings. 4. Implement best practices such as strong cryptographic policies, regular audits and the principle of minimum authority. 5. Optimize performance, use sudo and adjust PAM module configuration. Through these methods, users can be effectively managed and system security can be improved.

Linux Operations: File System, Processes, and MoreLinux Operations: File System, Processes, and MoreMay 05, 2025 am 12:16 AM

The core operations of Linux file system and process management include file system management and process control. 1) File system operations include creating, deleting, copying and moving files or directories, using commands such as mkdir, rmdir, cp and mv. 2) Process management involves starting, monitoring and killing processes, using commands such as ./my_script.sh&, top and kill.

Linux Operations: Shell Scripting and AutomationLinux Operations: Shell Scripting and AutomationMay 04, 2025 am 12:15 AM

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux Operations: Understanding the Core FunctionalityLinux Operations: Understanding the Core FunctionalityMay 03, 2025 am 12:09 AM

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

Linux: Entering and Exiting Maintenance ModeLinux: Entering and Exiting Maintenance ModeMay 02, 2025 am 12:01 AM

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

Understanding Linux: The Core Components DefinedUnderstanding Linux: The Core Components DefinedMay 01, 2025 am 12:19 AM

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.

The Building Blocks of Linux: Key Components ExplainedThe Building Blocks of Linux: Key Components ExplainedApr 30, 2025 am 12:26 AM

The core components of the Linux system include the kernel, file system, and user space. 1. The kernel manages hardware resources and provides basic services. 2. The file system is responsible for data storage and organization. 3. Run user programs and services in the user space.

Using Maintenance Mode: Troubleshooting and Repairing LinuxUsing Maintenance Mode: Troubleshooting and Repairing LinuxApr 29, 2025 am 12:28 AM

Maintenance mode is a special operating level entered in Linux systems through single-user mode or rescue mode, and is used for system maintenance and repair. 1. Enter maintenance mode and use the command "sudosystemctlisolaterscue.target". 2. In maintenance mode, you can check and repair the file system and use the command "fsck/dev/sda1". 3. Advanced usage includes resetting the root user password, mounting the file system in read and write mode and editing the password file.

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.