mirror of
https://github.com/LCTT/TranslateProject.git
synced 2024-12-26 21:30:55 +08:00
translated
This commit is contained in:
parent
a1d3bd813f
commit
e84846cf7b
@ -1,305 +0,0 @@
|
||||
Translating----geekpi
|
||||
|
||||
20 Unix Command Line Tricks – Part I
|
||||
================================================================================
|
||||
Let us start new year with **these Unix command line tricks** to increase productivity at the Terminal. I have found them over the years and I'm now going to share with you.
|
||||
|
||||
![](http://s0.cyberciti.org/uploads/cms/2015/01/unix-command-line-tricks.001.jpg)
|
||||
|
||||
### Deleting a HUGE file ###
|
||||
|
||||
I had a huge log file 200GB I need to delete on a production web server. My rm and ls command was crashed and I was afraid that the system to a crawl with huge disk I/O load. To remove a HUGE file, enter:
|
||||
|
||||
> /path/to/file.log
|
||||
# or use the following syntax
|
||||
: > /path/to/file.log
|
||||
|
||||
# finally delete it
|
||||
rm /path/to/file.log
|
||||
|
||||
### Want to cache console output? ###
|
||||
|
||||
Try the script command line utility to create a typescript of everything printed on your terminal.
|
||||
|
||||
script my.terminal.sessio
|
||||
|
||||
Type commands:
|
||||
|
||||
ls
|
||||
date
|
||||
sudo service foo stop
|
||||
|
||||
To exit (to end script session) type *exit* or *logout* or press *control-D*
|
||||
|
||||
exit
|
||||
|
||||
To view type:
|
||||
|
||||
more my.terminal.session
|
||||
less my.terminal.session
|
||||
cat my.terminal.session
|
||||
|
||||
### Restoring deleted /tmp folder ###
|
||||
|
||||
As my journey continues with [Linux and Unix shell, I made a few mistakes][1]. I accidentally deleted /tmp folder. To restore it all you have to do is:
|
||||
|
||||
mkdir /tmp
|
||||
chmod 1777 /tmp
|
||||
chown root:root /tmp
|
||||
ls -ld /tmp
|
||||
|
||||
### Locking a directory ###
|
||||
|
||||
For privacy of my data I wanted to lock down /downloads on my file server. So I ran:
|
||||
|
||||
chmod 0000 /downloads
|
||||
|
||||
The root user can still has access and ls and cd commands will not work. To go back:
|
||||
|
||||
chmod 0755 /downloads
|
||||
|
||||
### Password protecting file in vim text editor ###
|
||||
|
||||
Afraid that root user or someone may snoop into your personal text files? Try password protection to a file in vim, type:
|
||||
|
||||
vim +X filename
|
||||
|
||||
Or, before quitting in vim use :X vim command to encrypt your file and vim will prompt for a password.
|
||||
|
||||
### Clear gibberish all over the screen ###
|
||||
|
||||
Just type:
|
||||
|
||||
reset
|
||||
|
||||
### Becoming human ###
|
||||
|
||||
Pass the *-h* or *-H* (and other options) command line option to GNU or BSD utilities to get output of command commands like ls, df, du, in human-understandable formats:
|
||||
|
||||
ls -lh
|
||||
# print sizes in human readable format (e.g., 1K 234M 2G)
|
||||
df -h
|
||||
df -k
|
||||
# show output in bytes, KB, MB, or GB
|
||||
free -b
|
||||
free -k
|
||||
free -m
|
||||
free -g
|
||||
# print sizes in human readable format (e.g., 1K 234M 2G)
|
||||
du -h
|
||||
# get file system perms in human readable format
|
||||
stat -c %A /boot
|
||||
# compare human readable numbers
|
||||
sort -h -a file
|
||||
# display the CPU information in human readable format on a Linux
|
||||
lscpu
|
||||
lscpu -e
|
||||
lscpu -e=cpu,node
|
||||
# Show the size of each file but in a more human readable way
|
||||
tree -h
|
||||
tree -h /boot
|
||||
|
||||
### Show information about known users in the Linux based system ###
|
||||
|
||||
Just type:
|
||||
|
||||
## linux version ##
|
||||
lslogins
|
||||
|
||||
## BSD version ##
|
||||
logins
|
||||
|
||||
Sample outputs:
|
||||
|
||||
UID USER PWD-LOCK PWD-DENY LAST-LOGIN GECOS
|
||||
0 root 0 0 22:37:59 root
|
||||
1 bin 0 1 bin
|
||||
2 daemon 0 1 daemon
|
||||
3 adm 0 1 adm
|
||||
4 lp 0 1 lp
|
||||
5 sync 0 1 sync
|
||||
6 shutdown 0 1 2014-Dec17 shutdown
|
||||
7 halt 0 1 halt
|
||||
8 mail 0 1 mail
|
||||
10 uucp 0 1 uucp
|
||||
11 operator 0 1 operator
|
||||
12 games 0 1 games
|
||||
13 gopher 0 1 gopher
|
||||
14 ftp 0 1 FTP User
|
||||
27 mysql 0 1 MySQL Server
|
||||
38 ntp 0 1
|
||||
48 apache 0 1 Apache
|
||||
68 haldaemon 0 1 HAL daemon
|
||||
69 vcsa 0 1 virtual console memory owner
|
||||
72 tcpdump 0 1
|
||||
74 sshd 0 1 Privilege-separated SSH
|
||||
81 dbus 0 1 System message bus
|
||||
89 postfix 0 1
|
||||
99 nobody 0 1 Nobody
|
||||
173 abrt 0 1
|
||||
497 vnstat 0 1 vnStat user
|
||||
498 nginx 0 1 nginx user
|
||||
499 saslauth 0 1 "Saslauthd user"
|
||||
|
||||
### How do I fix mess created by accidentally untarred files in the current dir? ###
|
||||
|
||||
So I accidentally untar a tarball in /var/www/html/ directory instead of /home/projects/www/current. It created mess in /var/www/html/. The easiest way to fix this mess:
|
||||
|
||||
cd /var/www/html/
|
||||
/bin/rm -f "$(tar ztf /path/to/file.tar.gz)"
|
||||
|
||||
### Confused on a top command output? ###
|
||||
|
||||
Seriously, you need to try out htop instead of top:
|
||||
|
||||
sudo htop
|
||||
|
||||
### Want to run the same command again? ###
|
||||
|
||||
Just type !!. For example:
|
||||
|
||||
/myhome/dir/script/name arg1 arg2
|
||||
|
||||
# To run the same command again
|
||||
!!
|
||||
|
||||
## To run the last command again as root user
|
||||
sudo !!
|
||||
|
||||
The !! repeats the most recent command. To run the most recent command beginning with "foo":
|
||||
|
||||
!foo
|
||||
# Run the most recent command beginning with "service" as root
|
||||
sudo !service
|
||||
|
||||
The !$ use to run command with the last argument of the most recent command:
|
||||
|
||||
# Edit nginx.conf
|
||||
sudo vi /etc/nginx/nginx.conf
|
||||
|
||||
# Test nginx.conf for errors
|
||||
/sbin/nginx -t -c /etc/nginx/nginx.conf
|
||||
|
||||
# After testing a file with "/sbin/nginx -t -c /etc/nginx/nginx.conf", you
|
||||
# can edit file again with vi
|
||||
sudo vi !$
|
||||
|
||||
### ###Get a reminder you when you have to leave
|
||||
|
||||
If you need a reminder to leave your terminal, type the following command:
|
||||
|
||||
leave +hhmm
|
||||
|
||||
Where,
|
||||
|
||||
- **hhmm** - The time of day is in the form hhmm where hh is a time in hours (on a 12 or 24 hour clock), and mm are minutes. All times are converted to a 12 hour clock, and assumed to be in the next 12 hours.
|
||||
|
||||
### Home sweet home ###
|
||||
|
||||
Want to go the directory you were just in? Run:
|
||||
|
||||
cd -
|
||||
|
||||
Need to quickly return to your home directory? Enter:
|
||||
|
||||
cd
|
||||
|
||||
The variable *CDPATH* defines the search path for the directory containing directories:
|
||||
|
||||
export CDPATH=/var/www:/nas10
|
||||
|
||||
Now, instead of typing cd */var/www/html/* I can simply type the following to cd into /var/www/html path:
|
||||
|
||||
cd html
|
||||
|
||||
### Editing a file being viewed with less pager ###
|
||||
|
||||
To edit a file being viewed with less pager, press v. You will have the file for edit under $EDITOR:
|
||||
|
||||
less *.c
|
||||
less foo.html
|
||||
## Press v to edit file ##
|
||||
## Quit from editor and you would return to the less pager again ##
|
||||
|
||||
### List all files or directories on your system ###
|
||||
|
||||
To see all of the directories on your system, run:
|
||||
|
||||
find / -type d | less
|
||||
|
||||
# List all directories in your $HOME
|
||||
find $HOME -type d -ls | less
|
||||
|
||||
To see all of the files, run:
|
||||
|
||||
find / -type f | less
|
||||
|
||||
# List all files in your $HOME
|
||||
find $HOME -type f -ls | less
|
||||
|
||||
### Build directory trees in a single command ###
|
||||
|
||||
You can create directory trees one at a time using mkdir command by passing the -p option:
|
||||
|
||||
mkdir -p /jail/{dev,bin,sbin,etc,usr,lib,lib64}
|
||||
ls -l /jail/
|
||||
|
||||
### Copy file into multiple directories ###
|
||||
|
||||
Instead of running:
|
||||
|
||||
cp /path/to/file /usr/dir1
|
||||
cp /path/to/file /var/dir2
|
||||
cp /path/to/file /nas/dir3
|
||||
|
||||
Run the following command to copy file into multiple dirs:
|
||||
|
||||
echo /usr/dir1 /var/dir2 /nas/dir3 | xargs -n 1 cp -v /path/to/file
|
||||
|
||||
[Creating a shell function][2] is left as an exercise for the reader
|
||||
|
||||
### Quickly find differences between two directories ###
|
||||
|
||||
The diff command compare files line by line. It can also compare two directories:
|
||||
|
||||
ls -l /tmp/r
|
||||
ls -l /tmp/s
|
||||
# Compare two folders using diff ##
|
||||
diff /tmp/r/ /tmp/s/
|
||||
|
||||
[![Fig. : Finding differences between folders](http://s0.cyberciti.org/uploads/cms/2015/01/differences-between-folders.jpg)][3]
|
||||
|
||||
Fig. : Finding differences between folders
|
||||
|
||||
### Text formatting ###
|
||||
|
||||
You can reformat each paragraph with fmt command. In this example, I'm going to reformat file by wrapping overlong lines and filling short lines:
|
||||
|
||||
fmt file.txt
|
||||
|
||||
You can also split long lines, but do not refill i.e. wrap overlong lines, but do not fill short lines:
|
||||
|
||||
fmt -s file.txt
|
||||
|
||||
### See the output and write it to a file ###
|
||||
|
||||
Use the tee command as follows to see the output on screen and also write to a log file named my.log:
|
||||
|
||||
mycoolapp arg1 arg2 input.file | tee my.log
|
||||
|
||||
The tee command ensures that you will see mycoolapp output on on the screen and to a file same time.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.cyberciti.biz/open-source/command-line-hacks/20-unix-command-line-tricks-part-i/
|
||||
|
||||
作者:[nixCraft][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.cyberciti.biz/tips/about-us
|
||||
[1]:http://www.cyberciti.biz/tips/my-10-unix-command-line-mistakes.html
|
||||
[2]:http://bash.cyberciti.biz/guide/Writing_your_first_shell_function
|
||||
[3]:http://www.cyberciti.biz/open-source/command-line-hacks/20-unix-command-line-tricks-part-i/attachment/differences-between-folders/
|
305
translated/tech/20150115 20 Unix Command Line Tricks--Part I.md
Normal file
305
translated/tech/20150115 20 Unix Command Line Tricks--Part I.md
Normal file
@ -0,0 +1,305 @@
|
||||
20个Unix命令技巧 - 第一部分
|
||||
================================================================================
|
||||
让我们用**这些Unix命令技巧**开启新的一年,提高在终端下的生产力。我已经找了很久了,现在就与你们分享。
|
||||
|
||||
![](http://s0.cyberciti.org/uploads/cms/2015/01/unix-command-line-tricks.001.jpg)
|
||||
|
||||
### 删除一个大文件 ###
|
||||
|
||||
我在生产服务器上有一个很大的200GB的日志文件需要删除。我的rm和ls命令已经崩溃,我担心这是由于巨大的磁盘IO造成的,要删除这个大文件,输入:
|
||||
|
||||
> /path/to/file.log
|
||||
# or use the following syntax
|
||||
: > /path/to/file.log
|
||||
|
||||
# finally delete it
|
||||
rm /path/to/file.log
|
||||
|
||||
### 如何缓存终端输出? ###
|
||||
|
||||
尝试使用script命令行工具来为你的终端输出创建typescript。
|
||||
|
||||
|
||||
script my.terminal.sessio
|
||||
|
||||
输入命令:
|
||||
|
||||
ls
|
||||
date
|
||||
sudo service foo stop
|
||||
|
||||
要退出(结束script绘画),输入*exit* 或者 *logout* 或者按下 *control-D*
|
||||
|
||||
exit
|
||||
|
||||
要浏览输入:
|
||||
|
||||
more my.terminal.session
|
||||
less my.terminal.session
|
||||
cat my.terminal.session
|
||||
|
||||
### 还原删除的 /tmp 文件夹 ###
|
||||
|
||||
我在文章[Linux和Unix shell,我犯了一些错误][1]。我意外地删除了/tmp文件夹。要还原它,我需要这么做:
|
||||
|
||||
|
||||
mkdir /tmp
|
||||
chmod 1777 /tmp
|
||||
chown root:root /tmp
|
||||
ls -ld /tmp
|
||||
|
||||
### 锁定一个文件夹 ###
|
||||
|
||||
为了我的数据隐私,我想要锁定我文件服务器下的/downloads文件夹。因此我运行:
|
||||
|
||||
chmod 0000 /downloads
|
||||
|
||||
root用户仍旧可以访问,但是ls和cd命令还不可用。要还原它用:
|
||||
|
||||
chmod 0755 /downloads
|
||||
|
||||
### 在vim中用密码保护文件 ###
|
||||
|
||||
害怕root用户或者其他人偷窥你的个人文件么?尝试在vim中用密码保护,输入:
|
||||
|
||||
vim +X filename
|
||||
|
||||
或者,在退出vim之前使用:X 命令来加密你的文件,vim会提示你输入一个密码。
|
||||
|
||||
### 清除屏幕上的输出 ###
|
||||
|
||||
只要输入:
|
||||
|
||||
reset
|
||||
|
||||
### 成为人类 ###
|
||||
|
||||
传递*-h*或者*-H*(和其他选项)选项给GNU或者BSD工具来获取像ls、df、du等命令以人类可读的格式输出:
|
||||
|
||||
ls -lh
|
||||
# 以人类可读的格式 (比如: 1K 234M 2G)
|
||||
df -h
|
||||
df -k
|
||||
# 已字节输出如: KB, MB, or GB
|
||||
free -b
|
||||
free -k
|
||||
free -m
|
||||
free -g
|
||||
# 以人类可读的格式打印 (比如 1K 234M 2G)
|
||||
du -h
|
||||
# 以人类可读的格式获取系统perms
|
||||
stat -c %A /boot
|
||||
# 比较人类可读的数字
|
||||
sort -h -a file
|
||||
# 在Linux上以人类可读的形式显示cpu信息
|
||||
lscpu
|
||||
lscpu -e
|
||||
lscpu -e=cpu,node
|
||||
# 以人类可读的形式显示每个文件的大小
|
||||
tree -h
|
||||
tree -h /boot
|
||||
|
||||
### 在Linux系统中显示已知用户的信息 ###
|
||||
|
||||
只要输入:
|
||||
|
||||
## linux 版本 ##
|
||||
lslogins
|
||||
|
||||
## BSD 版本 ##
|
||||
logins
|
||||
|
||||
示例输出:
|
||||
|
||||
UID USER PWD-LOCK PWD-DENY LAST-LOGIN GECOS
|
||||
0 root 0 0 22:37:59 root
|
||||
1 bin 0 1 bin
|
||||
2 daemon 0 1 daemon
|
||||
3 adm 0 1 adm
|
||||
4 lp 0 1 lp
|
||||
5 sync 0 1 sync
|
||||
6 shutdown 0 1 2014-Dec17 shutdown
|
||||
7 halt 0 1 halt
|
||||
8 mail 0 1 mail
|
||||
10 uucp 0 1 uucp
|
||||
11 operator 0 1 operator
|
||||
12 games 0 1 games
|
||||
13 gopher 0 1 gopher
|
||||
14 ftp 0 1 FTP User
|
||||
27 mysql 0 1 MySQL Server
|
||||
38 ntp 0 1
|
||||
48 apache 0 1 Apache
|
||||
68 haldaemon 0 1 HAL daemon
|
||||
69 vcsa 0 1 virtual console memory owner
|
||||
72 tcpdump 0 1
|
||||
74 sshd 0 1 Privilege-separated SSH
|
||||
81 dbus 0 1 System message bus
|
||||
89 postfix 0 1
|
||||
99 nobody 0 1 Nobody
|
||||
173 abrt 0 1
|
||||
497 vnstat 0 1 vnStat user
|
||||
498 nginx 0 1 nginx user
|
||||
499 saslauth 0 1 "Saslauthd user"
|
||||
|
||||
### 我如何删除意外在当前文件夹下解压的文件? ###
|
||||
|
||||
我意外在/var/www/html/而不是/home/projects/www/current下解压了一个tarball。它混乱了/var/www/html下的文件。最简单修复这个问题的方法是:
|
||||
|
||||
cd /var/www/html/
|
||||
/bin/rm -f "$(tar ztf /path/to/file.tar.gz)"
|
||||
|
||||
### 对top命令的输出感到疑惑? ###
|
||||
|
||||
正经地说,你应该试一下用htop代替top:
|
||||
|
||||
sudo htop
|
||||
|
||||
### 想要再次运行相同的命令 ###
|
||||
|
||||
只需要输入!!。比如:
|
||||
|
||||
/myhome/dir/script/name arg1 arg2
|
||||
|
||||
# 要再次运行相同的命令
|
||||
!!
|
||||
|
||||
## 以root用户运行最后运行的命令
|
||||
sudo !!
|
||||
|
||||
!!会运行最近使用的命令。要运行最近运行的“foo”命令:
|
||||
|
||||
!foo
|
||||
# 以root用户运行上一次以“service”开头的命令
|
||||
sudo !service
|
||||
|
||||
!$用于运行带上最后一个参数的命令:
|
||||
|
||||
# 编辑 nginx.conf
|
||||
sudo vi /etc/nginx/nginx.conf
|
||||
|
||||
# 测试 nginx.conf
|
||||
/sbin/nginx -t -c /etc/nginx/nginx.conf
|
||||
|
||||
# 测试完 "/sbin/nginx -t -c /etc/nginx/nginx.conf"你可以用vi编辑了
|
||||
sudo vi !$
|
||||
|
||||
### 在你要离开的时候留下一个提醒 ###
|
||||
|
||||
If you need a reminder to leave your terminal, type the following command:
|
||||
如果你需要提醒离开你的终端,输入下面的命令:
|
||||
|
||||
leave +hhmm
|
||||
|
||||
这里:
|
||||
|
||||
- **hhmm** - 时间是以hhmm的形式,hh表示小时(12时制或者24小时制),mm代表分钟。所有的时间都转化成12时制,并且假定发生在接下来的12小时。
|
||||
|
||||
### 甜蜜的家 ###
|
||||
|
||||
想要进入刚才进入的地方?运行:
|
||||
|
||||
cd -
|
||||
|
||||
需要快速地回到家目录?输入:
|
||||
|
||||
cd
|
||||
|
||||
变量*CDPATH*定义了含有这个目录的搜索目录路径:
|
||||
|
||||
export CDPATH=/var/www:/nas10
|
||||
|
||||
现在,不用输入cd */var/www/html/ ,我可以直接输入下面的命令进入/var/www/html:
|
||||
|
||||
cd html
|
||||
|
||||
### 编辑一个用less浏览的文件 ###
|
||||
|
||||
要编辑一个用less浏览的文件,按下v。你就可以用变量$EDITOR下的编辑器来编辑了:
|
||||
|
||||
less *.c
|
||||
less foo.html
|
||||
## 下载v编辑文件 ##
|
||||
## 退出编辑器,你可以继续用less浏览了 ##
|
||||
|
||||
### 列出你系统中的所有文件和目录 ###
|
||||
|
||||
要看到你系统中的所有目录,运行:
|
||||
|
||||
find / -type d | less
|
||||
|
||||
# 列出$HOME 所有目录
|
||||
find $HOME -type d -ls | less
|
||||
|
||||
要看到所有的文件,运行:
|
||||
|
||||
find / -type f | less
|
||||
|
||||
# 列出 $HOME 中所有的文件
|
||||
find $HOME -type f -ls | less
|
||||
|
||||
### 用一条命令构造命令树 ###
|
||||
|
||||
你可以用mkdir加上-p选项一次创建目录树:
|
||||
|
||||
mkdir -p /jail/{dev,bin,sbin,etc,usr,lib,lib64}
|
||||
ls -l /jail/
|
||||
|
||||
### 复制文件到多个目录中 ###
|
||||
|
||||
不必运行:
|
||||
|
||||
cp /path/to/file /usr/dir1
|
||||
cp /path/to/file /var/dir2
|
||||
cp /path/to/file /nas/dir3
|
||||
|
||||
运行下面的命令来复制文件到多个目录中:
|
||||
|
||||
echo /usr/dir1 /var/dir2 /nas/dir3 | xargs -n 1 cp -v /path/to/file
|
||||
|
||||
留下[创建一个shell函数][2]作为读者的练习。
|
||||
|
||||
### 快速找出两个目录的不同 ###
|
||||
|
||||
diff命令会按行比较文件。它也可以比较两个目录:
|
||||
|
||||
ls -l /tmp/r
|
||||
ls -l /tmp/s
|
||||
# Compare two folders using diff ##
|
||||
diff /tmp/r/ /tmp/s/
|
||||
|
||||
[![Fig. : Finding differences between folders](http://s0.cyberciti.org/uploads/cms/2015/01/differences-between-folders.jpg)][3]
|
||||
|
||||
图片: 找出目录之间的不同
|
||||
|
||||
### 文本格式化 ###
|
||||
|
||||
你可以用fmt命令重新格式化每个段落。在本例中,我要用分割超长的行并且填充短行:
|
||||
|
||||
fmt file.txt
|
||||
|
||||
你也可以分割长的行,但是不重新填充,也就是说分割长行,但是不填充短行:
|
||||
|
||||
fmt -s file.txt
|
||||
|
||||
### 看见输出并写入到一个文件中 ###
|
||||
|
||||
如下使用tee命令在屏幕上看见输出并同样写入到日志文件my.log中:
|
||||
|
||||
mycoolapp arg1 arg2 input.file | tee my.log
|
||||
|
||||
tee可以保证你同时在屏幕上看到mycoolapp的输出和写入文件。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.cyberciti.biz/open-source/command-line-hacks/20-unix-command-line-tricks-part-i/
|
||||
|
||||
作者:[nixCraft][a]
|
||||
译者:[geekpi](https://github.com/geekpi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.cyberciti.biz/tips/about-us
|
||||
[1]:http://www.cyberciti.biz/tips/my-10-unix-command-line-mistakes.html
|
||||
[2]:http://bash.cyberciti.biz/guide/Writing_your_first_shell_function
|
||||
[3]:http://www.cyberciti.biz/open-source/command-line-hacks/20-unix-command-line-tricks-part-i/attachment/differences-between-folders/
|
Loading…
Reference in New Issue
Block a user