10 tips for exclamation mark (!) in Linux
'!'
Symbols or operators in Linux can be used as logical negationoperators, or can be obtained by changing the command from the history, or by modifying the execution before execution The command. All the following commands have been explicitly verified in the bash shell. Most of these won't work in other shells, although I haven't verified it.
- Run commands in the history by command number.
“
You may not be aware of the fact that you can run commands from historical commands (commands that have been / were executed earlier). To get started, first find the command number by running the "history" command.
”
$ history
“
Now, in the output of history, only the command from history is run by the number in which it appears. Assume that you run the command numbered 1551 that appears in the 'history' command output.
”
$ !1551
“
And, it runs the command listed in number 1551. This method of retrieving executed commands is very useful, especially in the case of those very long commands. You just need to use **!** to call it.
”
- Run the previously executed command as the second to last command, the 7th to last command, etc.
“
You can run commands that you have run before, and they will be run in order such that the last command to be run will be represented as **-1, the second to last as -2, and the last to run command as -2
. Seven is-7**,….
”
“######
首先运行history命令以获取上次执行命令的列表。有必要运行history命令,这样您就可以确保没有类似的命令
rm command > file
和其他命令,只是为了确保您不会意外运行任何危险的命令。然后检查第六个最后一个命令,八个最后一个命令和第十个最后一个命令。”
$ history $ !-6 $ !-8 $ !-10
“
按数字运行上次执行的命令
”
- 将我们运行的最后一个命令的参数传递给新命令,无需重新输入
“
我需要列出目录 “**/home/$USER/Binary/firefox**” 的内容。
”
$ ls /home/$USER/Binary/firefox
“
然后我意识到我应该触发 “ls -l” 以查看哪个文件在那里可执行?所以我应该再次输入整个命令!不,我不需要。我只需要将这个新命令的最后一个参数作为:
”
$ ls -l !$
“
这里
!$
将携带在最后一个命令中传递给这个新命令的参数。”
“
将上次执行命令的参数传递给新命令
”
- 如何使用 (!) 处理两个或多个参数
“
假设我在桌面上创建了一个文本文件1.txt。
”
$ touch /home/avi/Desktop/1.txt
“
然后使用cp命令在任一侧使用完整路径将其复制到 “**/home/avi/Downloads**”。
”
$ cp /home/avi/Desktop/1.txt /home/avi/downloads
“
现在我们已经使用cp命令传递了两个参数。第一个是 “**/home/avi/Desktop/1.txt”,第二个是 “/home/avi/Downloads**”,让我们以不同的方式处理它们,只需执行
echo [arguments]
以不同方式打印两个参数。”
$ echo "1st Argument is : !^" $ echo "2nd Argument is : !cp:2"
“
注意第一个参数可以打印为
“!^”
,其余参数可以通过执行“![Name_of_Command]:[Number_of_argument]”
.”
“
在上面的例子中,第一个命令是 “cp”,第二个参数需要打印。因此
“!cp:2”
,如果任何命令说xyz使用 5 个参数运行并且您需要获得第 4 个参数,您可以使用“!xyz:4”
,并根据需要使用它。所有参数都可以通过 访问“!*”
。”
“
处理两个或更多参数
”
- 根据关键字执行最后一条命令我们可以根据关键字执行上次执行的命令。我们可以这样理解:
$ ls /home > /dev/null [Command 1] $ ls -l /home/avi/Desktop > /dev/null [Command 2] $ ls -la /home/avi/Downloads > /dev/null [Command 3] $ ls -lA /usr/bin > /dev/null [Command 4]
“
在这里,我们使用了相同的命令**(ls),但使用了不同的开关和不同的文件夹。此外,我们已将每个命令的输出发送到 “/dev/null**”,因为我们不会处理命令的输出,控制台也保持干净。
”
“
现在根据关键字执行上次运行命令。
”
$ ! ls [Command 1] $ ! ls -l [Command 2] $ ! ls -la [Command 3] $ ! ls -lA [Command 4]
“
检查输出,您会惊讶地发现您只是通过
ls
关键字运行已执行的命令。”
“
根据关键字运行命令
”
-
神奇的
!!
“
您可以使用
(!!)
.它将在当前命令中使用 alter/tweak 调用最后一个运行命令。给你看场景”
“
最后一天我运行了一个单行脚本来获取我的私有 IP,所以我运行,
”
$ ip addr show | grep inet | grep -v 'inet6'| grep -v '127.0.0.1' | awk '{print $2}' | cut -f1 -d/
“
然后我突然发现我需要将上面脚本的输出重定向到一个文件ip.txt,那我该怎么办?我应该再次重新输入整个命令并将输出重定向到文件吗?那么一个简单的解决方案是使用
UP
导航键并添加'> ip.txt'
将输出重定向到文件。”
$ ip addr show | grep inet | grep -v 'inet6'| grep -v '127.0.0.1' | awk '{print $2}' | cut -f1 -d/ > ip.txt
“
UP
键可以快速找到历史命令。现在考虑以下条件,下次我在单行脚本下运行时。”
$ ifconfig | grep "inet addr:" | awk '{print $2}' | grep -v '127.0.0.1' | cut -f2 -d:
“
一旦我运行脚本,bash 提示符就会返回一个错误消息
“bash: ifconfig: command not found”
,我不难猜测我以用户身份运行此命令,而它应该以 root 身份运行。”
“
那么有什么解决办法呢?很难登录到 root 然后再次键入整个命令!此外,上一个示例中的(向上导航键)在这里也没有出现。所以?我们需要
“!!”
不带引号调用,这将调用该用户的最后一个命令。”
$ su -c !! root
“
这里su是切换用户,也就是 root,
-c
以用户身份运行特定的命令,最重要的部分!!
将被命令替换,最后运行的命令将在这里替换。是的!您需要提供 root 密码。”
!!
主要在以下场景中使用,
“
当我以普通用户身份运行apt-get命令时,我通常会收到一个错误,说您无权执行。
”
$ apt-get upgrade && apt-get dist-upgrade
“
Opps 错误… 不要担心执行以下命令以使其成功..
”
$ su -c !! $ service apache2 start or $ /etc/init.d/apache2 start or $ systemctl start apache2
“
OOPS 用户无权执行此类任务,所以我运行..
”
$ su -c 'service apache2 start' or $ su -c '/etc/init.d/apache2 start' or $ su -c 'systemctl start apache2'
- 运行影响除 ![FILE_NAME] 之外的所有文件的命令
“
该
!
(逻辑 NOT)可以用来运行所有命令的文件 / 文件扩展名,除了后面'!'
。”
A.**从目录中删除所有文件,但名称为**2.txt的文件除外。
$ rm !(2.txt)
B.从文件夹中删除所有文件类型,除了扩展名为 “pdf”的文件类型。
$ $ rm !(*.pdf)
- 检查一个目录(比如 / home/avi/Tecmint)是否存在?printf 如果所述目录存在与否。
“
在这里,我们将使用
'! -d'
来验证目录是否存在,然后使用逻辑 AND 运算符(&&)
来打印该目录不存在,然后使用逻辑 OR 运算符(||)
来打印目录是否存在。”
“
逻辑是,当输出
[ ! -d /home/rumenz ]
为0 时,它将执行超出 Logical 的内容,否则它将转到 Logical OR(||)
并执行超出 LogicalOR 的内容。”
$ [ ! -d /home/rumenz ] && printf '\nno such /home/rumenz directory exist\n' || printf '\n/home/rumenz directory exist\n'
- 检查目录是否存在?如果没有退出命令。与上述条件类似,但这里如果所需目录不存在,它将退出命令。
$ [ ! -d /home/rumenz ] && exit
- 如果它不存在,则在您的主目录中创建一个目录(比如 rumenz)。脚本语言中的一种通用实现,如果所需目录不存在,它将创建一个。
[ ! -d /home/rumenz ] && mkdir /home/rumenz
The above is the detailed content of 10 tips for exclamation mark (!) in Linux. For more information, please follow other related articles on the PHP Chinese website!

