linux命令

# 停止机器
shutdown -h now
# 关闭机器
shutdown -p now
# 取消关机
shutdown -c
# 重启机器
reboot
# 修改系统变量
vim /etc/profile
# 环境变量立即起效
source /etc/profile
# 查看指定端口是否被占用
[root@centos ~]# netstat -nlpt|grep 9090
tcp6       0      0 :::9090                 :::*                    LISTEN      1868/httpd

# 查看具体的进程信息
[root@centos ~]# ps -ef|grep httpd
xquant    1868     1  0 08:27 ?        00:00:00 /home/xquant/Apache2/bin/httpd -f /home/xquant/Apache2/conf/httpd.conf -k start
xquant    1869  1868  0 08:27 ?        00:00:05 /home/xquant/Apache2/bin/httpd -f /home/xquant/Apache2/conf/httpd.conf -k start
root      2570  2117  0 08:39 pts/0    00:00:00 grep --color=auto httpd
# 动态查看进程信息  ctrl+c退出
[root@centos ~]# top
top - 08:40:17 up 18 min,  1 user,  load average: 0.00, 0.05, 0.11
Tasks: 113 total,   1 running, 112 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.3 us,  0.3 sy,  0.0 ni, 99.3 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem :  3880456 total,  3146492 free,   165636 used,   568328 buff/cache
KiB Swap:  2097148 total,  2097148 free,        0 used.  3495256 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
    9 root      20   0       0      0      0 S  0.3  0.0   0:02.63 rcu_sched
 2609 root      20   0  162000   2208   1544 R  0.3  0.1   0:00.02 top
    1 root      20   0  125336   3856   2584 S  0.0  0.1   0:01.48 systemd
    2 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kthreadd
    4 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kworker/0:0H
    6 root      20   0       0      0      0 S  0.0  0.0   0:01.30 ksoftirqd/0
    7 root      rt   0       0      0      0 S  0.0  0.0   0:00.00 migration/0
    8 root      20   0       0      0      0 S  0.0  0.0   0:00.00 rcu_bh
   10 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 lru-add-drain
   11 root      rt   0       0      0      0 S  0.0  0.0   0:00.02 watchdog/0
   13 root      20   0       0      0      0 S  0.0  0.0   0:00.00 kdevtmpfs
   14 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 netns
   15 root      20   0       0      0      0 S  0.0  0.0   0:00.00 khungtaskd
   16 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 writeback
   17 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kintegrityd
   18 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   19 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   20 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 bioset
   21 root       0 -20       0      0      0 S  0.0  0.0   0:00.00 kblockd
