Linux 常用命令

  • 查看当前所在的工作目录的全路径 pwd
[root@localhost ~]# pwd
/root
  • 查看当前系统的时间 date
[root@localhost ~]# date +%Y-%m-%d
2022-09-02
[root@localhost ~]# date +%Y-%m-%d --date='-1 day' 
2022-09-01
[root@localhost ~]# date +%Y-%m-%d --date='-2 month'
2022-07-02
[root@localhost ~]# date +%Y-%m-%d --date='-2 year'
2020-09-02
[root@localhost ~]# date -s "2022-08-28 21:11:00"
  • 查看有谁在线(哪些人登陆到了服务器)
    [root@localhost ~]# who 
    root     pts/0        2022-09-02 04:44 (172.16.253.2)
    [root@localhost ~]# last #查看最近的登陆历史记录
    root     pts/0        172.16.253.2     Fri Sep  2 04:44   still logged in   
    root     pts/0        172.16.253.2     Fri Sep  2 04:25 - 04:34  (00:09)    
    root     pts/0        172.16.253.2     Fri Sep  2 04:04 - 04:21  (00:17)    
    root     pts/0        172.16.253.2     Fri Sep  2 03:49 - 04:00  (00:10)    
    root     pts/1        172.16.253.2     Fri Sep  2 01:40 - 04:07  (02:27)    
    root     pts/1        172.16.253.2     Thu Sep  1 23:56 - 01:04  (01:07)  
  •  关机重启
shutdown -h now  ## 立刻关机
shutdown -h +10  ##  10分钟以后关机
shutdown -h 12:00:00  ##12点整的时候关机
halt   #  等于立刻关机

重启
shutdown -r now
reboot   # 等于立刻重启
  • 创建文件
# 创建一个空文件
touch  test.txt     

# 利用重定向“>”的功能,将一条指令的输出结果写入到一个文件中,会覆盖原文件内容,如果指定的文件不存在,则会创建出来
echo "hello" > test.txt     

# 将一条指令的输出结果追加到一个文件中,不会覆盖原文件内容
echo "hi" >> test.txt    
  • 查看文件内容
cat test.txt      一次性将文件内容全部输出(控制台)
more test.txt      可以翻页查看, 下翻一页(空格)    上翻一页(b)   退出(q)
less test.txt      可以翻页查看,下翻一页(空格)    上翻一页(b),上翻一行(↑)  下翻一行(↓)  可以搜索关键字(/keyword)    跳到文件末尾: G    跳到文件首行: gg    退出less :  q

tail -10 test.txt  查看文件尾部的10行
tail +10 test.txt  查看文件 10-->末行
tail -f test.txt    小f跟踪文件的唯一inode号,就算文件改名后,还是跟踪原来这个inode表示的文件
tail -F test.txt    大F按照文件名来跟踪
head -10 test.txt   查看文件头部的10行
  • 打包压缩
# gzip压缩
gzip a.txt

# 解压
gunzip a.txt.gz
gzip -d a.txt.gz

# bzip2压缩
bzip2 a

# 解压
bunzip2 a.bz2
bzip2 -d a.bz2

# 打包:将指定文件或文件夹
tar -cvf bak.tar  ./aaa
将/etc/password追加文件到bak.tar中(r)
tar -rvf bak.tar /etc/password

# 解压
tar -xvf bak.tar

# 打包并压缩
tar -zcvf a.tar.gz  aaa/

# 解包并解压缩(重要的事情说三遍!!!)
tar  -zxvf  a.tar.gz
# 解压到/usr/下
tar  -zxvf  a.tar.gz  -C  /usr

# 查看压缩包内容
tar -ztvf a.tar.gz
zip /unzip

# 打包并压缩成bz2
tar -jcvf a.tar.bz2

# 解压bz2
tar -jxvf a.tar.bz2
  • grep命令
#查询包含hadoop的行
grep hadoop /etc/password
grep aaa  ./*.txt 

#cut截取以:分割保留第七段
grep hadoop /etc/passwd | cut -d: -f7

#查询不包含hadoop的行
grep -v hadoop /etc/passwd

#正则表达包含hadoop
grep 'hadoop' /etc/passwd

#正则表达(点代表任意一个字符)
grep 'h.*p' /etc/passwd

#正则表达以hadoop开头
grep '^hadoop' /etc/passwd

#正则表达以hadoop结尾
grep 'hadoop$' /etc/passwd

#规则:
#    .  : 任意一个字符
#    a* : 任意多个a(零个或多个a)
#    a? : 零个或一个a
#    a+ : 一个或多个a
#    .* : 任意多个任意字符
#    \. : 转义.
#    o\{2\} : o重复两次

#查找不是以#开头的行
grep -v '^#' a.txt | grep -v '^$' 

#以h或r开头的
grep '^[hr]' /etc/passwd

#不是以h和r开头的
grep '^[^hr]' /etc/passwd

#不是以h到r开头的
grep '^[^h-r]' /etc/passwd
  • 文件权限
# 表示将test.txt对所属组的rw权限取消
chmod g-rw test.txt
# 表示将test.txt对其他人的rw权限取消		 
chmod o-rw test.txt	 
# 表示将test.txt对所属用户的权限增加x
chmod u+x test.txt
# 表示将test.txt对所用户取消x权限		 
chmod a-x test.txt              


#也可以用数字的方式来修改权限
chmod 664 test.txt
#就会修改成   rw-rw-r--
#如果要将一个文件夹的所有内容权限统一修改,则可以-R参数
chmod -R 770 aaa/




#修改文件所有权。<只有root权限能执行>
# 改变所属用户
chown rootadmin  aaa
# 改变所属组		
chown :rootadmin  aaa
# 同时修改所属用户和所属组		
chown rootadmin:rootadmin aaa/	
  • 基本的用户管理
#添加一个用户:
useradd elasticsearch
#根据提示设置密码即可
passwd  elasticsearch     

#删除一个用户,  加一个-r就表示把用户及用户的主目录都删除
userdel -r elasticsearch     

#添加一个elasticsearch用户,设置它属于users组,并添加注释信息
#分步完成:
        useradd elasticsearch
        usermod -g users elasticsearch
	    usermod -c "admin elasticsearch" elasticsearch
#一步完成:
    useradd -g users -c "admin elasticsearch" elasticsearch

#设置tom用户的密码
passwd elasticsearch


#修改elasticsearch用户的登陆名为tomcat
usermod -l tomcat elasticsearch
#将tomcat添加到sys和root组中
usermod -G sys,root tomcat
#查看tomcat的组信息
groups tomcat


#添加一个叫america的组
groupadd america
#将jerry添加到america组中
usermod -g america jerry
#将tomcat用户从root组和sys组删除
gpasswd -d tomcat root
gpasswd -d tomcat sys
#将america组名修改为am
groupmod -n am america



#用root编辑 vi /etc/sudoers
#在文件的如下位置,为hadoop添加一行即可
root    ALL=(ALL)       ALL     
hadoop  ALL=(ALL)       ALL

#然后,hadoop用户就可以用sudo来执行系统级别的指令
sudo useradd xiaoming
  • 统计磁盘占用情况
df -lh
du -sh *

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值