linux基本命令

whoami 显示当前所在用户有效的用户id
id -un 显示当前所在用户有效的用户id
例如:
[root@localhost bin]# whoami
root 显示当前所在用户为root用户
[root@localhost bin]# id -un
root 显示当前所在用户为root用户

hostname 显示主机名

date 查看时间
例如:
[root@localhost bin]# date
2019年 08月 28日 星期三 06:47:39 PKT

基本命令的语法
命令 选项 参数
ls

  -a       查看目录下的所有文件,包括隐藏文件
  -l       长列表显示
   -h     以人性化的方式显示出来
   -d     只列出目录名,不列出其他内容
   -i      显示文件的索引号
   -s     以块形式显示每个文件分配的尺寸
   -t    根据修改时间排序
   -R    递归显示子目录
   -S    按文件的大小排序

命令的选项可以组合
例如:ls -RS
ls -ah
ls -ahi

  1. cd 改变目录

cd 绝对路径 cd /home/alice cd ~alice
cd 相对路径 cd Desktop/abc cd .. cd . cd -

cd - 返回上次目录
cd 直接回家

  1. 创建/复制/移动/删除/查看

1)创建
目录 mkdir

#mkdir [option]<目录名称>
[root@localhost ~]# mkdir /tmp/bj
[root@localhost ~]# mkdir sh
[root@localhost ~]# mkdir /linux

选项
-p 层级创建目录
[root@localhost ~]# mkdir -p /uplooking/linux

1)文件管理

1查看文件
#ls [option] [目录名称]

-l 以详细信息的方式显示文件信息

[root@localhost ~]# ls -l
总用量 4
-rw-------. 1 root root 955 8月 19 23:36 anaconda-ks.cfg

  -              文件
  d             目录
   l             软连接文件(link )
   b            块设备(block),硬盘,硬盘分区,u盘,光盘
   c            字符设备文件(character)    键盘鼠标,显示器

[root@localhost ~]# ls -l /etc/passwd
-rw-r--r--. 1 root root 2444 8月 20 17:19 /etc/passwd

-h 以人性化的方式显示出来文件信息

[root@localhost ~]# ls -lh /etc/passwd
-rw-r--r--. 1 root root 2.4K 8月 20 17:19 /etc/passwd

-d 显示目录自身的信息

[root@localhost ~]# ls -ldh /etc
drwxr-xr-x. 143 root root 8.0K 8月 30 06:53 /etc

-a 显示所有文件以及隐藏文件(以.开头的文件)

[root@localhost ~]# ls -a
. .bash_history .bashrc .cshrc .ICEauthority .tcshrc 视频 下载
.. .bash_logout .cache .dbus .local 公共 图片 音乐
anaconda-ks.cfg .bash_profile .config .esd_auth .pki 模板 文档 桌面

-t 按文件修改时间降序排列

[root@localhost ~]# ls -lt
总用量 4
-rw-r--r--. 1 root root 0 8月 30 07:21 1.txt
drwxr-xr-x. 2 root root 6 8月 30 06:55 公共
drwxr-xr-x. 2 root root 6 8月 30 06:55 视频
drwxr-xr-x. 2 root root 6 8月 30 06:55 图片
drwxr-xr-x. 2 root root 6 8月 30 06:55 文档
drwxr-xr-x. 2 root root 6 8月 30 06:55 音乐
drwxr-xr-x. 2 root root 6 8月 30 06:55 模板
drwxr-xr-x. 2 root root 6 8月 30 06:55 下载
drwxr-xr-x. 2 root root 6 8月 30 06:55 桌面
-rw-------. 1 root root 955 8月 19 23:36 anaconda-ks.cfg

-S 按文件大小降序排列

