linux学习(二)文件 、查找
一、文件操作
1.1 创建文件和目录(指定目录和不指定目录的区别)
cd # 进入操作
mkdir /tmp/test0 # 创建空文件夹
mkdir -p /data1/hadoop/hdfs/name # 递归创建文件夹
touch /tmp/tets0/file.sh # 创建空文件
vi file.log # 创建空文件并进入编辑模式
echo “hello world” >file.sh # 创建file.sh并输入hello world到该文件中
1.2 列出文件和目录列表
ls -a -h-l
ll -h(ls -l)
1.3 文本编辑器
vim/vi/nano file
:q 退出
:w 保存
:wq 保存并退出
:q! 强制退出
1.4 文件内容查看
cat -n # 查看内容时显示行号
cat /etc/var/boot.log # 不显示行号
cat -n /etc/passwd # 查看内容时显示行号
1.5 显示文件的头10行或尾10行
tail # 默认查看文件尾10行(tail -3 查看末尾3行)
head # 默认查看文件头10行(head -3 查看头3行)
tail -f # 动态监控文件数据
more # 百分比查看
less # 能往回滚看
vi
1.6 复制文件
# (将当前目录下的tom目录下的123文件复制到当前目录下的test目录)
cp ./tom/123 ./test
1.7复制目录
# (将当前目录下的 tom目录递归(包括tom目录下的所有文件)复制到当前目录下的test目录里)
cp -r ./tom/ ./test
# 批量命名
rename
1.8移动、剪切、重命名文件或目录
# (将当前目录下的test目录下的test5和abc剪切到当前目录下的test1目录)
mv ./test/test5 ./test/abc ./test1
# (将当前目录下的test目录下的def剪切到当前目录下的test1目录并重命名为ddd)
mv ./test/def ./test1/ddd
1.9重定向和追加 > >>
- “>” 先清空文件内容,后写入新的内容
ls -l /root > /tmp/file1.txt- “>>” 追加新的内容,旧的内容不会消除
ls -l /root >> /tmp/filr.txt
1.10屏幕打印
echo “hello” (打印hello到屏幕上)
echo “hello” > ./tom/123.txt(把hello重定向到123.txt)
echo “hello” >>./tom/123.txt (打印追加到123.txt)
1.11删除
rm # 删除文件
rm -r # 删除目录
rm -f # 强制删除