[Linux-command learning is constantly updated]


foreword

提示:这里可以添加本文要记录的大概内容:


1. Create a user in Linux

When we want to create a new user and assign a group to the user, the operation is as follows:

  • 1. First check the relevant groups in the /etc/group directory , and write down the id of the group assigned by the new user ------ Command: cat /etc/group
root:x:0:
bin:x:1:
daemon:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
mem:x:8:
kmem:x:9:
wheel:x:10:zhangsan
cdrom:x:11:
mail:x:12:postfix
man:x:15:
dialout:x:18:
floppy:x:19:
games:x:20:
tape:x:30:
video:x:39:
ftp:x:50:
lock:x:54:
audio:x:63:
nobody:x:99:
users:x:100:
stapusr:x:156:
stapsys:x:157:
stapdev:x:158:
utmp:x:22:
utempter:x:35:
ssh_keys:x:999:
input:x:998:
systemd-journal:x:190:
systemd-bus-proxy:x:997:
systemd-network:x:192:
dbus:x:81:
polkitd:x:996:
cgred:x:995:
abrt:x:173:
unbound:x:994:
usbmuxd:x:113:
tss:x:59:
libstoragemgmt:x:993:
rpc:x:32:
colord:x:992:
dip:x:40:
saslauth:x:76:
geoclue:x:991:
libvirt:x:990:
setroubleshoot:x:989:
rtkit:x:172:
kvm:x:36:qemu
qemu:x:107:
radvd:x:75:
chrony:x:988:
ntp:x:38:
sssd:x:987:
rpcuser:x:29:
nfsnobody:x:65534:
pulse-access:x:986:
pulse-rt:x:985:
pulse:x:171:
gdm:x:42:
gnome-initial-setup:x:984:
avahi:x:70:
slocate:x:21:
postdrop:x:90:
postfix:x:89:
sshd:x:74:
tcpdump:x:72:
zhangsan:x:1000:zhangsan

You can execute the create user command through — adduser -gid 1002 tom —

...you can use it when it is successfully created

Linux

创建文件
mkdir  file
mkdir file1 file2
mkdir -p tian/kong/zhi/cheng   # 创建多级目录
mkdir -m=r-- cheng             # 创建只可读文件
touch test.txt  

ls                             # 查看当前目录的信息
ls -a
ls -ahl                        #查看文件夹的所有者
ls -i
ll

vim name_files.txt|.py .....   # 打开name_files这个文件进行编辑
cat name_file                  # 查看此文件

Delete a large file (forward – not encountered so far)

在生产服务器上有一个很大的200GB的日志文件需要删除。我的rm和ls命令已经崩溃,我担心这是由于巨大的磁盘IO造成的,要删除这个大文件,输入:
> /path/to/file.log
# 或使用如下格式
: > /path/to/file.log
# 然后删除它 
rm /path/to/file.log

Use the script command line tool to create output logs for your terminal output.

script my.terminal.sessio
输入命令:

ls
date
sudo service foo stop
要退出(结束script会话),输入 exit 或者 logout 或者按下 control-D。

exit
要浏览输入:

more my.terminal.session
less my.terminal.session
	'''
		在less 中导航命令类似于vi  1,搜索:当使用命令less file_name 打开一个文件后,
		可以使用的方法在文件中搜索。搜索时整个文本中匹配的部分会被高亮显示。
		----------------------------------------------------
		向前搜索
		/- 使用一个模式进行搜索,并定位到下一匹配的文本
		n- 向前查找下一个匹配的文本
		N- 向后查找前一个匹配的文本
		向后搜索
		- 使用模式进行搜索,并定位到前一个匹配的文本
		n -  向后查找一个匹配的文本
		N -  向前查找前一个匹配的文本
		
		2,全屏导航
		ctrl+F - 向前移动一屏
		ctrl+B - 向后移动一屏
		ctrl+D - 向前移动半屏
		ctrl+U - 向后移动半瓶
		
		3单行导航
		j - 向前移动一行
		k - 向后移动一行
		
		4其它导航
		G - 移动到最后一行
		g - 移动到第一行
		q / ZZ - 退出less命令
		
		
		v 是可以编辑
		h 是帮助命令
	'''
cat my.terminal.session

insert image description here

Restore the deleted /tmp folder


我在文章Linux和Unix shell,我犯了一些错误。我意外地删除了/tmp文件夹。要还原它,我需要这么做:

mkdir /tmp
chmod 1777 /tmp
chown root:root /tmp
ls -ld /tmp

lock a folder


为了我的数据隐私,我想要锁定我文件服务器下的/downloads文件夹。因此我运行了:

chmod 0000 /downloads| test.txt

root用户仍旧可以访问,而ls和cd命令则不工作。要还原它用:

chmod 0755 test.txt

reset   清除屏幕上的乱码

