Recommended favorites: easy to use Unix and Linux command tips
- 2020-10-31 22:06:27
- OfStack
1. Delete a large file
I have a very large 200GB log file on the production server that needs to be deleted. My rm and ls commands have crashed. I am afraid this is due to the huge disk IO. To delete this large file, type:
> /path/to/file.log
# Or use the following format
: > /path/to/file.log
# And then delete it
rm /path/to/file.log
2. How to record terminal output?
Try using the script command line tool to create output records for your terminal output.
script my.terminal.sessio
Input command:
ls
date
sudo service foo stop
To exit (end the script session), type exit or logout or press control-ES25en.
exit
To browse input:
more my.terminal.session
less my.terminal.session
cat my.terminal.session
3. Restore the deleted /tmp folder
I made 1 mistakes in articles Linux and Unix shell. I accidentally deleted the /tmp folder. To restore it, I need to do this:
mkdir /tmp
chmod 1777 /tmp
chown root:root /tmp
ls -ld /tmp
4. Lock a folder
For my data privacy, I want to lock the /downloads folder under my file server. So I ran:
chmod 0000 /downloads
root users are still accessible, and the ls and cd commands do not work. To restore it:
chmod 0755 /downloads
5. Use a password to protect files in vim
Are you afraid of root users or others looking into your personal files? Try password protection in vim and enter:
vim +X filename
Or, use the :X command to encrypt your files before exiting vim, and vim will prompt you for a password.
6. Clear the messy code on the screen
Just type:
reset
7. Easy to read format
Pass the -ES86en or -ES87en (and other options) option to the GNU or BSD tool to get commands like ls, df, du to output in a readable format:
ls -lh
# In an easy-to-read format ( Such as: 1K 234M 2G)
df -h
df -k
# In bytes, KB , MB or GB Output:
free -b
free -k
free -m
free -g
# Output in a readable format ( Such as 1K 234M 2G)
du -h
# Displays file system permissions in a readable format
stat -c %A /boot
# Numbers that are easy to read
sort -h -a file
# in Linux Is displayed in an easy-to-read format cpu information
lscpu
lscpu -e
lscpu -e=cpu,node
# Displays the size of each file in a readable form
tree -h
tree -h /boot
8. Display the known user information in Linux system
Just type:
## linux version ##
lslogins
## BSD version ##
logins
Sample output:
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
You can slide left and right (the same goes for similar styles later)
9. How can I delete files that I accidentally unzipped under the current folder?
I accidentally in/var/www/html/rather than/home/projects www/current decompressed the tarball 1. It messes up the files under /var/www/html and you don't even know which ones were unzipped by mistake. The easiest way to fix this problem is:
cd /var/www/html/
/bin/rm -f "$(tar ztf /path/to/file.tar.gz)"
You can slide left and right
Confused about the output of the top command?
Seriously, you should try htop instead of top:
sudo htop
11. Want to run the same command again
Just type!! . Such as:
/myhome/dir/script/name arg1 arg2
# Run the same command again
!!
## In order to root The user runs the last run command
sudo !!
!! Runs the most recently used command. To run the most recently run" foo "The opening command:
!foo
# In order to root User run 1 Time to" service "The first command
sudo !service
!$ Used to run tape last 1 The command with three parameters:
# The editor nginx.conf
sudo vi /etc/nginx/nginx.conf
# test nginx.conf
/sbin/nginx -t -c /etc/nginx/nginx.conf
# After the test "/sbin/nginx -t -c /etc/nginx/nginx.conf" You can use vi Edit this file again
sudo vi !$
You can slide left and right
Remind you at the terminal that you have to go
If you need to be reminded to leave your terminal, type the following command:
leave +hhmm
Here:
hhmm - Time is in the form hhmm, with hh for hours (12 hours or 24 hours) and mm for minutes. All time is converted to 12 hours and is assumed to occur in the following 12 hours.
Sweet home
Want to get in where you just got in? Run:
cd -
Need to get back to your home directory quickly? Input:
cd
The variable CDPATH defines the search path for the directory:
export CDPATH=/var/www:/nas10
Now, there is no input cd * / var/www/html/so long, I can directly input the following command to enter/var/www/html:
chmod 0000 /downloads
0
14. Edit files while browsing less
To edit a file that you are browsing with less, click v. You can edit it using the editor specified by the variable $EDITOR:
less *.c
less foo.html
## Press the v Key to edit the file ##
## After you exit the editor, you can continue using it less Browse the ##
15. List all files and directories on your system
To see all the directories on your system, run:
ls
date
sudo service foo stop
0
To see all the files, run:
ls
date
sudo service foo stop
1
16. Create a directory tree
You can use the mkdir plus -ES210en option to create 1 directory tree at a time:
ls
date
sudo service foo stop
2
Copy files to multiple directories
Do not have to run:
cp /path/to/file /usr/dir1
cp /path/to/file /var/dir2
cp /path/to/file /nas/dir3
Run the following command to copy the file to multiple directories:
ls
date
sudo service foo stop
4
You can slide left and right
Leave it as an exercise for the reader to create an shell function.
Quickly find out the difference between the two directories
The diff command compares files by line. But it can also compare two directories:
ls
date
sudo service foo stop
5
Pictures: Find the differences between directories
19. Text formatting
You can reformat each paragraph with the fmt command. In this case, I'm going to split the extra-long lines and fill the short lines:
fmt file.txt
You can also split long lines, but not refilled, which means split long lines, but not filled with short lines:
fmt -s file.txt
20. You can see the output and write it to a file
See the output on the screen using the tee command as follows and also write to the log file my.log:
ls
date
sudo service foo stop
6
tee ensures that you see the output of mycoolapp on the screen at the same time and write to the file my.log.
conclusion