Station B|Official account: a graduate student who knows everything
bash
The 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. sudo
Is this a problem? This is no problem
can entersudo !!
The command line will !!
be replaced with the last command run:
2. Rollback
We all know that we can use cd
to enter the directory
But few people know that you cd -
can go back to the previous directory with:
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 cd
something that isn't immediately followed by something else, you'll still get into the home directory:
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+R
and start typing a command, and the reverse search fill finds the closest match in the recent history (had to mention how handy zsh is):
5. Reuse a parameter
Another handy trick is !$
to replace the arguments of the previous command with
cd
For example, when creating a folder and wanting to enter it is often encountered :
6. Copy and paste
Someone might notice that Ctrl+C
and Ctrl+V
doesn't work in the Linux terminal
In most cases these are replaced with Ctrl+Shift+C
andCtrl+Shit+V
This is because Ctrl+C
it 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-keygen
to 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 nohup
the command -- for "don't hang up"
For example, to use scp
transfer 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
nohup
Also creates a nohup.out
file named to hold the output of the command
9. Answer Yes
If you're writing bash scripts to automate certain tasks, you might be yes
annoyed 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
su
The command is logged in as root and sudo
will be executed as root, no root password is required
Also, some distros disable the root password, so this is the only option:
11. Logout
The fastest way to log out from SSH, SFTP, root or from a terminal session is Ctrl+D
the 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
rm
command 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 shred
the command
use like thisshred -zvu <filename>
13. Password protect files in VIM
Files can be password protected vim +X filename
in Vim with the command or directly in :X
Vim 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 w
the command to list all users currently in the system
I am born with everything, see you next time~