Basic skills of Linux command line that you absolutely do not know

Station B|Official account: a graduate student who knows everything

bashThe following are all explained by default in Linux . Believe me, each one is very good, and the fat intestines are useful.

1. !!

After entering and running a long command, I often find that I forgot to add it at the beginning. sudoIs this a problem? This is no problem

can entersudo !!

The command line will !!be replaced with the last command run:

insert image description here

2. Rollback

We all know that we can use cdto enter the directory

But few people know that you cd - can go back to the previous directory with:
insert image description here

3. Back to /Home

You may know ~that it is a shortcut to the main folder

But there's a little-known trick: if you type cdsomething that isn't immediately followed by something else, you'll still get into the home directory:
insert image description here

4. Search

In many cases, in order to find the commands entered several times before, you need to press the up arrow N times to find

But this can be achieved more easily using the reverse search function

Press Ctrl+Rand start typing a command, and the reverse search fill finds the closest match in the recent history (had to mention how handy zsh is):

insert image description here

5. Reuse a parameter

Another handy trick is !$to replace the arguments of the previous command with

cdFor example, when creating a folder and wanting to enter it is often encountered :

insert image description here

6. Copy and paste

Someone might notice that Ctrl+Cand Ctrl+Vdoesn't work in the Linux terminal

In most cases these are replaced with Ctrl+Shift+CandCtrl+Shit+V

This is because Ctrl+Cit is reserved for terminating the currently running program, don't think everyone knows this trick

7. Authenticating via SSH without a password

I often log in to an SSH server, and it will be annoying to enter the password every time

It can be skipped if the host and server exchange certificates

First, run the command ssh-keygento create a private/public key pair and save it to~/.ssh/id_rsa

Then use the command to copy the public key to the server:ssh-copy-id [email protected]_host

You will be prompted for the server password, and copy the public key

This way, no password is required to log into that server from that particular system

Note : This method is by no means less secure than regular authentication. It might even be more secure if the local system is secure. You won't be able to log into SSH unless your private key is revealed

8. Let the program run in the background

If you run a program in the terminal, it will normally be killed immediately after ending the terminal session

To prevent this and keep the program running, use nohupthe command -- for "don't hang up"

For example, to use scptransfer files between servers, while ensuring that the transfer continues even if the terminal window is accidentally closed, use the following command:

nohup scp very-big-file.mkv [email protected]:~/very-big-file.mkv

nohupAlso creates a nohup.outfile named to hold the output of the command

9. Answer Yes

If you're writing bash scripts to automate certain tasks, you might be yesannoyed by typing for every command you run

To skip this step and answer for any command yes, precede it withyes |

If you want to answer no, add in frontyes no |

10. Login as root

usesudo su

suThe command is logged in as root and sudowill be executed as root, no root password is required

Also, some distros disable the root password, so this is the only option:

insert image description here

11. Logout

The fastest way to log out from SSH, SFTP, root or from a terminal session is Ctrl+Dthe shortcut

It will come in handy when dealing with a large number of SSH connections or cannot enter exit, this is really commonly used, dear

12. Shred files

rmcommand is widely used to delete files, but does not completely delete files

Even after deletion, data can be extracted using special software

To completely delete a file and fill the space it uses with zeros, use shredthe command

use like thisshred -zvu <filename>

13. Password protect files in VIM

Files can be password protected vim +X filenamein Vim with the command or directly in :XVim with the command

14. List users

If you have privacy concerns, want to check who is logged into the system at any time

You can use wthe command to list all users currently in the system

I am born with everything, see you next time~

Guess you like

Origin blog.csdn.net/zzh516451964zzh/article/details/129495419