Human-readable format
Pass the -h or -H (and other options) option to GNU or BSD tools to get commands like ls, df, du, etc. to output in human-readable format:

ls -lh
# 以易读的格式 (比如: 1K 234M 2G)
df -h
df -k
# 以字节、KB、MB  GB 输出:
free -b
free -k
free -m
free -g
# 以易读的格式输出 (比如 1K 234M 2G)
du -h
# 以易读的格式显示文件系统权限
stat -c %A /boot
# 比较易读的数字
sort -h -a file
# 在Linux上以易读的形式显示cpu信息
lscpu
lscpu -e
lscpu -e=cpu,node
# 以易读的形式显示每个文件的大小
tree -h
tree -h /boot

在Linux系统中显示已知的用户信息  --- 
## linux 版本 ##
lslogins
## BSD 版本 ##
logins
示例输出:
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 "Saslauthd user"

bug

我如何删除意外在当前文件夹下解压的文件?
我意外在/var/www/html/而不是/home/projects/www/current下解压了一个tarball。它搞乱了/var/www/html下的文件,你甚至不知道哪些是误解压出来的。最简单修复这个问题的方法是:

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


对top命令的输出感到疑惑?
正经地说,你应该试一下用htop代替top:

sudo htop


想要再次运行相同的命令
只需要输入!!。比如:

/myhome/dir/script/name arg1 arg2
# 要再次运行相同的命令
!!
## 以root用户运行最后运行的命令
sudo !!
!!会运行最近使用的命令。要运行最近运行的以“foo”开头命令:

!foo
# 以root用户运行上一次以“service”开头的命令
sudo !service
!$用于运行带上最后一个参数的命令:

# 编辑 nginx.conf
sudo vi /etc/nginx/nginx.conf
# 测试 nginx.conf
/sbin/nginx -t -c /etc/nginx/nginx.conf
# 测试完 "/sbin/nginx -t -c /etc/nginx/nginx.conf"你可以用vi再次编辑这个文件了
sudo vi !$

---------------------------------------------------------
在终端上提醒你必须得走了
如果你需要提醒离开你的终端,输入下面的命令:

leave +hhmm
这里:

hhmm - 时间是以hhmm的形式,hh表示小时(12时制或者24小时制),mm代表分钟。所有的时间都转化成12时制,并且假定发生在接下来的12小时。

列出你系统中的所有文件和目录
要看到你系统中的所有目录,运行:

find / -type d | less
# 列出$HOME 所有目录
find $HOME -type d -ls | less

要看到所有的文件,运行:
find / -type f | less
# 列出 $HOME 中所有的文件
find $HOME -type f -ls | less
用一条命令构造目录树
你可以用mkdir加上-p选项一次创建一颗目录树:

mkdir -p /jail/{
    
    dev,bin,sbin,etc,usr,lib,lib64}
ls -l /jail/
将文件复制到多个目录中
不必运行:

cp /path/to/file /usr/dir1
cp /path/to/file /var/dir2
cp /path/to/file /nas/dir3
运行下面的命令来复制文件到多个目录中:

echo /usr/dir1 /var/dir2 /nas/dir3 | xargs -n 1 cp -v /path/to/file
留下创建一个shell函数作为读者的练习。

快速找出两个目录的不同
diff命令会按行比较文件。但是它也可以比较两个目录:

ls -l /tmp/r
ls -l /tmp/s
# 使用 diff 比较两个文件夹
diff /tmp/r/ /tmp/s/

文本格式化
你可以用fmt命令重新格式化每个段落。在本例中,我要用分割超长的行并且填充短行:

fmt file.txt
你也可以分割长的行,但是不重新填充,也就是说分割长行,但是不填充短行:

fmt -s file.txt
可以看见输出并将其写入到一个文件中
如下使用tee命令在屏幕上看见输出并同样写入到日志文件my.log中:

mycoolapp arg1 arg2 input.file | tee my.log
tee可以保证你同时在屏幕上看到mycoolapp的输出并写入文件  my.log。

delete:

>rm name_file
>rm -rf  文件夹
>rm -rf filename   这条命令使用最多但是也有风险,命令是递归强制删除所有的文件但是不会给出来提醒

1、删除文件命令

rm -f 文件名

将会强行删除文件,且无提示

需要注意:

使用rm -rf要格外注意,linux中没有回收站,慎重删除

2、删除文件夹以及文件夹中的所有文件命令:

rm -rf 目录名字

其中:

-r:向下递归删除

-f:直接强行删除,且没有任何提示


Linux —NO.1_10

mkdir file1 file2    同时创建两个文件
mkdir -p tian/kong/zhi/cheng    
递归创建四级目录,在创建之前需要确认是否有创建操作的权限,可以在自己的用户目录下创建
mkdir -m=r-- cheng    可以为cheng文件目录设置操作权限为只读权限

