This article is participating in the topic “Through Linux 30 years” essay activity.

1 Mandatory override by CP copy in Linux

The problem

When cp is overwritten, no matter what parameter is added, such as -f or whether it is overwritten, when there are few files, you can also press Y to confirm, when there are many files, it is not easy to say.

-r stands for recursive copy, that is, copy the folder and all the files under it

-f indicates that a file with the same name is overwritten without prompting.

But why add -f, also appear overwrite prompt?

This is because the system uses the alias of the command to prevent us from accidentally overwriting the wrong file.

Use the alias command to take a look:

[root@VM-1-14-centos ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
Copy the code

From the above we can see that the cp command we enter is actually cp -i command. Several other commands, such as ll, ls, mv, rm, etc., also use aliases.

Solution 1

Run without the alias by prefacing cp with a ‘ ‘as follows:

Solution 2

Edit the file to comment out the aliases

# .bashrc# User specific aliases and functionsalias rm='rm -i'
#alias cp='cp -i'
alias mv='mv -i'# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fiCopy the code

2 The Linux history command displays the operation time

The editor/etc/bashrc“, add some content

export HISTSIZE=3000
export HISTTIMEFORMAT="%F %T "
export PROMPT_COMMAND="history -a; $PROMPT_COMMAND"
unset HISTCONTROL
Copy the code
[root@VM-1-14-centos ~]# source /etc/bashrc
Copy the code
 1507  2021-09-15 22:58:59 ls
 1508  2021-09-15 22:59:07 alias
 1509  2021-09-15 23:00:52 vim .bashrc
 1510  2021-09-15 23:02:22 vim /etc/bashrc
 1511  2021-09-15 23:02:36 history 
Copy the code

3 Linux command watch

introduce

The watch command executes a given command in a periodic manner, and the command output is displayed in full screen mode.

Watch is a very useful command. Almost all Linux distributions come with this little tool. As the name suggests, Watch can help you monitor the results of a command without having to run it manually.

grammar

Watch (Option) (Parameter)Copy the code

options

-n: Indicates the interval of command execution (seconds). -d: highlights the differences between command output information. -t: does not display the title.Copy the code

parameter

Instructions: Instructions that need to be executed periodically.

The sample

# # # watch uptime watch -t uptime watch - d - n netstat - NTLP # watch - 1 d 'ls -l | fgrep goface' / / monitoring goface file # watch -t -differences=cumulative uptime # watch -n 60 from // monitor mail # watch -n 1 "df -i; Df "// Monitor disk inodes and blocksCopy the code

Monitors the number of TIME_WAIT connections and refreshes every second

watch -n 1 'netstat -ant |grep TIME_WAIT |wc -l'
Copy the code

4 FG and bg enable processes to switch between the front and background

In Linux, fg and bg commands are front-background scheduling commands for processes. That is, a command process with a specified number (not a process number) is put to the foreground or background to run. For example, if a command is running for a long time, we want to put it in the background so that it doesn’t block the current operation. Some service command processes we want to keep running in the background for a long time.

The following commands or keys are used to perform operations before and after the process:

Ctrl+C

Terminates and exits the foreground command and returns to the SHELL

Ctrl+Z

Pause the foreground command, place the process in the background, and return to the SHELL

jobs

You can view the command process number that is being executed in the background

&

When running a command, add & to the end of the command to make it run in the background

fg N

Run the command process N in the foreground, same as %N

bg N

Run the command process whose number is N in the background

Fg, bg, JOBS, &, CTRL + Z are all related to system tasks, and although they are rarely needed today, they are useful to learn.