linux 第三章:文件操作

1、生产任意大小的文件
[root@localhost dd_test]#
[root@localhost dd_test]# dd if=/dev/zero of=junk.data bs=1k count=10
10+0 records in
10+0 records out
10240 bytes (10 kB) copied, 0.00137023 s, 7.5 MB/s

2、文件系统相关测试
[ -f $file_var ]: 给定的变量包含正常的文件路径或文件名,则返回真
[ -d $var ]: 给定的变量是目录,则返回真。
[ -e $var ]: 给定的变量包含的文件存在,则返回真。
[ [ -z $str1 ]]: 如果str1包含的是空字符串,则返回真。
[ [ -n $str1 ]]: 如果str1包含的是非空字符串,则返回真。
-gt: 大于
-lt: 小于
-ge: 大于或等于.
-le: 小于或等于.

3、文件权限
[root@localhost program_test]# chmod 777 cnts.sh

4、批量生成任意大小的文件
[root@localhost touch_more]# cat create_morefile.sh
#!/bin/bash
for name in {1..100}.txt
do
touch $name
dd if=/dev/zero of=$name bs=1k count=1
done

5、生成符号链接文件
[root@localhost touch_more]# ln -s 100.txt 100_symbol.txt
[root@localhost touch_more]# ll -al 100*
lrwxrwxrwx. 1 root root    7 Jan  2 00:24 100_symbol.txt -> 100.txt
-rw-r--r--. 1 root root 1024 Jan  2 00:22 100.txt

查找符号链接的文件
方法一:
[root@localhost touch_more]# ls -al | grep '^l' | awk '{print $9}'   //特征标记,以l开头。
100_symbol.txt
方法二:
[root@localhost touch_more]# find ./ -type l
./100_symbol.txt

打印符号链接指向文件的名称:
[root@localhost touch_more]# ls -al 100_symbol.txt | awk '{ print $11 }'
100.txt

6、遍历文件,分类型统计文件
[root@localhost touch_more]# cat filestat.sh
#!/bin/bash

if [ $# -ne 1 ];
then
        echo $0 basepath;
        exit 1
fi
path=$1

declare -A statarray;

while read line;
do
        ftype=$(file -b "$line")
        let statarray["$ftype"]++;
done < <(find $path -type f -print)  //以子进程统计文件名

echo ===================FILE types and counts ===============

for ftype in "${!statarray[@]}"; //数组表
do
        echo $ftype : ${statarray["$ftype"]}
done

6、实时观看不断增长的文件
[root@localhost touch_more]# tail -f filestat.sh

7、目录切换
[root@localhost program_test]# cd -
/home/yxx/program_test/touch_more
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值