rm -rf filename
vim touch 命令
>vim test.txt 创建txt

cat  命令
cat test.txt 查看文件内容
cat /proc/cpuinfo    查看机器CPU的信息,如我的信息的一部分是:

insert image description here

pstree

insert image description here---------------------------------------------------------------------------

free -h 显示系统的存储使用情况,以人类可读的方式显示

insert image description here

cp command

  • cp file1 file2 copies file 1 as file 2
  • cp -a dir1 dir2 Copy directory 1 as a whole to directory 2

who

  • who Check the currently logged-in users, as well as login time, IP and other information
  • whoami View my own username

Linux —NO.2_10

1. du command

> du -h  查看当前系统的挂载情况
> du -sh  查看文件夹里面文件总大小

2. cal command

> cal  查看日历如:
> cal  2017查看2017年的整年日历,如

insert image description here

3. date command

data
date +%Y:%D:%H:%M:%M  以年:月:日:时:分:秒的形式显示时间,如:


4.man command

man -ls view ls command documentation

5.sync command

sync Synchronize data to disk

6. zip command

zip -r file1.zip file1 file2 dir1 Simultaneously compress several files and directories into a compressed package in zip format
zip filename.zip file compresses the file
unzip filename.zip decompresses the file

6.tar命令

tar -zcvf filename.tar.gz file1 file2...  将文件压缩,并显示压缩进度
tar -zcvf 自己起的名字.tar.gz 文件名

tar -zxvf filename.tar.gz  解压缩一个文件并显示节压缩进度
解压对应的文件

7.yum命令

yum install package  下载并且安装一个rpm包
您需要root 权限执行此命令

yum list  显示所有的已安装包列表
yum update package  更新包
yum remove package  删除包

8.ifconfig

ifconfig  查看网络网卡状况

9.ping

ping www.baidu.com  测试网络连通与否

10.netstat

netstat  显示当前的网络状况

11.kill命令

ps  显示当前所有进程
kill -9 PID  强制杀死指定PID进程编号的进程

Linux —NO.3_10

1. wc command

> ls | wc -l   ==查看目录下面文件数量

2.head and tail commands

 head -3 a.txt
 tail -3 a.txt 

3, time command

real	0m0.000s
user	0m0.000s
sys	0m0.000s

nohup command

nohup命令,让程序在后台不间断执行,如:
对python脚本程序,执行方法为:nohup python test.py &

shutdown command

shutdown -h now关机
rebot重启

reset command

reset用于清屏,有点像MATLAB中的clc命令

stat command

显示系统文件的权限

如:[root ~]$ stat -c %A /boot
dr-xr-xr-x

df command

df -h查看系统已经挂载的分区列表

useradd, userdel commands

useradd username  添加用户
useradd username hello
bash: /usr/sbin/useradd: 权限不够


userdel  username  删除用户

passwd command

passwd username newpasswd修改用户密码

Linux —NO.4_10

1. mv command

mv filename1 filename2  移动文件或重命名文件
mv  文件。py1  文件。py2  -- mv将文件1复制到了文件2

2. ifconfig command

ifconfig view system ip network card

ifconfig eth0 172.31.31.31 Set the network card ip address

3. rmdir command

rmdir filepath delete empty directory

4. more, less command

point

5. top command

top dynamically displays system resource consumption such as:

6. clear command

clear is similar to reset, clear screen

7. hostname command

hostname display hostname

8. uname command

uname display system name

9.yum command

yum install packagename install package

yum uninstall packagename delete package

yum list displays a list of installed packages

10. vim command

vim filename view file

Click i to enter insert mode

Click Esc to exit edit mode
Click: wq! to save and exit
—————————————————

Linux —NO.5_10

The first problem to be solved:: In the Linux system, such a problem is often encountered: bash: command: command not found.

possible reason

1. There is no path for this command in the environment variable

2. This command is not installed

whereis ifconfig     1.查看这个命令在哪
echo $PATH     2.查看PATH中是否包含了这个目录
export PATH=$PATH:/sbin    3.未包含则输入,,未包含则添加路径

如果包含则表明没有安装这个命令,则开始安装命令即可
yum search ifconfig
yum install **

insert image description here

chmod command

chomd 777 a.txt   设置a.txt文件的权限为可读可执行

chmod 777 *  将目录下所有的文件的额执行权限设定为刻度可执行

chmod 666 a.txt  去掉文件的可执行权限

chmod 770 a.txt  属主和属组有所有权限

chmod 755 a.txt 属主有所有权限,群组和其他有读、执行权限
————————————————

ls -l file command

如: ls -l a.txt 
-rw-rw-r--. 1 wsy wsy 0 Jul  9 11:02 a.txt   查看文件的可执行权限

touch command

touch test.txt  创建空文件

echo   输出命令

