Linux基础常用命令

目录结构

image-20210914191326410

[root@localhost /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr

root 管理员的 home目录 root
其他用户的 home目录在home 目录中

[root@localhost /]# cd /home
[root@localhost home]# ls
lisonglin  marstor
[root@localhost home]# 

Linux 的常用命令

切换目录命令 cd:

使用 cd app
切换到 app 目录 cd …
切换到上一层目录 cd /
切换到系统根目录 cd ~
切换到用户主目录 cd -
切换到上一个所在目录

使用 tab 键来补全文件路径

[root@localhost bin]# cd ..
[root@localhost /]# ls
bin   dev  home  lib64  mnt  proc  run   srv  tmp  var
boot  etc  lib   media  opt  root  sbin  sys  usr
[root@localhost /]# cd lib
[root@localhost lib]# ls
alsa      dracut      grub         kernel          NetworkManager  sysctl.d
binfmt.d  firewalld   jvm          locale          os-release      systemd
cpp       firmware    jvm-exports  modprobe.d      polkit-1        tmpfiles.d
crda      fontconfig  jvm-private  modules         python2.7       tuned
cups      games       kbd          modules-load.d  rpm             udev
debug     gcc         kdump        mozilla         sse2            yum-plugins
[root@localhost lib]# cd gcc
[root@localhost gcc]# cd ..
[root@localhost lib]# 
[root@localhost lib]# 
[root@localhost lib]# cd -
/lib/gcc
[root@localhost gcc]# cd ~
[root@localhost ~]# pwd
/root
[root@localhost ~]# 

列出文件列表:ls ll

ls(list)是一个非常有用的命令,用来显示当前目录下的内容。配合参数的使用,能以不同的方式显示目录内容。
格式:ls[参数] [路径或文件名]

常用:

在 linux 中以 . 开头的文件都是隐藏的文件

  • ls
  • ls -a
    显示所有文件或目录(包含隐藏的文件)
  • ls -l
    缩写成 ll
[root@localhost ~]# ls -1
anaconda-ks.cfg
Desktop
Documents
Downloads
Music
Pictures
Public
Templates
Videos
[root@localhost ~]# ll
total 4
-rw-------. 1 root root 1428 Jun  6 10:54 anaconda-ks.cfg
drwxr-xr-x. 2 root root    6 Jun  6 04:20 Desktop
drwxr-xr-x. 2 root root    6 Jun  6 04:20 Documents
drwxr-xr-x. 2 root root    6 Jun  6 04:20 Downloads
drwxr-xr-x. 2 root root    6 Jun  6 04:20 Music
drwxr-xr-x. 2 root root    6 Jun  6 04:20 Pictures
drwxr-xr-x. 2 root root    6 Jun  6 04:20 Public
drwxr-xr-x. 2 root root    6 Jun  6 04:20 Templates
drwxr-xr-x. 2 root root    6 Jun  6 04:20 Videos
[root@localhost ~]# ls -a
.                .bash_profile  .dbus      .ICEauthority   Pictures           Videos
..               .bashrc        Desktop    .local          Public             .viminfo
anaconda-ks.cfg  .cache         Documents  .mozilla        .rediscli_history
.bash_history    .config        Downloads  Music           .tcshrc
.bash_logout     .csh

创建目录和移除目录:mkdir rmdir

mkdir(make directory)命令可用来创建子目录。

mkdir app 在当前目录下创建 app 目录

mkdir –p app2/test 级联创建 aap2 以及 test目录

[root@localhost marstor]# ls
shell
[root@localhost marstor]# mkdir app
[root@localhost marstor]# ls
app  shell
[root@localhost marstor]# mkdir -p app2/test
[root@localhost marstor]# ls
app  app2  shell
[root@localhost marstor]# cd app2
[root@localhost app2]# ls
test 

rmdir(remove directory)命令可用来删除“空”的子目录:

rmdir app 删除 app 目录

[root@localhost app2]# cd ..
[root@localhost marstor]# rm app2
rm: cannot remove 鈥榓pp2鈥? Is a directory
[root@localhost marstor]# ls
app  app2  shell
[root@localhost marstor]# cd app2
[root@localhost app2]# ls
test
[root@localhost app2]# rmdir test
[root@localhost app2]# ls
[root@localhost app2]# cd ..
[root@localhost marstor]# ls
app  app2  shell
[root@localhost marstor]# rmdir app
[root@localhost marstor]# ls
app2  shell
[root@localhost marstor]#

浏览文件/查看日志

cat、more、less

cat 用于显示文件的内容。格式:cat[参数]<文件名>

more 一般用于要显示的内容会超过一个画面长度的情况。按空格键显示下一个画面。

回车显示下一行内容。

按 q 键退出查看

more yum.conf

  • 空格显示下一页数据
    回车显示下一行的数据

less 用法和 more 类似,不同的是 less 可以通过 PgUp、PgDn 键来控制。

less yum.conf

  • PgUp 和 PgDn 进行上下翻页.
tail

tail 命令是在实际使用过程中使用非常多的一个命令,它的功能是:用于显示文件后几行的内容。

ctrl+c 结束查看

用法:
tail -10 /etc/passwd 查看后 10 行数据
tail -f catalina.log 动态查看日志(*****)

tail -n 10 test.log 查询日志尾部最后10行的日志;
tail -n +10 test.log 查询10行之后的所有日志;

文件操作

rm

rm 删除文件,rm -r 文件:只能删除文件,删除不了文件夹
用法:rm [选项]… 文件…

rm a.txt 删除 a.txt 文件(删除需要用户确认,y/nrm)

删除不询问
rm -f a.txt :不询问,直接删除 rm 删除目录
rm -r a :递归删除不询问递归删除(慎用)
rm -rf a :不询问递归删除
rm -rf * :删除所有文件
rm -rf /* :自杀

[root@localhost marstor]# ls
app2  shell
[root@localhost marstor]# rm -f app2
rm: cannot remove 鈥榓pp2鈥? Is a directory
[root@localhost marstor]# rm -rf app2
[root@localhost marstor]# ls
shell
[root@localhost marstor]# 
[root@localhost marstor]# pwd
/home/marstor
[root@localhost marstor]# ls
shell  testmarstor.txt
[root@localhost marstor]# pwd
/home/marstor
[root@localhost marstor]# touch test.txt
[root@localhost marstor]# ls
shell  testmarstor.txt  test.txt
[root@localhost marstor]# rm test.txt testmarstor.txt 
rm: remove regular empty file 鈥榯est.txt鈥? 
rm: remove regular file 鈥榯estmarstor.txt鈥? y
[root@localhost marstor]# ls
shell  test.txt
[root@localhost marstor]# rm -r test.txt 
rm: remove regular empty file 鈥榯est.txt鈥? n
[root@localhost marstor]# rm -rf test.txt 
[root@localhost marstor]# ls
shell
[root@localhost marstor]# 


cp、mv

cp(copy)命令可以将文件从一处复制到另一处。一般在使用 cp 命令时将一个文件复制成另一个文件或复制到某目录时,需要指定源文件名与目标文件名或目录。

cp a.txt b.txt : 将 a.txt 复制为 b.txt 文件
cp a.txt …/ :将 a.txt 文件复制到上一层目录中

mv 移动或者重命名

mv a.txt …/ :将 a.txt 文件移动到上一层目录中
mv a.txt b.txt :将 a.txt 文件重命名为 b.txt

tar(打包或解压)

tar 命令位于/bin 目录下,它能够将用户所指定的文件或目录打包成一个文件,但不做压缩。

一般Linux 上常用的压缩方式是选用 tar 将许多文件打包成一个文件,再以 gzip 压缩命令压缩成xxx.tar.gz(或称为 xxx.tgz)的文件。

常用参数:
-c:创建一个新 tar 文件
-v:显示运行过程的信息
-f:指定文件名
-z:调用 gzip 压缩命令进行压缩
-t:查看压缩文件的内容
-x:解开 tar 文件

打包:
tar –cvf xxx.tar ./*
打包并且压缩:
tar –zcvf xxx.tar.gz ./*
解压
tar –xvf xxx.tar
tar -zxvf xxx.tar.gz -C /usr/aaa

find

find 指令用于查找符合条件的文件

find / -name “ins*”:查找文件名称是以 ins 开头的文件

find / -name “ins*” –ls
find / -user marstor –ls :查找用户 marstor的文件
find / -user marstor –type d –ls :查找用户 marstor的目录
find / -perm -777 –type d -ls :查找权限是 777 的文件

[root@localhost /]# pwd
/
[root@localhost /]# find / -name "nginx.conf"
/usr/local/nginx-1.9.9/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf
[root@localhost /]# 
[root@localhost /]# find / -name "nginx.*"
/usr/lib/python2.7/site-packages/sos/plugins/nginx.py
/usr/lib/python2.7/site-packages/sos/plugins/nginx.pyc
/usr/lib/python2.7/site-packages/sos/plugins/nginx.pyo
/usr/share/augeas/lenses/dist/nginx.aug
/usr/local/nginx-1.9.9/conf/nginx.conf
/usr/local/nginx-1.9.9/contrib/vim/ftdetect/nginx.vim
/usr/local/nginx-1.9.9/contrib/vim/indent/nginx.vim
/usr/local/nginx-1.9.9/contrib/vim/syntax/nginx.vim
/usr/local/nginx-1.9.9/src/core/nginx.c
/usr/local/nginx-1.9.9/src/core/nginx.h
/usr/local/nginx-1.9.9/src/http/modules/perl/nginx.pm
/usr/local/nginx-1.9.9/src/http/modules/perl/nginx.xs
/usr/local/nginx-1.9.9/man/nginx.8
/usr/local/nginx-1.9.9/objs/src/core/nginx.o
/usr/local/nginx-1.9.9/objs/nginx.8
/usr/local/nginx/conf/nginx.conf
/usr/local/nginx/conf/nginx.conf.default
/usr/local/nginx/logs/nginx.pid
[root@localhost /]# find / -user marstor -ls
find: 鈥?proc/72714/task/72714/fd/5鈥? No such file or directory
find: 鈥?proc/72714/task/72714/fdinfo/5鈥? No such file or directory
find: 鈥?proc/72714/fd/6鈥? No such file or directory
find: 鈥?proc/72714/fdinfo/6鈥? No such file or directory
669464    0 -rw-rw----   1 marstor  mail            0 Jun  6 10:52 /var/spool/mail/marstor

50984047    0 drwx------   3 marstor  marstor        75 Sep 14 07:35 /home/marstor
50984048    4 -rw-r--r--   1 marstor  marstor        18 Mar 31  2020 /home/marstor/.bash_logout
50984049    4 -rw-r--r--   1 marstor  marstor       193 Mar 31  2020 /home/marstor/.bash_profile
50984050    4 -rw-r--r--   1 marstor  marstor       231 Mar 31  2020 /home/marstor/.bashrc
[root@localhost /]# 
[root@localhost /]# find / -user marstor -type d -ls
50984047    0 drwx------   3 marstor  marstor        75 Sep 14 07:35 /home/marstor
[root@localhost /]# find / -user marstor-perm -77  -type d -ls
find: 鈥榤arstor-perm鈥?is not the name of a known user
[root@localhost /]# find / -user marstor -perm -77  -type d -ls
[root@localhost /]# 
grep

查找文件里符合条件的字符串。
用法: grep [选项]… PATTERN [FILE]…

[root@localhost marstor]# pwd
/home/marstor
[root@localhost marstor]# ls
shell  testmarstor.txt
[root@localhost marstor]# grep nginx testmarstor.txt --color
nginx.conf
nginx
[root@localhost marstor]# grep nginx testmarstor.txt --color -A5
nginx.conf
jkljmjklm
dskaj
nginx
ssasad
[root@localhost marstor]# cat testmarstor.txt 
nginx.conf
jkljmjklm
dskaj
nginx
ssasad
[root@localhost marstor]# grep nginx testmarstor.txt --color -A5 -B5
nginx.conf
jkljmjklm
dskaj
nginx
ssasad
[root@localhost marstor]# 

pwd、touch、clear\ctrl+L

[root@localhost marstor]# pwd
/home/marstor
[root@localhost marstor]# ls
shell  testmarstor.txt
[root@localhost marstor]# pwd
/home/marstor
[root@localhost marstor]# touch test.txt
[root@localhost marstor]# ls
shell  testmarstor.txt  test.txt
[root@localhost marstor]# clear

系统管理命令

ps 正在运行的某个进程的状态
ps –ef: 查看所有进程
ps –ef | grep ssh 查找某一进程
kill 1187:杀掉 1187编号的进程
kill -9 2263:强制杀死进程

[root@localhost marstor]# ps -ef|grep ssh
root       1187      1  0 Sep13 ?        00:00:00 /usr/sbin/sshd -D
root       2263   2133  0 Sep13 ?        00:00:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic"
root      58998   1187  0 03:54 ?        00:00:00 sshd: root@pts/1
root      73557  59003  0 07:57 pts/1    00:00:00 grep --color=auto ssh
[root@localhost marstor]# 

管道|

管道是 Linux 命令中重要的一个概念,其作用是将一个命令的输出用作另一个命令的输入。

ls --help | more:分页查询帮助信息
ps –ef | grep java:查询名称中包含 java 的进程

[root@localhost marstor]# ps -ef|grep java
root      73700  59003  0 07:59 pts/1    00:00:00 grep --color=auto java
[root@localhost marstor]# 

ifconfig | more:

cat index.html | more

ps –ef | grep aio

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

?abc!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值