mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-03-27 02:30:10 +08:00
Merge remote-tracking branch 'LCTT/master'
This commit is contained in:
commit
16466a0c6f
@ -1,3 +1,4 @@
|
||||
[ translating by Dotcra ]
|
||||
How "Exit Traps" Can Make Your Bash Scripts Way More Robust And Reliable
|
||||
============================================================
|
||||
|
||||
|
@ -1,3 +1,6 @@
|
||||
Translating by MjSeven
|
||||
|
||||
|
||||
How To Quickly Monitor Multiple Hosts In Linux
|
||||
======
|
||||
|
||||
|
@ -1,311 +0,0 @@
|
||||
[ dotcra translating ]
|
||||
The fc Command Tutorial With Examples For Beginners
|
||||
======
|
||||
|
||||

|
||||
The **fc** command, short for **f** ix **c** ommands, is a shell built-in command used to list, edit and re-execute the most recently entered commands in to an interactive shell. You can edit the recently entered commands in your favorite editor and run them without having to retype the entire commands. This command can be helpful to correct the spelling mistakes in the previously entered commands and avoids the repetition of long and complicated commands. Since it is shell-builtin, it is available in most shells, including Bash, Zsh, Ksh etc. In this brief tutorial, we are going to learn to use fc command in Linux.
|
||||
|
||||
### The fc Command Tutorial With Examples
|
||||
|
||||
**List the recently executed commands**
|
||||
|
||||
If you run “fc -l” command with no arguments, it will display the last **16** commands.
|
||||
```
|
||||
$ fc -l
|
||||
507 fish
|
||||
508 fc -l
|
||||
509 sudo netctl restart wlp9s0sktab
|
||||
510 ls -l
|
||||
511 pwd
|
||||
512 uname -r
|
||||
513 uname -a
|
||||
514 touch ostechnix.txt
|
||||
515 vi ostechnix.txt
|
||||
516 echo "Welcome to OSTechNix"
|
||||
517 sudo apcman -Syu
|
||||
518 sudo pacman -Syu
|
||||
519 more ostechnix.txt
|
||||
520 wc -l ostechnix.txt
|
||||
521 cat ostechnix.txt
|
||||
522 clear
|
||||
|
||||
```
|
||||
|
||||
To reverse the order of the commands, use **-r** flag.
|
||||
```
|
||||
$ fc -l
|
||||
|
||||
```
|
||||
|
||||
You can suppress the line numbers using “-n” parameter.
|
||||
```
|
||||
$ fc -ln
|
||||
nano ~/.profile
|
||||
source ~/.profile
|
||||
source ~/.profile
|
||||
fc -ln
|
||||
fc -l
|
||||
sudo netctl restart wlp9s0sktab
|
||||
ls -l
|
||||
pwd
|
||||
uname -r
|
||||
uname -a
|
||||
echo "Welcome to OSTechNix"
|
||||
sudo apcman -Syu
|
||||
cat ostechnix.txt
|
||||
wc -l ostechnix.txt
|
||||
more ostechnix.txt
|
||||
clear
|
||||
|
||||
```
|
||||
|
||||
Now you won’t see the line numbers.
|
||||
|
||||
To list the result staring from a specific command, simply use the line number along with **-l** option. For instance, to display the commands starting from line number 520 up to the present, we do:
|
||||
```
|
||||
$ fc -l 520
|
||||
520 ls -l
|
||||
521 pwd
|
||||
522 uname -r
|
||||
523 uname -a
|
||||
524 echo "Welcome to OSTechNix"
|
||||
525 sudo apcman -Syu
|
||||
526 cat ostechnix.txt
|
||||
527 wc -l ostechnix.txt
|
||||
528 more ostechnix.txt
|
||||
529 clear
|
||||
530 fc -ln
|
||||
531 fc -l
|
||||
|
||||
```
|
||||
|
||||
To list a commands within a specific range, for example 520 to 525, do:
|
||||
```
|
||||
$ fc -l 520 525
|
||||
520 ls -l
|
||||
521 pwd
|
||||
522 uname -r
|
||||
523 uname -a
|
||||
524 echo "Welcome to OSTechNix"
|
||||
525 sudo apcman -Syu
|
||||
|
||||
```
|
||||
|
||||
Instead of using the line numbers, we can also use strings. For example, list the commands starting from “pwd” command up to the resent, just use the staring letter of that command (i.e **p** ) like below.
|
||||
```
|
||||
$ fc -l p
|
||||
521 pwd
|
||||
522 uname -r
|
||||
523 uname -a
|
||||
524 echo "Welcome to OSTechNix"
|
||||
525 sudo apcman -Syu
|
||||
526 cat ostechnix.txt
|
||||
527 wc -l ostechnix.txt
|
||||
528 more ostechnix.txt
|
||||
529 clear
|
||||
530 fc -ln
|
||||
531 fc -l
|
||||
532 fc -l 520
|
||||
533 fc -l 520 525
|
||||
534 fc -l 520
|
||||
535 fc -l 522
|
||||
536 fc -l l
|
||||
|
||||
```
|
||||
|
||||
To see everything between “pwd” to “more” command, you could use either:
|
||||
```
|
||||
$ fc -l p m
|
||||
|
||||
```
|
||||
|
||||
Or, use combination of first letter of the starting command command and line number of the ending command:
|
||||
```
|
||||
$ fc -l p 528
|
||||
|
||||
```
|
||||
|
||||
Or, just line numbers of starting and ending commands:
|
||||
```
|
||||
$ fc -l 521 528
|
||||
|
||||
```
|
||||
|
||||
All of these three commands will display the same result.
|
||||
|
||||
**Edit and re-run the last command automatically**
|
||||
|
||||
At times, you might misspelled a previous command. In such situations, you can easily edit the spelling mistakes of the command using your default editor and execute it without having to retype again.
|
||||
|
||||
To edit the last command and re-run it again, do:
|
||||
```
|
||||
$ fc
|
||||
|
||||
```
|
||||
|
||||
This will open your last command in the default editor.
|
||||
|
||||
![][2]
|
||||
|
||||
As you see in the above screenshot, my last command was “fc -l”. You can make any changes in the command and re-run it automatically again once you save and quit the editor. This can be useful when you use long and complicated commands or arguments. Please be mindful that this also can be a **destructive**. For example, if the previous command was a deadly command like “rm -fr <some-path>”, it will automatically execute and you may lost your important data. So, be very careful before using command.
|
||||
|
||||
**Change the default editor to edit commands**
|
||||
|
||||
Another notable option of fc is **“e”** to choose a different editor to edit the commands. For example, we can use “nano” editor to edit the last command like below.
|
||||
```
|
||||
$ fc -e nano
|
||||
|
||||
```
|
||||
|
||||
This command will open the nano editor(instead of the default editor) to edit last command.
|
||||
|
||||
![][3]
|
||||
|
||||
You may find it time consuming to use **-e** option for each command. To make the new editor as your default, just set the environment variable **FCEDIT** to the name of the editor you want **fc** to use.
|
||||
|
||||
For example, to set “nano” as the new default editor, edit your **~/.profile** or environment file:
|
||||
```
|
||||
$ vi ~/.profile
|
||||
|
||||
```
|
||||
|
||||
Add the following line:
|
||||
```
|
||||
FCEDIT=nano
|
||||
|
||||
```
|
||||
|
||||
You can also use the full path of the editor like below.
|
||||
```
|
||||
FCEDIT=/usr/local/bin/emacs
|
||||
|
||||
```
|
||||
|
||||
Type **:wq** to save and close the file. To update the changes, run:
|
||||
```
|
||||
$ source ~/.profile
|
||||
|
||||
```
|
||||
|
||||
Now, you can just type to “fc” to edit the last command using “nano” editor.
|
||||
|
||||
**Re-run the last command without editing it**
|
||||
|
||||
We already knew if we run “fc” without any arguments, it loads the editor with the most recent command. At times, you may not want to edit, but simply execute the last command. To do so, use hyphen (-) symbol at the end as shown below.
|
||||
```
|
||||
$ echo "Welcome to OSTechNix"
|
||||
Welcome to OSTechNix
|
||||
|
||||
$ fc -e -
|
||||
echo "Welcome to OSTechNix"
|
||||
Welcome to OSTechNix
|
||||
|
||||
```
|
||||
|
||||
As you see, fc didn’t edit the last command (i.e echo “Welcome to OSTechNix”) even if I used **-e** option.
|
||||
|
||||
Please note that some of the options are shell-specific. They may not work in other shells. For example the following options can be used in **zsh** shell. It won’t work in Bash or Ksh shells.
|
||||
|
||||
**Display when the commands were executed**
|
||||
|
||||
To view when the commands were run, use **-d** like below.
|
||||
```
|
||||
fc -ld
|
||||
1 18:41 exit
|
||||
2 18:41 clear
|
||||
3 18:42 fc -l
|
||||
4 18:42 sudo netctl restart wlp9s0sktab
|
||||
5 18:42 ls -l
|
||||
6 18:42 pwd
|
||||
7 18:42 uname -r
|
||||
8 18:43 uname -a
|
||||
9 18:43 cat ostechnix.txt
|
||||
10 18:43 echo "Welcome to OSTechNix"
|
||||
11 18:43 more ostechnix.txt
|
||||
12 18:43 wc -l ostechnix.txt
|
||||
13 18:43 cat ostechnix.txt
|
||||
14 18:43 clear
|
||||
15 18:43 fc -l
|
||||
|
||||
```
|
||||
|
||||
Now you see the execution time of most recently executed commands.
|
||||
|
||||
We can also display the full timestamp of each command using **-f** option.
|
||||
```
|
||||
fc -lf
|
||||
1 4/5/2018 18:41 exit
|
||||
2 4/5/2018 18:41 clear
|
||||
3 4/5/2018 18:42 fc -l
|
||||
4 4/5/2018 18:42 sudo netctl restart wlp9s0sktab
|
||||
5 4/5/2018 18:42 ls -l
|
||||
6 4/5/2018 18:42 pwd
|
||||
7 4/5/2018 18:42 uname -r
|
||||
8 4/5/2018 18:43 uname -a
|
||||
9 4/5/2018 18:43 cat ostechnix.txt
|
||||
10 4/5/2018 18:43 echo "Welcome to OSTechNix"
|
||||
11 4/5/2018 18:43 more ostechnix.txt
|
||||
12 4/5/2018 18:43 wc -l ostechnix.txt
|
||||
13 4/5/2018 18:43 cat ostechnix.txt
|
||||
14 4/5/2018 18:43 clear
|
||||
15 4/5/2018 18:43 fc -l
|
||||
16 4/5/2018 18:43 fc -ld
|
||||
|
||||
```
|
||||
|
||||
Of course, the European folks can use european date format using **-E** option.
|
||||
```
|
||||
fc -lE
|
||||
2 5.4.2018 18:41 clear
|
||||
3 5.4.2018 18:42 fc -l
|
||||
4 5.4.2018 18:42 sudo netctl restart wlp9s0sktab
|
||||
5 5.4.2018 18:42 ls -l
|
||||
6 5.4.2018 18:42 pwd
|
||||
7 5.4.2018 18:42 uname -r
|
||||
8 5.4.2018 18:43 uname -a
|
||||
9 5.4.2018 18:43 cat ostechnix.txt
|
||||
10 5.4.2018 18:43 echo "Welcome to OSTechNix"
|
||||
11 5.4.2018 18:43 more ostechnix.txt
|
||||
12 5.4.2018 18:43 wc -l ostechnix.txt
|
||||
13 5.4.2018 18:43 cat ostechnix.txt
|
||||
14 5.4.2018 18:43 clear
|
||||
15 5.4.2018 18:43 fc -l
|
||||
16 5.4.2018 18:43 fc -ld
|
||||
17 5.4.2018 18:49 fc -lf
|
||||
|
||||
```
|
||||
|
||||
### TL;DR
|
||||
|
||||
* When running without any arguments, fc will load the most recent command in the default text editor.
|
||||
* When running with a numeric argument, fc loads the editor with the command with that specified number.
|
||||
* When running with a string argument, fc loads the most recent command starting with that specified string.
|
||||
* When running with two arguments to fc , the arguments specify the beginning and end of a range of commands.
|
||||
|
||||
|
||||
|
||||
For more details, refer man pages.
|
||||
```
|
||||
$ man fc
|
||||
|
||||
```
|
||||
|
||||
And, that’s all for today. Hope you find this article useful. More good stuffs to come. Stay tuned!
|
||||
|
||||
Cheers!
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.ostechnix.com/the-fc-command-tutorial-with-examples-for-beginners/
|
||||
|
||||
作者:[SK][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.ostechnix.com/author/sk/
|
||||
[2]:http://www.ostechnix.com/wp-content/uploads/2018/04/fc-command-1.png
|
||||
[3]:http://www.ostechnix.com/wp-content/uploads/2018/04/fc-command-2.png
|
@ -1,140 +0,0 @@
|
||||
Translating by MjSeven
|
||||
|
||||
|
||||
Finding what you’re looking for on Linux
|
||||
======
|
||||
|
||||

|
||||
It isn’t hard to find what you’re looking for on a Linux system — a file or a command — but there are a _lot_ of ways to go looking.
|
||||
|
||||
### 7 commands to find Linux files
|
||||
|
||||
#### find
|
||||
|
||||
The most obvious is undoubtedly the **find** command, and find has become easier to use than it was years ago. It used to require a starting location for your search, but these days, you can also use find with just a file name or regular expression if you’re willing to confine your search to the local directory.
|
||||
```
|
||||
$ find e*
|
||||
empty
|
||||
examples.desktop
|
||||
|
||||
```
|
||||
|
||||
In this way, it works much like the **ls** command and isn't doing much of a search.
|
||||
|
||||
For more relevant searches, find requires a starting point and some criteria for your search (unless you simply want it to provide a recursive listing of that starting point’s directory. The command **find . -type f** will recursively list all regular files starting with the current directory while **find ~nemo -type f -empty** will find empty files in Nemo’s home directory.
|
||||
```
|
||||
$ find ~nemo -type f -empty
|
||||
/home/nemo/empty
|
||||
|
||||
```
|
||||
|
||||
**Also on Network world:[11 pointless but awesome Linux terminal tricks][1]**
|
||||
|
||||
#### locate
|
||||
|
||||
The name of the **locate** command suggests that it does basically the same thing as find, but it works entirely differently. Where the **find** command can select files based on a variety of criteria — name, size, owner, permissions, state (such as empty), etc. with a selectable depth for the search, the **locate** command looks through a file called /var/lib/mlocate/mlocate.db to find what you’re looking for. That db file is periodically updated, so a locate of a file you just created will probably fail to find it. If that bothers you, you can run the updatedb file and get the update to happen right away.
|
||||
```
|
||||
$ sudo updatedb
|
||||
|
||||
```
|
||||
|
||||
#### mlocate
|
||||
|
||||
The **mlocate** command works like the **locate** command and uses the same mlocate.db file as locate.
|
||||
|
||||
#### which
|
||||
|
||||
The **which** command works very differently than the **find** and **locate** commands. It uses your search path and checks each directory on it for an executable with the file name you’re looking for. Once it finds one, it stops searching and displays the full path to that executable.
|
||||
|
||||
The primary benefit of the **which** command is that it answers the question, “If I enter this command, what executable file will be run?” It ignores files that aren’t executable and doesn’t list all executables on the system with that name — just the one that it finds first. If you wanted to find _all_ executables that have some name, you could run a find command like this, but it might take considerably longer to run the very efficient **which** command.
|
||||
```
|
||||
$ find / -name locate -perm -a=x 2>/dev/null
|
||||
/usr/bin/locate
|
||||
/etc/alternatives/locate
|
||||
|
||||
```
|
||||
|
||||
In this find command, we’re looking for all executables (files that cen be run by anyone) named “locate”. We’re also electing not to view all of the “Permission denied” messages that would otherwise clutter our screens.
|
||||
|
||||
#### whereis
|
||||
|
||||
The **whereis** command works a lot like the **which** command, but it provides more information. Instead of just looking for executables, it also looks for man pages and source files. Like the **which** command, it uses your search path ($PATH) to drive its search.
|
||||
```
|
||||
$ whereis locate
|
||||
locate: /usr/bin/locate /usr/share/man/man1/locate.1.gz
|
||||
|
||||
```
|
||||
|
||||
#### whatis
|
||||
|
||||
The **whatis** command has its own unique mission. Instead of actually finding files, it looks for information in the man pages for the command you are asking about and provides the brief description of the command from the top of the man page.
|
||||
```
|
||||
$ whatis locate
|
||||
locate (1) - find files by name
|
||||
|
||||
```
|
||||
|
||||
If you ask about a script that you’ve just set up, it won’t have any idea what you’re referring to and will tell you so.
|
||||
```
|
||||
$ whatis cleanup
|
||||
cleanup: nothing appropriate.
|
||||
|
||||
```
|
||||
|
||||
#### apropos
|
||||
|
||||
The **apropos** command is useful when you know what you want to do, but you have no idea what command you should be using to do it. If you were wondering how to locate files, for example, the commands “apropos find” and “apropos locate” would have a lot of suggestions to offer.
|
||||
```
|
||||
$ apropos find
|
||||
File::IconTheme (3pm) - find icon directories
|
||||
File::MimeInfo::Applications (3pm) - Find programs to open a file by mimetype
|
||||
File::UserDirs (3pm) - find extra media and documents directories
|
||||
find (1) - search for files in a directory hierarchy
|
||||
findfs (8) - find a filesystem by label or UUID
|
||||
findmnt (8) - find a filesystem
|
||||
gst-typefind-1.0 (1) - print Media type of file
|
||||
ippfind (1) - find internet printing protocol printers
|
||||
locate (1) - find files by name
|
||||
mlocate (1) - find files by name
|
||||
pidof (8) - find the process ID of a running program.
|
||||
sane-find-scanner (1) - find SCSI and USB scanners and their device files
|
||||
systemd-delta (1) - Find overridden configuration files
|
||||
xdg-user-dir (1) - Find an XDG user dir
|
||||
$
|
||||
$ apropos locate
|
||||
blkid (8) - locate/print block device attributes
|
||||
deallocvt (1) - deallocate unused virtual consoles
|
||||
fallocate (1) - preallocate or deallocate space to a file
|
||||
IO::Tty (3pm) - Low-level allocate a pseudo-Tty, import constants.
|
||||
locate (1) - find files by name
|
||||
mlocate (1) - find files by name
|
||||
mlocate.db (5) - a mlocate database
|
||||
mshowfat (1) - shows FAT clusters allocated to file
|
||||
ntfsfallocate (8) - preallocate space to a file on an NTFS volume
|
||||
systemd-sysusers (8) - Allocate system users and groups
|
||||
systemd-sysusers.service (8) - Allocate system users and groups
|
||||
updatedb (8) - update a database for mlocate
|
||||
updatedb.mlocate (8) - update a database for mlocate
|
||||
whereis (1) - locate the binary, source, and manual page files for a...
|
||||
which (1) - locate a command
|
||||
|
||||
```
|
||||
|
||||
### Wrap-up
|
||||
|
||||
The commands available on Linux for locating and identifying files are quite varied, but they're all very useful.
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.networkworld.com/article/3268768/linux/finding-what-you-re-looking-for-on-linux.html
|
||||
|
||||
作者:[Sandra Henry-Stocker][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
||||
[1]:http://www.networkworld.com/article/2926630/linux/11-pointless-but-awesome-linux-terminal-tricks.html#tk.nww-fsb
|
@ -0,0 +1,311 @@
|
||||
使用 fc 修改历史命令
|
||||
======
|
||||
|
||||

|
||||
|
||||
fc (**F**ix **C**ommands 的缩写) 是个 shell 内置命令,用于在交互式 shell 里列出、编辑和执行最近输入的命令。你可以用你喜欢的编辑器编辑最近的命令并再次执行,而不用把它们整个重新输入一遍。除了可以避免重复输入又长又复杂的命令,它对修正拼写错误来说也很有用。因为是 shell 内置命令,大多 shell 都包含它,比如 Bash 、 Zsh 、 Ksh 等。在这篇短文中,我们来学一学在 Linux 中使用 fc 命令。
|
||||
|
||||
### fc 命令教程及示例
|
||||
|
||||
**列出最近执行的命令**
|
||||
|
||||
执行不带参数的"fc -l"命令,它会列出最近 **16** 个命令。
|
||||
```
|
||||
$ fc -l
|
||||
507 fish
|
||||
508 fc -l
|
||||
509 sudo netctl restart wlp9s0sktab
|
||||
510 ls -l
|
||||
511 pwd
|
||||
512 uname -r
|
||||
513 uname -a
|
||||
514 touch ostechnix.txt
|
||||
515 vi ostechnix.txt
|
||||
516 echo "Welcome to OSTechNix"
|
||||
517 sudo apcman -Syu
|
||||
518 sudo pacman -Syu
|
||||
519 more ostechnix.txt
|
||||
520 wc -l ostechnix.txt
|
||||
521 cat ostechnix.txt
|
||||
522 clear
|
||||
|
||||
```
|
||||
|
||||
**-r** 选项用于将输出反向排序。
|
||||
```
|
||||
$ fc -lr
|
||||
|
||||
```
|
||||
|
||||
**-n** 选项用于隐藏行号。
|
||||
```
|
||||
$ fc -ln
|
||||
nano ~/.profile
|
||||
source ~/.profile
|
||||
source ~/.profile
|
||||
fc -ln
|
||||
fc -l
|
||||
sudo netctl restart wlp9s0sktab
|
||||
ls -l
|
||||
pwd
|
||||
uname -r
|
||||
uname -a
|
||||
echo "Welcome to OSTechNix"
|
||||
sudo apcman -Syu
|
||||
cat ostechnix.txt
|
||||
wc -l ostechnix.txt
|
||||
more ostechnix.txt
|
||||
clear
|
||||
|
||||
```
|
||||
|
||||
这样行号就不再显示了。
|
||||
|
||||
如果想以某个命令开始,只需在 **-l** 选项后面加上行号即可。比如,要显示行号 520 至最近的命令,可以这样:
|
||||
```
|
||||
$ fc -l 520
|
||||
520 ls -l
|
||||
521 pwd
|
||||
522 uname -r
|
||||
523 uname -a
|
||||
524 echo "Welcome to OSTechNix"
|
||||
525 sudo apcman -Syu
|
||||
526 cat ostechnix.txt
|
||||
527 wc -l ostechnix.txt
|
||||
528 more ostechnix.txt
|
||||
529 clear
|
||||
530 fc -ln
|
||||
531 fc -l
|
||||
|
||||
```
|
||||
|
||||
要列出一段范围内的命令,将始末行号作为 "fc -l" 的参数即可,比如 520 至 525:
|
||||
```
|
||||
$ fc -l 520 525
|
||||
520 ls -l
|
||||
521 pwd
|
||||
522 uname -r
|
||||
523 uname -a
|
||||
524 echo "Welcome to OSTechNix"
|
||||
525 sudo apcman -Syu
|
||||
|
||||
```
|
||||
|
||||
除了使用行号,我们还可以使用字符。比如,要列出最近一个 "pwd" 至上一个命令之间的所以命令,只需要像下面这样使用起始字母即可:
|
||||
```
|
||||
$ fc -l p
|
||||
521 pwd
|
||||
522 uname -r
|
||||
523 uname -a
|
||||
524 echo "Welcome to OSTechNix"
|
||||
525 sudo apcman -Syu
|
||||
526 cat ostechnix.txt
|
||||
527 wc -l ostechnix.txt
|
||||
528 more ostechnix.txt
|
||||
529 clear
|
||||
530 fc -ln
|
||||
531 fc -l
|
||||
532 fc -l 520
|
||||
533 fc -l 520 525
|
||||
534 fc -l 520
|
||||
535 fc -l 522
|
||||
536 fc -l l
|
||||
|
||||
```
|
||||
|
||||
要列出所有 "pwd" 和 "more" 之间的命令,你可以都使用起始字母,像这样:
|
||||
```
|
||||
$ fc -l p m
|
||||
|
||||
```
|
||||
|
||||
或者,使用开始命令的首字母以及结束命令的行号:
|
||||
```
|
||||
$ fc -l p 528
|
||||
|
||||
```
|
||||
|
||||
或者都使用行号:
|
||||
```
|
||||
$ fc -l 521 528
|
||||
|
||||
```
|
||||
|
||||
这三个命令都显示一样的结果。
|
||||
|
||||
**编辑并执行上一个命令**
|
||||
|
||||
我们经常敲错命令,这时你可以用默认编辑器修正拼写错误并执行而不用将命令重新再敲一遍。
|
||||
|
||||
编辑并执行上一个命令:
|
||||
```
|
||||
$ fc
|
||||
|
||||
```
|
||||
|
||||
这会在默认编辑器里载入上一个命令。
|
||||
|
||||
|
||||
![][2]
|
||||
|
||||
你可以看到,我上一个命令是 "fc -l"。你可以随意修改,它会在你保存退出编辑器时自动执行。这在命令或参数又长又复杂时很有用。需要注意的是,它同时也可能是**毁灭性**的。比如,如果你的上一个命令是危险的 `rm -fr <some-path>`,当它自动执行时你可能丢掉你的重要数据。所以,小心谨慎对待每一个命令。
|
||||
|
||||
**更改默认编辑器**
|
||||
|
||||
另一个有用的选项是 **-e** ,它可以用来为 fc 命令选择不同的编辑器。比如,如果我们想用 "nano" 来编辑上一个命令:
|
||||
```
|
||||
$ fc -e nano
|
||||
|
||||
```
|
||||
|
||||
这个命令会打开 nano 编辑器(而不是默认编辑器)编辑上一个命令。
|
||||
|
||||
![][3]
|
||||
|
||||
如果你觉得用 **-e** 选项太麻烦,你可以修改你的默认编辑器,只需要将环境变量 **FCEDIT** 设为你想要让 **fc** 使用的编辑器名称即可。
|
||||
|
||||
比如,要把 "nano" 设为默认编辑器,编辑你的 **~/.profile** 或其他初始化文件: ( LCTT 译注:如果 ~/.profile 不存在可自己创建;如果使用的是 bash ,可以编辑 ~/.bash_profile )
|
||||
```
|
||||
$ vi ~/.profile
|
||||
|
||||
```
|
||||
|
||||
添加下面一行:
|
||||
```
|
||||
FCEDIT=nano
|
||||
# ( LCTT译注:如果在子 shell 中会用到 fc ,最好在这里 `export FCEDIT` )
|
||||
|
||||
```
|
||||
|
||||
你也可以使用编辑器的完整路径:
|
||||
```
|
||||
FCEDIT=/usr/local/bin/emacs
|
||||
|
||||
```
|
||||
|
||||
输入 **:wq** 保存退出。要使改动立即生效,运行以下命令:
|
||||
```
|
||||
$ source ~/.profile
|
||||
|
||||
```
|
||||
|
||||
现在再输入 "fc" 就可以使用 "nano" 编辑器来编辑上一个命令了。
|
||||
|
||||
**不编辑而直接执行上一个命令**
|
||||
|
||||
我们现在知道 "fc" 命令不带任何参数的话会将上一个命令载入编辑器。但有时你可能不想编辑,仅仅是想再次执行上一个命令。这很简单,在末尾加上连字符(-)就可以了:
|
||||
```
|
||||
$ echo "Welcome to OSTechNix"
|
||||
Welcome to OSTechNix
|
||||
|
||||
$ fc -e -
|
||||
echo "Welcome to OSTechNix"
|
||||
Welcome to OSTechNix
|
||||
|
||||
```
|
||||
|
||||
如你所见,"fc" 带了 **-e** 选项,但并没有编辑上一个命令(例中的 echo " Welcome to OSTechNix")。
|
||||
|
||||
需要注意的是,有些选项仅对指定 shell 有效。比如下面这些选项可以用在 **zsh** 中,但在 Bash 或 Ksh 中则不能用。
|
||||
|
||||
**显示命令的执行时间**
|
||||
|
||||
想要知道命令是在什么时候执行的,可以用 **-d** 选项:
|
||||
```
|
||||
fc -ld
|
||||
1 18:41 exit
|
||||
2 18:41 clear
|
||||
3 18:42 fc -l
|
||||
4 18:42 sudo netctl restart wlp9s0sktab
|
||||
5 18:42 ls -l
|
||||
6 18:42 pwd
|
||||
7 18:42 uname -r
|
||||
8 18:43 uname -a
|
||||
9 18:43 cat ostechnix.txt
|
||||
10 18:43 echo "Welcome to OSTechNix"
|
||||
11 18:43 more ostechnix.txt
|
||||
12 18:43 wc -l ostechnix.txt
|
||||
13 18:43 cat ostechnix.txt
|
||||
14 18:43 clear
|
||||
15 18:43 fc -l
|
||||
|
||||
```
|
||||
|
||||
这样你就可以查看最近命令的具体执行时间了。
|
||||
|
||||
使用选项 **-f** ,可以为每个命令显示完整的时间戳。
|
||||
```
|
||||
fc -lf
|
||||
1 4/5/2018 18:41 exit
|
||||
2 4/5/2018 18:41 clear
|
||||
3 4/5/2018 18:42 fc -l
|
||||
4 4/5/2018 18:42 sudo netctl restart wlp9s0sktab
|
||||
5 4/5/2018 18:42 ls -l
|
||||
6 4/5/2018 18:42 pwd
|
||||
7 4/5/2018 18:42 uname -r
|
||||
8 4/5/2018 18:43 uname -a
|
||||
9 4/5/2018 18:43 cat ostechnix.txt
|
||||
10 4/5/2018 18:43 echo "Welcome to OSTechNix"
|
||||
11 4/5/2018 18:43 more ostechnix.txt
|
||||
12 4/5/2018 18:43 wc -l ostechnix.txt
|
||||
13 4/5/2018 18:43 cat ostechnix.txt
|
||||
14 4/5/2018 18:43 clear
|
||||
15 4/5/2018 18:43 fc -l
|
||||
16 4/5/2018 18:43 fc -ld
|
||||
|
||||
```
|
||||
|
||||
当然,欧洲的老乡们还可以使用 **-E** 选项来显示欧洲时间格式。
|
||||
```
|
||||
fc -lE
|
||||
2 5.4.2018 18:41 clear
|
||||
3 5.4.2018 18:42 fc -l
|
||||
4 5.4.2018 18:42 sudo netctl restart wlp9s0sktab
|
||||
5 5.4.2018 18:42 ls -l
|
||||
6 5.4.2018 18:42 pwd
|
||||
7 5.4.2018 18:42 uname -r
|
||||
8 5.4.2018 18:43 uname -a
|
||||
9 5.4.2018 18:43 cat ostechnix.txt
|
||||
10 5.4.2018 18:43 echo "Welcome to OSTechNix"
|
||||
11 5.4.2018 18:43 more ostechnix.txt
|
||||
12 5.4.2018 18:43 wc -l ostechnix.txt
|
||||
13 5.4.2018 18:43 cat ostechnix.txt
|
||||
14 5.4.2018 18:43 clear
|
||||
15 5.4.2018 18:43 fc -l
|
||||
16 5.4.2018 18:43 fc -ld
|
||||
17 5.4.2018 18:49 fc -lf
|
||||
|
||||
```
|
||||
|
||||
### fc 用法总结
|
||||
|
||||
* 当不带任何参数时,fc 将上一个命令载入默认编辑器。
|
||||
* 当带一个数字作为参数时,fc 将数字指定的命令载入默认编辑器。
|
||||
* 当带一个字符作为参数时,fc 将最近一个以指定字符开头的命令载入默认编辑器。
|
||||
* 当有两个参数时,它们分别指定需要列出的命令范围的开始和结束。
|
||||
|
||||
|
||||
|
||||
更多细节,请参考 man 手册。
|
||||
```
|
||||
$ man fc
|
||||
|
||||
```
|
||||
|
||||
好了,今天就这些。希望这篇文章能帮助到你。更多精彩内容,敬请期待!
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.ostechnix.com/the-fc-command-tutorial-with-examples-for-beginners/
|
||||
|
||||
作者:[SK][a]
|
||||
译者:[Dotcra](https://github.com/Dotcra)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.ostechnix.com/author/sk/
|
||||
[2]:http://www.ostechnix.com/wp-content/uploads/2018/04/fc-command-1.png
|
||||
[3]:http://www.ostechnix.com/wp-content/uploads/2018/04/fc-command-2.png
|
@ -0,0 +1,137 @@
|
||||
在 Linux 上寻找你正在寻找的东西
|
||||
=====
|
||||
|
||||

|
||||
在 Linux 系统上找到你要找的东西并不难 - 一个文件或一个命令 - 但是有很多种方法可以寻找。
|
||||
|
||||
### 7 个命令来寻找 Linux 文件
|
||||
|
||||
#### find
|
||||
|
||||
最明显的无疑是 **find** 命令,并且 find 变得比以前更容易使用。它过去需要一个搜索的起始位置,但是现在,如果你想将搜索限制在本地目录中,你还可以使用仅包含文件名或正则表达式的 find 命令。
|
||||
```
|
||||
$ find e*
|
||||
empty
|
||||
examples.desktop
|
||||
|
||||
```
|
||||
|
||||
这样,它就像 **ls** 命令一样工作,并没有做太多的搜索。
|
||||
|
||||
对于更专业的搜索,find 命令需要一个起点和一些搜索条件(除非你只是希望它提供该起点目录的递归列表)。命令 **find -type f** 从当前目录开始将递归列出所有常规文件,而 **find ~nemo -type f -empty** 将在 nemo 的主目录中找到空文件。
|
||||
```
|
||||
$ find ~nemo -type f -empty
|
||||
/home/nemo/empty
|
||||
|
||||
```
|
||||
|
||||
**同样在网络世界:[11 个毫无意义但是很棒的 Linux 终端技巧][1]。**
|
||||
|
||||
#### locate
|
||||
|
||||
**locate** 命令的名称表明它与 find 命令基本相同,但它的工作原理完全不同。**find** 命令可以根据各种条件 - 名称,大小,所有者,权限,状态(如空)等等选择文件并作为搜索选择深度,**locate** 命令通过名为 /var/lib/mlocate/mlocate.db 的文件查找你要查找的内容。该数据文件会定期更新,因此你刚创建的文件的位置它可能无法找到。如果这让你感到困扰,你可以运行 updatedb 文件并立即获得更新。
|
||||
```
|
||||
$ sudo updatedb
|
||||
|
||||
```
|
||||
|
||||
#### mlocate
|
||||
|
||||
**mlocate** 命令的工作类似于 **locate** 命令,它使用与 locate 相同的 mlocate.db 文件。
|
||||
|
||||
#### which
|
||||
|
||||
**which** 命令的工作方式与 **find** 命令和 **locate** 命令有很大的区别。它使用你的搜索路径并检查其上的每个目录,以查找具有你要查找的文件名的可执行文件。一旦找到一个,它会停止搜索并显示该可执行文件的完整路径。
|
||||
|
||||
**which** 命令的主要优点是它回答了“如果我输入此命令,将运行什么可执行文件?”的问题。它会忽略不可执行文件,并且不会列出系统上带有该名称的所有可执行文件 - 列出的就是它找到的第一个。如果你想查找具有某个名称的所有可执行文件,则可以像这样运行 find 命令,但运行非常高效 **which** 命令可能需要相当长的时间。
|
||||
```
|
||||
$ find / -name locate -perm -a=x 2>/dev/null
|
||||
/usr/bin/locate
|
||||
/etc/alternatives/locate
|
||||
|
||||
```
|
||||
|
||||
在这个 find 命令中,我们在寻找名为 “locate” 的所有可执行文件(任何人都可以运行的文件)。我们也选择了不要查看所有“拒绝访问”的消息,否则这些消息会混乱我们的屏幕。
|
||||
|
||||
#### whereis
|
||||
|
||||
**whereis** 命令与 **which** 命令非常类似,但它提供了更多信息。它不仅仅是寻找可执行文件,它还寻找手册页(man page)和源文件。像 **which** 命令一样,它使用搜索路径($PATH) 来驱动搜索。
|
||||
```
|
||||
$ whereis locate
|
||||
locate: /usr/bin/locate /usr/share/man/man1/locate.1.gz
|
||||
|
||||
```
|
||||
|
||||
#### whatis
|
||||
|
||||
**whatis** 命令有其独特的使命。它不是实际查找文件,而是在手册页中查找有关所询问命令的信息,并从手册页的顶部提供该命令的简要说明。
|
||||
```
|
||||
$ whatis locate
|
||||
locate (1) - find files by name
|
||||
|
||||
```
|
||||
|
||||
如果你询问你刚刚设置的脚本,它不会知道你指的是什么,并会告诉你。
|
||||
```
|
||||
$ whatis cleanup
|
||||
cleanup: nothing appropriate.
|
||||
|
||||
```
|
||||
|
||||
#### apropos
|
||||
|
||||
当你知道你想要做什么,但不知道应该使用什么命令来执行此操作时,**apropos** 命令很有用。例如,如果你想知道如何查找文件,那么 “apropos find” 和 “apropos locate” 会提供很多建议。
|
||||
```
|
||||
$ apropos find
|
||||
File::IconTheme (3pm) - find icon directories
|
||||
File::MimeInfo::Applications (3pm) - Find programs to open a file by mimetype
|
||||
File::UserDirs (3pm) - find extra media and documents directories
|
||||
find (1) - search for files in a directory hierarchy
|
||||
findfs (8) - find a filesystem by label or UUID
|
||||
findmnt (8) - find a filesystem
|
||||
gst-typefind-1.0 (1) - print Media type of file
|
||||
ippfind (1) - find internet printing protocol printers
|
||||
locate (1) - find files by name
|
||||
mlocate (1) - find files by name
|
||||
pidof (8) - find the process ID of a running program.
|
||||
sane-find-scanner (1) - find SCSI and USB scanners and their device files
|
||||
systemd-delta (1) - Find overridden configuration files
|
||||
xdg-user-dir (1) - Find an XDG user dir
|
||||
$
|
||||
$ apropos locate
|
||||
blkid (8) - locate/print block device attributes
|
||||
deallocvt (1) - deallocate unused virtual consoles
|
||||
fallocate (1) - preallocate or deallocate space to a file
|
||||
IO::Tty (3pm) - Low-level allocate a pseudo-Tty, import constants.
|
||||
locate (1) - find files by name
|
||||
mlocate (1) - find files by name
|
||||
mlocate.db (5) - a mlocate database
|
||||
mshowfat (1) - shows FAT clusters allocated to file
|
||||
ntfsfallocate (8) - preallocate space to a file on an NTFS volume
|
||||
systemd-sysusers (8) - Allocate system users and groups
|
||||
systemd-sysusers.service (8) - Allocate system users and groups
|
||||
updatedb (8) - update a database for mlocate
|
||||
updatedb.mlocate (8) - update a database for mlocate
|
||||
whereis (1) - locate the binary, source, and manual page files for a...
|
||||
which (1) - locate a command
|
||||
|
||||
```
|
||||
|
||||
### 总结
|
||||
|
||||
Linux 上可用于查找和识别文件的命令有很多种,但它们都非常有用。
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://www.networkworld.com/article/3268768/linux/finding-what-you-re-looking-for-on-linux.html
|
||||
|
||||
作者:[Sandra Henry-Stocker][a]
|
||||
译者:[MjSeven](https://github.com/MjSeven)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
选题:[lujun9972](https://github.com/lujun9972)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://www.networkworld.com/author/Sandra-Henry_Stocker/
|
||||
[1]:http://www.networkworld.com/article/2926630/linux/11-pointless-but-awesome-linux-terminal-tricks.html#tk.nww-fsb
|
Loading…
Reference in New Issue
Block a user