[root@localhost ~]# ls -lhS
总用量 4.0K
-rw-------. 1 root root 955 8月 19 23:36 anaconda-ks.cfg
drwxr-xr-x. 2 root root 6 8月 30 06:55 公共
drwxr-xr-x. 2 root root 6 8月 30 06:55 模板
drwxr-xr-x. 2 root root 6 8月 30 06:55 视频
drwxr-xr-x. 2 root root 6 8月 30 06:55 图片
drwxr-xr-x. 2 root root 6 8月 30 06:55 文档
drwxr-xr-x. 2 root root 6 8月 30 06:55 下载
drwxr-xr-x. 2 root root 6 8月 30 06:55 音乐
drwxr-xr-x. 2 root root 6 8月 30 06:55 桌面
-rw-r--r--. 1 root root 0 8月 30 07:21 1.txt

2)统计目录大小
[root@localhost ~]# du -sh /etc/passwd
4.0K /etc/passwd

-a 显示目录中个别文件的大小
[root@localhost ~]# du -ah /etc/

3)创建文件

#touch <文件名称>

花括号展{}开:
[root@localhost ~]# touch /tmp/1.txt

[root@localhost ~]# touch /tmp/{1,3,9}.jpg 只创建 1,3,9的jpg格式的文件

[root@localhost ~]# touch /tmp/{1..10}.mp3 创建1到10的.mp3的文件

[root@localhost ~]# touch /tmp/{a,b,c}.txt

[root@localhost ~]# date
2019年 08月 30日 星期五 07:59:13 PKT

[root@localhost ~]# date +%F
2019-08-30

[root@localhost ~]# date +%T
08:02:33

[root@localhost ~]# date +%H 只显示小时
08

[root@localhost ~]# date +%M 只显示分钟
05

[root@localhost ~]# date +%m 只显示秒
08

[root@localhost ~]# date +%F-%T 具体年月日-具体时间
2019-08-30-08:08:31

命令引用:

方法1:$(command)

[root@localhost ~]# touch /tmp/$(date +%F-%T).log
[root@localhost ~]# ls /tmp
2019-08-30-08:13:42.log

方法2:反引号

[root@localhost ~]# touch /tmp/date +%F-%T.log
[root@localhost ~]# ls /tmp
2019-08-30-08:23:08.log

4)复制文件目录

#cp [option] 源文件 目标文件

选项:

-r 复制目录时

[root@localhost ~]# cp -r /linux/ /windows/

-f 强制覆盖

[root@localhost ~]# cp -fn /linux/1.txt /windows/

5)移动文件目录

#mv 源文件 目标文件

[root@localhost ~]# mv /linux/1.txt /linux/66.txt

6)删除文件目录

#rm [option] 文件名称

[root@localhost ~]# rm /linux/66.txt
rm:是否删除普通空文件 "/linux/66.txt"?

-r 删除目录时

[root@localhost ~]# rm -r /linux/66.txt
rm:是否删除普通空文件 "/linux/66.txt"?

-f 强制删除目录
[root@localhost ~]# rm -rf /linux/66.txt

7)统计文件的行数

[root@localhost ~]# wc -l /etc/passwd
46 /etc/passwd

[root@localhost ~]# wc /etc/passwd
46 96 2444 /etc/passwd 查看文件行数,字数,字节数

8)查看文件内容 ------cat/more/less/head/tail

  1. cat

[root@localhost ~]# cat /etc/fstab

#

/etc/fstab

Created by anaconda on Tue Aug 20 02:30:01 2019

#

Accessible filesystems, by reference, are maintained under '/dev/disk'

See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#
/dev/mapper/centos-root / xfs defaults 0 0
UUID=72251e67-7f60-4598-99cd-67f415c4496d /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0

[root@localhost ~]# cat -n /etc/fstab 显示所有航
1
2 #
3 # /etc/fstab
4 # Created by anaconda on Tue Aug 20 02:30:01 2019
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 /dev/mapper/centos-root / xfs defaults 0 0
10 UUID=72251e67-7f60-4598-99cd-67f415c4496d /boot xfs defaults 0 0
11 /dev/mapper/centos-swap swap swap defaults 0 0

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

  1. more/less分页内容

[root@localhost ~]# more /etc/fstab 用b翻页

