Linux基礎(chǔ)命令-文件處理

1.cd命令

命令 含義
cd 切換目錄
cd - 切換回上一次所在的目錄
cd . 代表當前目錄
cd .. 回上一級目錄
cd ~ 切換回家目錄 root用戶=/root ,oldboy=/home/oldboy

2.mkdir創(chuàng)建目錄

命令 含義
mkdir oldboy 在當前目錄創(chuàng)建oldboy目錄
mkdir -p /oldboy/oldboy01 在根下創(chuàng)建oldboy目錄,在oldboy目錄里創(chuàng)建oldboy01(遞歸創(chuàng)建)
mkdir dir-{1..50} 批量創(chuàng)建目錄

3.touch創(chuàng)建文件

命令 含義
touch shuai.txt 在當前目錄創(chuàng)建shuai.txt文件
[root@shuai ~]# touch shuai.txt
[root@shuai ~]# ls
shuai.txt

4.mv移動文件(相當于剪切)

命令 含義
mv shuai.txt /data/ 將文件移動到/data/目錄下
mv shuai.txt test.txt 文件重命名
mv test.txt /data/shuai.txt 移動并重命名
mv dir-1 dir-2 dir-3 test dir-15/ 將多個文件或目錄移到到指定的位置
[root@shuai ~]# mv shuai.txt /data/
[root@shuai ~]# ls /data/
shuai.txt
[root@shuai data]# mv shuai.txt test.txt
[root@shuai data]# ls
test.txt
[root@shuai data]# mv test.txt /data/shuai.txt
[root@shuai data]# ls
shuai.txt

5.rm刪除

命令 含義
rm -f 強制刪除
rm -r 遞歸刪除

PS: 在使用rm刪除時, 文件用-f 目錄才加-r 盡可能不要-rf一起使用
6.cp拷貝

命令 含義
cp file /tmp/ 復制文件到tmp目錄下
cp file /tmp/file1 復制文件到tmp目錄下并重新命名
cp -p file 復制時保持文件屬性不變
cp -r /etc/ /tmp/ 復制目錄
cp -rv file /etc/hostname /opt/ 復制不同的目錄和不同的文件到同一位置
\cp -r /etc/ /tmp/ 直接覆蓋不提示

7.cat查看文件內(nèi)容

命令 含義
cat file1 查看file1文件全部內(nèi)容
cat -p file1 查看file1文件有多少行
cat -A file1 查看file1文件特殊字符
cat >> test.txt <<EOF 新建test.txt文件,并追加內(nèi)容,后續(xù)是追加
[root@shuai ~]# cat file1 
 www.fushuaizhang.cn  個人博客
[root@shuai ~]# cat -n file1 
     1   www.fushuaizhang.cn  個人博客
     2   www.fushuaizhang.cn  個人博客
     3   www.fushuaizhang.cn  個人博客
     4   www.fushuaizhang.cn  個人博客
     5   www.fushuaizhang.cn  個人博客
     6   www.fushuaizhang.cn  個人博客
     7   www.fushuaizhang.cn  個人博客
     8   www.fushuaizhang.cn  個人博客
     9   www.fushuaizhang.cn  個人博客
    10   www.fushuaizhang.cn  個人博客
    11   www.fushuaizhang.cn  個人博客
    12   www.fushuaizhang.cn  個人博客
    13   www.fushuaizhang.cn  個人博客
    14   www.fushuaizhang.cn  個人博客
    15   www.fushuaizhang.cn  個人博客
    16   www.fushuaizhang.cn  個人博客
    17   www.fushuaizhang.cn  個人博客
[root@shuai ~]# cat -A file1 
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
 www.fushuaizhang.cn  M-dM-8M-*M-dM-:M-:M-eM-^MM-^ZM-eM-.M-"$
[root@shuai ~]# cat >> test.txt <<EOF 
> test1
> test2
> test3
> EOF

8.less查看文件前,空格進行翻頁

命令 含義
less /etc /services 查看service文件前幾行
[root@shuai ~]# less /etc/services

9.more查看文件顯示查看百分比

命令 含義
more /etc /services 查看文件顯示查看百分比
[root@shuai ~]# more /etc/services

10.head查看文件頭部10行內(nèi)容

命令 含義
hade /etc /services 查看文件頭部10行內(nèi)容
hade -2 /etc /services 指定查看行數(shù)
[root@shuai ~]# head /etc/services
[root@shuai ~]# head  -2 /etc/services

11.tail查看文件尾部10行內(nèi)容

命令 含義
tail /etc /services 查看文件尾部10行內(nèi)容
tail -2 /etc /services 指定查看行數(shù)
tail -f /var/log/secure 動態(tài)的查看尾部的信息
[root@shuai ~]# tail /etc/services
[root@shuai ~]# tail  -2 /etc/services
[root@shuai ~]# tail -f /var/log/secure 

12.grep文件過濾

