Linux文件操作命令詳解與實(shí)戰(zhàn)
1. 文件的基本操作
1.1 文件創(chuàng)建
touch 命令
touch
用于創(chuàng)建空文件或更新文件的時(shí)間戳。
- 用法:
# 創(chuàng)建一個(gè)空文件 touch file1.txt # 同時(shí)創(chuàng)建多個(gè)文件 touch file1.txt file2.txt # 更新文件時(shí)間戳 touch -a file1.txt
cat 和 echo
cat
創(chuàng)建文件:
# 創(chuàng)建并寫入內(nèi)容 cat > file.txt <<EOF This is a test file. EOF
echo
寫入內(nèi)容:
echo "Hello, World!" > file.txt
1.2 文件刪除
rm 命令
rm
用于刪除文件或目錄。
- 用法:
# 刪除單個(gè)文件 rm file1.txt # 刪除多個(gè)文件 rm file1.txt file2.txt # 強(qiáng)制刪除 rm -f file1.txt # 刪除目錄及其內(nèi)容 rm -r directory_name
- 注意:
rm -rf /
是高危操作,可能導(dǎo)致系統(tǒng)文件丟失,需慎用。
1.3 文件重命名和移動(dòng)
mv 命令
mv
用于重命名文件或移動(dòng)文件。
- 用法:
# 重命名文件 mv old_name.txt new_name.txt # 移動(dòng)文件到指定目錄 mv file.txt /path/to/directory/ # 重命名并移動(dòng) mv file1.txt /path/to/directory/new_file.txt
2. 文件內(nèi)容查看與編輯
2.1 查看文件內(nèi)容
cat 命令
cat
是最常用的查看文件內(nèi)容的工具。
- 用法:
# 查看整個(gè)文件內(nèi)容 cat file.txt # 帶行號顯示 cat -n file.txt
less 命令
less
用于分頁查看大文件內(nèi)容。
用法:
less file.txt
導(dǎo)航:
- 向下翻頁:
Space
或f
- 向上翻頁:
b
- 退出:
q
- 向下翻頁:
tail 和 head 命令
tail
查看文件末尾內(nèi)容:
tail file.txt # 查看最后 20 行 tail -n 20 file.txt
head
查看文件開頭內(nèi)容:
head file.txt # 查看前 10 行 head -n 10 file.txt
2.2 編輯文件
nano 和 vim 編輯器
nano
操作簡單,適合新手:
nano file.txt
vim
功能強(qiáng)大,適合進(jìn)階用戶:
vim file.txt
3. 文件權(quán)限管理
3.1 查看文件權(quán)限
ls -l 命令
用法:
ls -l file.txt
輸出格式:
-rw-r--r-- 1 user group 1024 Dec 6 12:34 file.txt
- 第一列:權(quán)限標(biāo)志(
r
可讀,w
可寫,x
可執(zhí)行)。 - 第二列:鏈接數(shù)。
- 第三、四列:所屬用戶和組。
- 后續(xù):文件大小、修改時(shí)間、文件名。
- 第一列:權(quán)限標(biāo)志(
3.2 修改文件權(quán)限
chmod 命令
用法:
# 添加權(quán)限 chmod +x file.sh # 刪除權(quán)限 chmod -w file.txt # 設(shè)置權(quán)限 chmod 644 file.txt
符號與數(shù)字模式:
r=4
,w=2
,x=1
。644
表示所有者可讀寫,組和其他用戶只讀。
3.3 修改文件所屬用戶或組
chown 命令
- 用法:
# 更改文件所有者 sudo chown user file.txt # 更改文件所有者和組 sudo chown user:group file.txt
4. 文件搜索與定位
4.1 find 命令
find
用于根據(jù)條件搜索文件。
- 用法:
# 按名稱搜索 find /path -name "file.txt" # 按大小搜索 find /path -size +100M # 按修改時(shí)間搜索 find /path -mtime -7
4.2 locate 命令
locate
利用索引快速定位文件。
- 用法:
# 搜索文件 locate file.txt
4.3 grep 命令
grep
搜索文件內(nèi)容中的指定模式。
- 用法:
# 搜索單詞 grep "word" file.txt # 遞歸搜索目錄 grep -r "pattern" /path
5. 文件壓縮與解壓
5.1 tar 命令
tar
用于歸檔文件。
- 用法:
# 壓縮文件 tar -czvf archive.tar.gz file1 file2 # 解壓文件 tar -xzvf archive.tar.gz
5.2 zip 和 unzip
壓縮文件:
zip archive.zip file1 file2
解壓文件:
unzip archive.zip
6. 文件傳輸
6.1 本地傳輸
cp source.txt destination.txt
6.2 網(wǎng)絡(luò)傳輸
scp
命令:
scp file.txt user@remote:/path
7. 文件操作實(shí)戰(zhàn)案例
7.1 統(tǒng)計(jì)日志文件行數(shù)
wc -l /var/log/syslog
7.2 刪除超過 30 天的日志文件
find /var/log -type f -mtime +30 -exec rm {} \;
7.3 查找大文件
find / -type f -size +1G
7.4 批量修改文件權(quán)限
find /path -type f -name "*.sh" -exec chmod +x {} \;
總結(jié)
Linux 提供了豐富的文件操作命令,從文件的創(chuàng)建、編輯到權(quán)限管理,再到搜索和壓縮,涵蓋了日常管理的各個(gè)方面。通過本文的系統(tǒng)學(xué)習(xí),讀者不僅能快速掌握常用命令,還能應(yīng)用到實(shí)際工作中,提升效率和管理能力。
以上就是Linux文件操作命令詳解與實(shí)戰(zhàn)的詳細(xì)內(nèi)容,更多關(guān)于Linux文件操作的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
shell腳本自動(dòng)刪除30天以前的文件(最新推薦)
該文章介紹了如何使用Shell腳本自動(dòng)刪除指定目錄下30天以前的文件,并通過crontab設(shè)置定時(shí)任務(wù),此外,還提供了如何使用Shell腳本刪除Elasticsearch索引的參考,感興趣的朋友一起看看吧2025-02-02在Linux與Windows上獲取當(dāng)前堆棧信息的方法
下面小編就為大家?guī)硪黄贚inux與Windows上獲取當(dāng)前堆棧信息的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06