[root@localhost ~]# less /etc/fstab 能用上下键翻页

head/tail

head -n 具体要显示几行 从头开始第几行

[root@localhost ~]# head -n 3 /etc/passwd 显示前3行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

[root@localhost ~]# head /etc/passwd 默认显示前10行
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

[root@localhost ~]# tail -n 3 /etc/passwd 后3行
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
roo:x:1000:1000:root:/home/roo:/bin/bash

[root@localhost ~]# tail /etc/passwd 默认提示后10行
rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
nfsnobody:x:65534:65534:Anonymous NFS User:/var/lib/nfs:/sbin/nologin
pulse:x:171:171:PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
saned:x:988:982:SANE scanner daemon user:/usr/share/sane:/sbin/nologin
sssd:x:987:981:User for sssd:/:/sbin/nologin
gdm:x:42:42::/var/lib/gdm:/sbin/nologin
gnome-initial-setup:x:986:980::/run/gnome-initial-setup/:/sbin/nologin
avahi:x:70:70:Avahi mDNS/DNS-SD Stack:/var/run/avahi-daemon:/sbin/nologin
tcpdump:x:72:72::/:/sbin/nologin
roo:x:1000:1000:root:/home/roo:/bin/bash

9)查看文件类型
[root@localhost ~]# file /etc/passwd
/etc/passwd: ASCII text

[root@localhost ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls

[root@localhost ~]# file /usr/bin/ls
/usr/bin/ls: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.32, BuildID[sha1]=aa7ff68f13de25936a098016243ce57c3c982e06, stripped

管道符的使用: |

[root@localhost ~]# ls -hlS /etc/ |head -n 5
总用量 1.5M
-rw-r--r--. 1 root root 655K 6月 7 2013 services
-rw-r--r--. 1 root root 79K 8月 20 23:52 ld.so.cache
-rw-r--r--. 1 root root 25K 8月 6 2015 dnsmasq.conf
-rw-r--r--. 1 root root 22K 4月 11 2018 brltty.conf

[root@localhost ~]# ls -lt /etc/ |head -n 11
总用量 1496
drwxr-xr-x. 5 root lp 4096 8月 30 12:46 cups
-rw-r--r--. 1 root root 73 8月 30 06:53 resolv.conf
drwxr-xr-x. 6 root root 4096 8月 30 06:53 sysconfig
drwx------. 5 root root 4096 8月 21 00:12 libvirt
-rw-r--r--. 1 root root 80791 8月 20 23:52 ld.so.cache
drwxr-xr-x. 2 root root 4096 8月 20 23:52 alternatives
drwxr-xr-x. 2 root root 4096 8月 20 23:52 bash_completion.d
drwxr-xr-x. 2 root root 54 8月 20 23:52 cron.daily
drwxr-xr-x. 2 root root 21 8月 20 23:52 cron.hourly
drwxr-xr-x. 6 root root 95 8月 20 23:52 yum

10)查找文件目录

find 路径 查找方式

按文件名查找

[root@localhost ~]# find /etc/ -name "*.conf"

[root@localhost ~]# find /etc/ -name "*.conf" |wc -l
452

按文件大小查找

[root@localhost ~]# find /etc/ -size +500k

[root@localhost ~]# find /etc/ -size +1M

按文件修改时间查找
[root@localhost ~]# find /etc -mtime +5 修改时间超过5天的

[root@localhost ~]# find /etc -mtime 5 修改时间等于5天的

[root@localhost ~]# find /etc/ -mtime -5 修改时间为5天内的

文件类型查找

[root@localhost ~]# find /dev/ -type b 查找块设备类型的
/dev/dm-1
/dev/dm-0
/dev/sr0
/dev/sda2
/dev/sda1
/dev/sda

[root@localhost ~]# find /dev/ -type l 查找链接类型的文件

扩展知识:

dos2unix 将windows格式的文件转换成unix

unix2dos 将unix格式的文件转换成windows

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章