Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

20 Unix/Linux command skills

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

20 Unix/Linux command skills

Let's use these Unix/Linux command techniques to start a new year and improve productivity under the terminal. I've been looking for it for a long time, and I'll share it with you now.

Delete a large file

I have a large 200GB log file on the production server that needs to be deleted. My rm and ls commands have crashed, and I'm 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

How to record terminal output?

Try using the script command line tool to create an output record for your terminal output.

Script my.terminal.sessio

Enter the command:

Ls

Date

Sudo service foo stop

To exit (end the script session), type exit or logout or press control-D.

Exit

To browse and enter:

More my.terminal.session

Less my.terminal.session

Cat my.terminal.session

Restore the deleted / tmp folder

I made some mistakes in the 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

Lock a folder

I want to lock the / downloads folder under my file server for the sake of my data privacy. So I ran:

Chmod 0000 / downloads

Root users can still access it, while the ls and cd commands do not work. To restore it with:

Chmod 0755 / downloads

Protect files with passwords in vim

Are you afraid of root users or others spying on your personal files? To try password protection in vim, enter:

Vim + X filename

Clear the garbled code on the screen

Just enter:

Reset

Readable format

Pass the-h or-H (and other options) option to the GNU or BSD tool to get commands such as ls, df, du, and so on, output in an easy-to-read format:

Ls-lh

# in an easy-to-read format (e.g. 1K 234m 2G)

Df-h

Df-k

# output in bytes, KB, MB or GB:

Free-b

Free-k

Free-m

Free-g

# output in an easy-to-read format (such as 1K 234m 2G)

Du-h

# display file system permissions in an easy-to-read format

Stat-c% A / boot

# numbers that are easier to read

Sort-h-a file

# display cpu information in an easy-to-read form on Linux

Lscpu

Lscpu-e

Lscpu-eBay CPU node

# display the size of each file in a readable form

Tree-h

Tree-h / boot

Display known user information in Linux system

Just enter:

# # 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 01 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 "Saslauthd user"

How do I delete files that were unzipped accidentally under the current folder?

I accidentally extracted a tarball under / var/www/html/ instead of / home/projects/www/current. It messes up the files under / var/www/html, and you don't even know which ones are misunderstood. The easiest way to fix this problem is:

Cd / var/www/html/

/ bin/rm-f "$(tar ztf / path/to/file.tar.gz)"

Confused about the output of the top command?

Seriously, you should try using htop instead of top:

Sudo htop

Want to run the same command again

Just input!!. For example:

/ myhome/dir/script/name arg1 arg2

# to run the same command again

!!

# # run the last command as a root user

Sudo!!

!! Runs the most recently used command. To run the recently run command that starts with "foo":

! foo

# run the last command starting with "service" as the root user

Sudo! service

! $to run the command with the last argument:

# Editing nginx.conf

Sudo vi / etc/nginx/nginx.conf

# Test nginx.conf

/ sbin/nginx-t-c / etc/nginx/nginx.conf

# after testing "/ sbin/nginx-t-c / etc/nginx/nginx.conf", you can edit this file again with vi

Sudo vi! $

Remind you on the terminal that you have to go.

If you need a reminder to leave your terminal, enter the following command:

Leave + hhmm

Here:

Hhmm-time is in the form of hhmm, hh for hours (12-hour or 24-hour) and mm for minutes. All the time is converted to a 12-hour system, and it is assumed to happen in the next 12 hours.

Home sweet home

Want to enter the place you just entered? Run:

Cd-

Need to get back to your home directory quickly? Enter:

Cd

The variable CDPATH defines the search path to the directory:

Export CDPATH=/var/www:/nas10

Now, instead of typing cd * / var/www/html/, I can enter / var/www/html directly by typing the following command:

Cd html

Edit the file while less browsing

To edit a file that you are browsing with less, press v. You can edit it with the editor specified by the variable $EDITOR:

Less * .c

Less foo.html

# # Press the v key to edit the file # #

# # after quitting the editor, you can continue browsing with less # #

List all the files and directories in your system

To see all the directories in your system, run:

Find /-type d | less

# list all directories of $HOME

Find $HOME-type d-ls | less

To see all the files, run:

Find /-type f | less

# list all files in $HOME

Find $HOME-type f-ls | less

Construct a directory tree with a command

You can use mkdir plus the-p option to create one directory tree at a time:

Copy files to multiple directories

You 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 files to multiple directories:

Echo / usr/dir1 / var/dir2 / nas/dir3 | xargs-n 1 cp-v / path/to/file

Leave it as an exercise for the reader to create a 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-l / tmp/r

Ls-l / tmp/s

# compare two folders using diff

Diff / tmp/r/ / tmp/s/

Picture: find out the differences between directories

Text formatting

You can reformat each paragraph with the fmt command. In this case, I want to split the extra-long line and fill the short line:

Fmt file.txt

You can also split long lines, but not refill them, that is, split long lines, but not fill short lines:

Fmt-s file.txt

You can see the output and write it to a file

Use the tee command to see the output on the screen and write to the log file my.log as follows:

Mycoolapp arg1 arg2 input.file | tee my.log

Tee ensures that you can see the output of mycoolapp on the screen and write to the file my.log at the same time.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report