# 查看linux内存剩余容量
[root@centos ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           3789         161        3073           8         554        3414
Swap:          2047           0        2047

# 查看磁盘大小 默认以k为单位
[root@centos app]# df
Filesystem                     1K-blocks     Used Available Use% Mounted on
devtmpfs                         1928500        0   1928500   0% /dev
tmpfs                            1940228        0   1940228   0% /dev/shm
tmpfs                            1940228     8748   1931480   1% /run
tmpfs                            1940228        0   1940228   0% /sys/fs/cgroup
/dev/mapper/centos_centos-root  52403200 30950132  21453068  60% /
/dev/sda1                        1038336   183012    855324  18% /boot
/dev/mapper/centos_centos-home  78596612  2026944  76569668   3% /home
tmpfs                             388048        0    388048   0% /run/user/0
# 显示本地系统的占用信息, 以 K, M, G 为单位
[root@centos app]# df -lh
Filesystem                      Size  Used Avail Use% Mounted on
devtmpfs                        1.9G     0  1.9G   0% /dev
tmpfs                           1.9G     0  1.9G   0% /dev/shm
tmpfs                           1.9G  8.6M  1.9G   1% /run
tmpfs                           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/mapper/centos_centos-root   50G   30G   21G  60% /
/dev/sda1                      1014M  179M  836M  18% /boot
/dev/mapper/centos_centos-home   75G  2.0G   74G   3% /home
tmpfs                           379M     0  379M   0% /run/user/0


# 查看当前登录账户
[root@centos ~]# whoami
root
# 查看多少用户使用系统
[root@centos ~]# who
root     pts/0        2020-03-16 08:25 (192.168.7.201)
# 查看系统日期 date --help
[root@centos ~]# date
Mon Mar 16 08:43:16 CST 2020
# 查看日历
[root@centos ~]# cal
     March 2020
Su Mo Tu We Th Fr Sa
 1  2  3  4  5  6  7
 8  9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31

# 查看系统服务启动
[root@centos ~]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

netconsole      0:off   1:off   2:off   3:off   4:off   5:off   6:off
network         0:off   1:off   2:on    3:on    4:on    5:on    6:off

# 显示当前目录下所有文件
[root@centos app]# du -sh *
230M    elasticsearch-6.8.2
142M    elasticsearch-6.8.2.tar.gz
8.0K    nohup.out
97M     zipkin-dependencies-2.4.0.jar
54M     zipkin-server-2.19.0-exec.jar
# 显示文件大小
[root@centos app]# du -sh zipkin-dependencies-2.4.0.jar
97M     zipkin-dependencies-2.4.0.jar
# 显示当前目录所占空间大小
[root@centos app]# du -sh
522M

# 查看详细指令格式
[root@centos app]# du --help
Usage: du [OPTION]... [FILE]...
  or:  du [OPTION]... --files0-from=F
Summarize disk usage of each FILE, recursively for directories.

Mandatory arguments to long options are mandatory for short options too.
  -0, --null            end each output line with 0 byte rather than newline
  -a, --all             write counts for all files, not just directories
      --apparent-size   print apparent sizes, rather than disk usage; although
                          the apparent size is usually smaller, it may be
                          larger due to holes in ('sparse') files, internal
                          fragmentation, indirect blocks, and the like
  -B, --block-size=SIZE  scale sizes by SIZE before printing them; e.g.,
                           '-BM' prints sizes in units of 1,048,576 bytes;
                           see SIZE format below
  -b, --bytes           equivalent to '--apparent-size --block-size=1'
  -c, --total           produce a grand total
  -D, --dereference-args  dereference only symlinks that are listed on the
                          command line
  -d, --max-depth=N     print the total for a directory (or file, with --all)
                          only if it is N or fewer levels below the command
                          line argument;  --max-depth=0 is the same as
                          --summarize
      --files0-from=F   summarize disk usage of the
                          NUL-terminated file names specified in file F;
                          if F is -, then read names from standard input
  -H                    equivalent to --dereference-args (-D)
  -h, --human-readable  print sizes in human readable format (e.g., 1K 234M 2G)
      --inodes          list inode usage information instead of block usage
  -k                    like --block-size=1K
  -L, --dereference     dereference all symbolic links
  -l, --count-links     count sizes many times if hard linked
  -m                    like --block-size=1M
  -P, --no-dereference  don't follow any symbolic links (this is the default)
  -S, --separate-dirs   for directories do not include size of subdirectories
      --si              like -h, but use powers of 1000 not 1024
  -s, --summarize       display only a total for each argument
  -t, --threshold=SIZE  exclude entries smaller than SIZE if positive,
                          or entries greater than SIZE if negative
      --time            show time of the last modification of any file in the
                          directory, or any of its subdirectories
      --time=WORD       show time as WORD instead of modification time:
                          atime, access, use, ctime or status
      --time-style=STYLE  show times using STYLE, which can be:
                            full-iso, long-iso, iso, or +FORMAT;
                            FORMAT is interpreted like in 'date'
  -X, --exclude-from=FILE  exclude files that match any pattern in FILE
      --exclude=PATTERN    exclude files that match PATTERN
  -x, --one-file-system    skip directories on different file systems
      --help     display this help and exit
      --version  output version information and exit

Display values are in units of the first available SIZE from --block-size,
and the DU_BLOCK_SIZE, BLOCK_SIZE and BLOCKSIZE environment variables.
Otherwise, units default to 1024 bytes (or 512 if POSIXLY_CORRECT is set).

SIZE is an integer and optional unit (example: 10M is 10*1024*1024).  Units
are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'du invocation'
 Linux查看CPU和内存命令:ps
1.CPU占用最多的前10个进程:

ps auxw|sort -rn -k3|head -10

2.内存消耗最多的前10个进程

ps auxw|sort -rn -k4|head -10

3.虚拟内存使用最多的前10个进程

ps auxw|sort -rn -k5|head -10


[root@60 images]# docker images |grep -w 'redis' |gawk '{ print "docker save -o "$1"-"$2".tar "$1":"$2"" } '
docker save -o redis-latest.tar redis:latest
[root@60 images]# docker images |grep -w 'redis' |gawk '{ print "docker save -o "$1"-"$2".tar "$1":"$2"" }'| sh
[root@60 images]# ll
总用量 3597744
-rw------- 1 root root  107601408 911 10:53 redis-latest.tar


find . -name "*.txt" | awk -v mvCmd='mv "%s" "%s"\n' \
    '{ old=$0;
       sub(/[.]txt$/,".log"); -- $0中的.txt更换为.log,由于.是特殊符号,所以用方括号包围
       printf mvCmd,old,$0;
     }' | sh
 
ls 
file1.log file2.log file3.log file4.log file5.log

find . -iname "*backup*" -exec rename backup ignore '{}' \;   -- 将文件名中的backup改为ignore


$ find . -name "*.log" | awk -v mvCmd='mv "%s" "%s"\n' \
    '{ new=gensub(/([0-9]{2})-([0-9]{2})-([0-9]{4})/, "\\3-\\2-\\1", "g");
       printf mvCmd,$0,new;
     }' | sh
 
$ ls -1
1976-11-18_system.log
1992-08-08_system.log
2019-11-29_system.log


$ ls -1
afile-1.txt
bfile-10.txt
cfile-123.txt
dfile-4711.txt
efile-20191201.txt


$ find . -name "*.txt" | awk -F'[-.]' -v mvCmd='mv -n "%s" "%s"\n' \
    '{ num = $(NF-1);
       files[$0] = num;
       max = num > max? num : max;
     }
     END { width = length(max);
           for(name in files) {
               old = new = name;
               formattedNum = sprintf("%0*d", width, files[name]);
               sub(files[name],formattedNum,new);
               printf mvCmd,old,new;
           }
     }' | sh
 
