Shell History #
Run the last Command as Root #
sudo !!
su -c "!!"
“!” is called event designator, it references a command in your shell history
sudo !w # run the most recent command starts with a given string
Repeat the Last Command That Started with a given String #
!<string>
Reuse the Second Word from the Previous Command #
host www.google.com 8.8.8.8
ping -c1 !^
Reuse the Last Word form the Previous Command #
unzip report.zip
rm !$
Reuse the Nth Word from a Previous Command #
!!:N
<event_designator>:<number>
The first word is 0(command), the second word is 1.
avconv -i screencast.mp4 podcast.mp3
mv !!:2 converted/ #move screencast.mp4 to converted/
mv !a:3 podcasts/ #mov podcast.mp3 to podcasts/
Repeat the Previous Command While Substituting a String #
grpe jason /etc/passwd
^pe^ep #grep jason /etc/passwd
grep rooty /etc/passwd
^y #"y" removed from the previous command
grep canon /etc/passwd; ls -ld /home/canon
^canon^cannon^:& #:& means replace every occurrence
Reference a Word of the Current Command and Reuse It #
!#:N
“!#” event designator represents the current command line.
mv Working-with-Files.pdf Chapter-18-!#:1
Find out Which Commands You Use Most Often #
history | awk '{print $2}' | sort | uniq -c | sort -rn | head
Clean Your Shell History #
history -c
Text Processing and Manipulation #
Strip out Comments and Blank Lines #
gerp -E -v '^#|^$' httpd.conf
Display Output in a Table #
alias ct='column -t'
mount -t ext4 | ct
Append Text to a File Using Sudo #
echo "Production Environment" | sudo tee -a /etc/motd
Change the Case of a String #
tr [:upper:][:lower:]
Display Your Command Search Path in a Human Readable Format #
echo $PATH | tr ':' '\n'
Display a Block of Text between Two Strings #
awk '/worker.c/,/^$/' httpd.conf
Sort the Body of Output While Leaving the Header on the First Line Intact #
Add a function in ~/.bash_profile
body() {
IFS=read -r header
printf '%s\n' "$header"
"$@"
}
command | body sort
cat file | body sort
对命令输出调用sort时,会把每一行也排进去。这个函数可以避免对第一行排序。
Remove a Character or set of Characters from a String of Line of Output #
cat cities.csv | cut -d, -f2 | tr -d '"' # remove "
Count the Number of Occurrences of a String #
uniq omits adjacent duplicate lines from file, it is typically preceded by the sort command.
Networking and SSH #
Serve Files in the Current Directory via a Web Interface #
python -m SimpleHTTPServer
python3 -m http.server
Get Your Public IP from the Command Line Using Curl #
curl ifconfig.me
Send a Email Attachment from the Command Line #
echo 'message' | mail -s 'subject' -a /path/to/attachment recipient@domain.com
System Administration #
Kill All Processes for a given User of Program #
sudo pkill -9 httpd
sudo killall -u bob sshd
Repeat a Command until It Succeeds #
while true
do
ping -c1 -W 1 remote-host > /dev/null 2>&1 && break
done; echo "remote-host is up at $(date)"
Find the Files That Are Using the Most Disk Space #
sudo find /var -xdev -type f -exec wc -c {} \; | sort -n
List Processes, Sorted by Memory Usage #
ps aux | sort -nk 4 | tail -5
List Processes, Sorted by CPU Usage #
ps aux | sort -nk 3 | tail -5
Generate a Random Password #
openssl rand -base64 48 | cut -c1-8
Files and Directories #
Quickly Make a Backup of a File #
cp file{,.bak}
Quickly Change a File’s Extension #
mv file{.old,.new}
Empty a File That Is Being Written To #
> file
Delete Empty Directories #
find . -type d -empty -delete
Watch Multiple Log Files a the Same Time #
multitail file1 fileN
multitail file1 -I fileN
“-I” option merges multiple files into one window.
An Easy-to-Read Recursive File Listing #
find . -type f -ls
Extract the Nth Line from a File #
awk 'NR==74' deploy.sh
Convert Text Files from Windows Format to Linux Format #
dos2unix file.txt
unix2dos file.txt
Miscellaneous #
Transform the Directory Structure of a Tar File When Extracting It #
The name and version of the project is the top directory in the tar file. To extract the files below that directory:
tar zxvf v3.1.1.tar.gz --strip-components=1 -C ~/bootstrap-latest
Repeat a Command at Regular Intervals and Watch Its Changing Output #
watch df -h /var
watch -n 1 "ps -ef | grep httpd | grep -v grep | wc -l"
Save the Output of a Command as an Image #
command | convert label:@- image.png
ImageMagick的policy.xml文件中要配置如下权限:
<policy domain="path" rights="read|write" pattern="@*"/>