[Bash] Operations against files
Viewing file
Viewing the whole file:
cat package.json
Viewing the whole file with line number
cat -n package.json
Viewing the file with pagination
less package.json
Highlight the search term, for example, we want to looking for lodash
inside package.json, what we can do is:
less package.json
/lodash
Press q
to exit the viewing.
Open the file
open lib/npm.js
open lib/npm.js -a TextEdit ## when there is no default app accosiate with this file extension
Write content to file
Create a new file
touch file.txt
echo 'hi' > file.txt
It write 'hi' into file.txt, but if we do
echo 'hello world' > file.txt
It overwrites the hi
.
If you want to append
content to the end of file:
echo "hi" >> file.txt
Now inside file we have:
hello world
hi
Remove a file
rm file.txt
Move a file
If you want to move index.js
to a src
folder:
mv index.js src/index.js
Move all files from one dir to another dir
Move all the files under lib
folder to src
folder
mv lib/* src/
Move all the nested folder and files from lib
to src
:
cp -R lib/* src/
Copy file
Copy README.mnd
to src
folder:
cp README.md src/README.md
Rename a file
Rename index.js
file to a.js
:
mv index.js a.js
Find files in folder
Find all png
files under images
folder
find images/ -name "*.png"
It is case sensitive, to ignore case, need to add -i
:
find images/ -iname "*.png"
Find and delete files
Find all javascript files under dist
folder and delete those:
find dist/ -name "*.js" -delete
Find files and run cmd against files
Add -exec
flag and following with commands
.
find images/ -name "*.ng" -exec pngquant {} \
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-02-08 [Functional Programming ADT] Debug a Functional JavaScript composeK Flow
2019-02-08 [Functional Programming ADT] Initialize Redux Application State Using The State ADT
2019-02-08 [Angular] Angular i18n Pluralization Support
2018-02-08 [ReactVR] Render Custom 3D Objects Using the Model Component in React VR
2018-02-08 [ReactVR] Add Lighting Using Light Components in React VR
2018-02-08 [ReactVR] Add Shapes Using 3D Primitives in React VR
2017-02-08 [NPM] Add comments to your npm scripts