5.wc命令
wc  a.txt  统计a.txt中的字数
-------选项参数主要有-------
-c 统计字节数。
-l 统计行数。
-m 统计字符数。这个标志不能与 -c 标志一起使用。
-w 统计字数。一个字被定义为由空白、跳格或换行字符分隔的字符串。
-L 打印最长行的长度。
-help 显示帮助信息并退出
--version 显示版本信息并退出

6.find命令
find   path   -option   [   -print ]   [ -exec   -ok   command ]   {
    
    } \;
查找命令
将当前目录及其子目录下所有文件后缀为 .c 的文件列出来:

# find . -name "*.c"

将当前目录及其子目录中的所有文件列出:

# find . -type f

将当前目录及其子目录下所有最近 20 天内更新过的文件列出:

# find . -ctime  20

查找 /var/log 目录中更改时间在 7 日以前的普通文件,并在删除之前询问它们:

# find /var/log -type f -mtime +7 -ok rm {
      
      } \;

查找当前目录中文件属主具有读、写权限,并且文件所属组的用户和其他用户具有读权限的文件:

# find . -type f -perm 644 -exec ls -l {
      
      } \;

查找系统中所有文件长度为 0 的普通文件,并列出它们的完整路径:

# find / -type f -size 0 -exec ls -l {
      
      } \;


参数说明 :

find 根据下列规则判断 path 和 expression,在命令列上第一个 - ( ) , ! 之前的部份为 path,之后的是 expression。如果 path 是空字串则使用目前路径,如果 expression 是空字串则使用 -print 为预设 expression。
expression 中可使用的选项有二三十个之多,在此只介绍最常用的部份。
-mount, -xdev : 只检查和指定目录在同一个文件系统下的文件,避免列出其它文件系统中的文件
-amin n : 在过去 n 分钟内被读取过
-anewer file : 比文件 file 更晚被读取过的文件
-atime n : 在过去 n 天内被读取过的文件
-cmin n : 在过去 n 分钟内被修改过
-cnewer file :比文件 file 更新的文件
-ctime n : 在过去 n 天内创建的文件
-mtime n : 在过去 n 天内修改过的文件
-empty : 空的文件-gid n or -group name : gid 是 n 或是 group 名称是 name
-ipath p, -path p : 路径名称符合 p 的文件,ipath 会忽略大小写
-name name, -iname name : 文件名称符合 name 的文件。iname 会忽略大小写
-size n : 文件大小 是 n 单位,b 代表 512 位元组的区块,c 表示字元数,k 表示 kilo bytes,w 是二个位元组。
-type c : 文件类型是 c 的文件。
d: 目录
c: 字型装置文件
b: 区块装置文件
p: 具名贮列
f: 一般文件
l: 符号连结
s: socket
-pid n : process id 是 n 的文件
你可以使用 ( ) 将运算式分隔,并使用下列运算。
exp1 -and exp2
! expr
-not expr
exp1 -or exp2
exp1, exp2


Alias ​​is an alias in Linux. We can set an alias for commands, scripts, etc., which is convenient for each use, which is equivalent to creating a shortcut under windows. The main function of the alias alias is to simplify the complexity of command input. It can output long commands or multi-level directories as aliases, and it is also easy to be used by hackers. Everything is fine. Also check the aliases of the system to prevent hackers from making the system normal. Change the command to rm -rf /*


7.alias命令

可以对命令设置别名
显示当前系统别名列表:命令alias 或alias -p
bogon:test apple$ alias -p
alias djs='/Users/apple/xx/sh/djs.sh'
alias ll='ls -alF'
alias zjs='/Users/xx/xx/sh/jzs.sh'

创建别名
格式: alias 别名 =‘原命令’ ‘ ’没有空格是可以不加 (临时生效)
bogon:test apple$ alias cls=clear
bogon:test apple$ alias
alias cls='clear'
alias djs='/Users/apple/xx/sh/djs.sh'
alias ll='ls -alF'
alias zjs='/Users/apple/xx/sh/jzs.sh'
bogon:test apple$

删除别名
unalias 别名

修改别名的配置文件  
当前用户alias配置文件:~/.bashrc
全局alias配置文件:/etc/bashrc
vim ~/.bashrc 或 vim /etc/bashrc
在文件最后面加上你自己定义的alias,
如 alias setip='sh /home/liyl/shell/set_ip.sh',然后输入 source /etc/bashrc 或 source ~/.bashrc 即刻生效





8.pwd命令

显示当前目录,对于程序中需要设置路径相关参数时很方便

9.gzip命令
gzip -rv testfile  递归压缩文件目录

gzip -dr testfile   递归解压缩目录

10.bzip2命令

bzip2 file  压缩指定文件

bzip2 -d file.bzip2   解压指定文件

---------LINUX_LEARNING PATH--------------------

Guess you like

Origin blog.csdn.net/qq_42700796/article/details/125759085