Virtual Data Rooms (VDRs) offer secure document storage and sharing, ideal for sensitive business information. This article explores three open-source VDR solutions for on-premises deployment on Linux, eliminating the need for cloud-based services a

Upscayl: Your Free and Open-Source Solution for High-Resolution Images on Linux Linux users who frequently work with images know the frustration of low-resolution pictures. Luckily, Upscayl offers a powerful, free, and open-source solution. This des

The terminal emulator landscape is evolving rapidly, with developers leveraging modern hardware, GPU acceleration, containerization, and even AI/LLMs to enhance console experiences. Enter Ghostty, a new open-source, cross-platform terminal emulator

Innotop: Powerful MySQL monitoring command line tool Innotop is an excellent command line program, similar to the top command, used to monitor local and remote MySQL servers running under the InnoDB engine. It provides a comprehensive set of features and options to help database administrators (DBAs) track various aspects of MySQL performance, troubleshoot issues and optimize server configuration. Innotop allows you to monitor critical MySQL metrics, such as: MySQL replication status User statistics Query list InnoDB buffer pool InnoDB I/O Statistics Open table Locked table etc… The tool regularly refreshes its data to provide server status

Restic: Your Comprehensive Guide to Secure Linux Backups Data loss can cripple a Linux system. Accidental deletions, hardware failures, or system corruption necessitate a robust backup strategy. Restic is a leading solution, providing speed, securi

Top 10 Most Popular Linux Distributions in 2025 Entering 2025, we are excited to share with Linux enthusiasts the most popular distribution this year so far. DistroWatch has always been the most reliable source of information about open source operating systems, with particular attention to Linux distributions and BSD versions. It continuously collects and presents a lot of information about Linux distributions, making them easier to access. While it doesn't measure the popularity or usage of a distribution very well, DistroWatch remains the most accepted measure of popularity within the Linux community. It uses page click ranking (PHR) statistics to measure the popularity of Linux distributions among website visitors. [You can

Linux Window Managers: A Comprehensive Guide to the Best Tiling Options Linux window managers orchestrate how application windows behave, quietly managing the visual arrangement of your open programs. This article explores top-tier tiling window man

The sed command (stream editor) in Linux system is a powerful text processing tool that is widely used for text manipulation tasks, including searching, finding and replacing text, and even executing advanced scripting. This article will guide you through the basics of sed, explain how to use it for dynamic number replacement, and provide practical examples for beginners. What is sed? The sed command processes text line by line, allowing you to: Search for specific patterns. Replace text or number. Delete or insert rows. Convert text in various ways. It works in a non-interactive way, meaning it can process files or text streams without human intervention. Basic syntax of sed command sed [Options] 'Command' file illustrate: Options


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