命令 含義
grep "root" /etc/passwd 在passwd文件過濾出root關(guān)鍵字的行
grep "^root" /etc/passwd 在passwd文件過濾出root開頭的行
grep "bash$" /etc/passwd 在passwd文件過濾出bash結(jié)尾的行
grep -n -A 2 "bash$" /etc/passwd 在passwd文件過濾出root開頭的行和下兩行
grep -n -B 2 "bash$" /etc/passwd 在passwd文件過濾出root開頭的行和上兩行
grep -n -C 2 "bash$" /etc/passwd 在passwd文件過濾出root開頭的行和上下各兩行
grep -v "root" /etc/passwd 在passwd文件過濾出除了root關(guān)鍵字的行
grep -i "root" /etc/passwd 忽略大小寫匹配
grep "^root" |"bash$" /etc/passwd 過濾多個目標
[root@shuai ~]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
[root@shuai ~]# grep "^root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@shuai ~]# grep "bash$" /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@shuai ~]# grep -v "root" /etc/passwd
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
games:x:12:100:games:/usr/games:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
systemd-network:x:192:192:systemd Network Management:/:/sbin/nologin
dbus:x:81:81:System message bus:/:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
tss:x:59:59:Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
abrt:x:173:173::/etc/abrt:/sbin/nologin
sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
chrony:x:998:996::/var/lib/chrony:/sbin/nologin
[root@shuai ~]# grep "^root" |"bash$" /etc/passwd

13.rz通過Windows上傳文件
安裝軟件

[root@shuai ~]# yum install -y lrzsz

rz只能上傳文件,不支持上傳文件夾,不支持大于4G的文件,不支持斷點續(xù)傳

4.sz下載文件到Windows
命令 含義
sz file 下載文件到Windows


sz只能下載文件,不支持下載文件夾
14.which和whereis查找命令的絕對路徑

root@shuai ~]# whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz
[root@shuai ~]# whereis -b ls
ls: /usr/bin/ls
[root@shuai ~]# which ls
alias ls='ls --color=auto'
    /usr/bin/ls 
[root@shuai ~]# type -a ls
ls 是 `ls --color=auto' 的別名
ls 是 /usr/bin/ls
##對于內(nèi)核相關(guān)的命令要用type查詢
[root@shuai ~]# type pwd
pwd 是 shell 內(nèi)嵌

15.sort排序

命令 含義
sort file 文件內(nèi)容進行排序
sort -t ":" -k2 -n file2 以冒號尾分隔符,第二行數(shù)字進行排序
[root@shuai ~]# sort file2
a:4
b:3
c:2
d:1
e:5
f:11
[root@shuai ~]# sort -t ":" -k2 -n file2 
d:1
c:2
b:3
a:4
e:5
f:11

IP地址排序小問題及解答

[root@shuai ~]# sort -t "." -k3.1,3.1 -k4.1,4.3 -n ip.txt 
#第三列的第一個字符到第三列的第一個字符k3.1,3.1,從第四列的第一個字符到第四列的第三個字符k4.1,4.3
192.168.0.151 00:0F:AF:85:6C:F6
192.168.0.151 00:0F:AF:85:6C:F6
192.168.0.152 00:0F:AF:83:1F:65
192.168.0.153 00:0F:AF:85:70:03
192.168.0.153 00:0F:AF:85:70:03
192.168.1.1 00:0F:AF:81:19:1F
192.168.1.10 00:30:15:A2:3B:B6
192.168.1.11 00:30:15:A3:23:B7
192.168.1.11 00:30:15:A3:23:B7
192.168.1.12 00:30:15:A2:3A:A1
192.168.1.21 00:0F:AF:85:6C:09
192.168.1.152 00:0F:AF:83:1F:65
192.168.2.2 00:0F:AF:85:6C:25
192.168.2.20 00:0F:AF:85:55:DE
192.168.2.20 00:0F:AF:85:55:DE
192.168.2.21 00:0F:AF:85:6C:09
192.168.2.22 00:0F:AF:85:5C:41
192.168.2.22 00:0F:AF:85:5C:41
192.168.3.1 00:0F:AF:81:19:1F
192.168.3.2 00:0F:AF:85:6C:25
192.18.3.3 00:0F:AF:85:70:423.1
192.168.3.3 00:0F:AF:85:70:42
192.168.3.10 00:30:15:A2:3B:B6
192.168.3.12 00:30:15:A2:3A:A1

16.uniq去重

命令 含義
sort file4 | uniq 文件內(nèi)容進行去重
sort file4| uniq -c 查看重復次數(shù)

uniq只能取出連續(xù)的重復,應該先用sort排序,才能進行去重

[root@shuai ~]# cat file4
111
aaa
222
111
aaa
222
[root@shuai ~]# sort file4 | uniq 
111
222
aaa
[root@shuai ~]# sort file4 | uniq -c
      2 111
      2 222
      2 aaa

17.wc統(tǒng)計行數(shù)

命令 含義
wc -l file 文件行數(shù)統(tǒng)計
[root@shuai ~]# grep "nologin$" /etc/passwd | wc -l
17
[root@shuai ~]# wc -l /etc/passwd
21 /etc/passwd
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容