$ ls -1
afile-00000001.txt
bfile-00000010.txt
cfile-00000123.txt
dfile-00004711.txt
efile-20191201.txt

-- 删除文件中包含特定字段的行
grep -v "Baeldung" myfile.txt > tmpfile && mv tmpfile myfile.txt
awk '!/Baeldung/' myfile.txt > tmpfile && mv tmpfile myfile.txt
sed -i '/Baeldung/d' myfile.txt  -- 通过d删除匹配行

-- 查看CPU信息
[root@localhost dev]# cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 140
model name      : 11th Gen Intel(R) Core(TM) i5-11300H @ 3.10GHz
stepping        : 1
microcode       : 0xffffffff
cpu MHz         : 2611.208
cache size      : 8192 KB
physical id     : 0
siblings        : 1
core id         : 0
cpu cores       : 1
apicid          : 0
initial apicid  : 0
fpu             : yes
fpu_exception   : yes
cpuid level     : 22
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc eagerfpu pni ssse3 cx16 pcid sse4_1 sse4_2 hypervisor lahf_lm ibrs_enhanced fsgsbase invpcid flush_l1d arch_capabilities
bogomips        : 5222.41
clflush size    : 64
cache_alignment : 64
address sizes   : 39 bits physical, 48 bits virtual
power management:
-- 查看磁盘信息
[root@localhost dev]# cat /proc/diskstats
   8       0 sda 7868 10 607684 13482 5674 4714 381787 31170 0 10558 44652
   8       1 sda1 2235 4 52964 1486 10 0 4137 22 0 1321 1508
   8       2 sda2 5602 6 551376 11957 4981 4714 377650 29882 0 8858 41839
  11       0 sr0 0 0 0 0 0 0 0 0 0 0 0
 253       0 dm-0 5131 0 529277 11613 7330 0 361580 285361 0 8378 296974
 253       1 dm-1 90 0 4920 67 0 0 0 0 0 43 67
 253       2 dm-2 305 0 12939 216 2411 0 16070 2295 0 941 2511
 -- 查看内存信息
[root@localhost dev]# cat /proc/meminfo
MemTotal:        3880620 kB
MemFree:         3208404 kB
MemAvailable:    3448524 kB
... 


find myDir -name '*.txt' -exec sed -i 's/Linux/& operating system/g' {} +
head $(find myDir -name "*.txt")
find myDir -name '*.txt' | xargs sed -i 's/Linux/& operating system/g'
grep -Rl 'Linux' myDir
grep -Rl 'Linux' myDir | xargs sed -i 's/Linux/& operating system/g'

-- 查看进程以及包含的线程
ps -p 13101 -L -f

-- 在ps命令当中 -p指定进程号 -o指定输出格式
[root@60 ~]# ps -p 13101 -o pid,sid,cmd   
  PID   SID CMD
13101 24953 /usr/local/jdk/bin/java -Djava.ext.dirs=/usr/local/jdk/jre/lib/ext:/usr/local/jdk/lib/ext -DembeddedStorage=tr
-- 通过sid查看子进程
[root@60 ~]# ps -g 24953 -H
  PID TTY          TIME CMD
13237 ?        10-06:52:30 java
13149 ?        10-20:12:34 java
13101 ?        9-22:09:44 java
[root@60 ~]# ps -p 13101 -o lwp,comm,time,pid  (LWP 为线程 Light weight process)
  LWP COMMAND             TIME   PID
13101 java            9-22:09:57 13101
[root@60 ~]# ps -p 13101 -o comm,pid,rss,pmem,stat,vsz
COMMAND           PID   RSS %MEM STAT    VSZ
java            13101 2280228  6.9 Sl 9235356

-- 根据端口号查找程序
netstat -ltnup | grep ':22'
-- 查看进程的workingPath
lsof -p 5620 | grep cwd
-- 查找僵尸进程
ps ux | awk '{if($8=="Z") print}'
-- 查号nacos相关进程 分配内存 使用内存
ps eo user,pid,vsz,rss $(pgrep -f 'nacos')
-- 查看进程使用内存
cat /proc/13237/smaps | grep -w 'Pss:' | awk '{Total+=$2} END {print Total/1024" MB"}'
--current working directory 
[root@60 ~]# lsof -p 13101 | grep cwd
java    13101 root  cwd       DIR              253,0      4096  100663361 /root
[root@60 ~]# ls -l /proc/13101/cwd
lrwxrwxrwx 1 root root 0 921 09:25 /proc/13101/cwd -> /root
[root@60 ~]# readlink -e /proc/13101/cwd
/root

## Linux信号
kill -s SIGTERM 123

1 SIGHUP 挂起进程
2 SIGINT 中断进程
3 SIGQUIT 停止进程
9 SIGKILL 无条件终止进程
15 SIGTERM 尽可能终止进程
18 SIGONT 继续运行停止的进程
19 SIGSTOP 无条件停止,但不终止进程
20 SIGTSTP 停止或暂停,但不终止进程
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lang20150928

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值