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

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