Shell之文件操作
maiaimei 2023/9/23 Shell
统计文件个数
# 统计当前文件夹文件个数
# 首先使用`ls -l`命令列出文件夹中所有文件及其详细信息
# 然后使用`grep ^-`命令筛选出只包含`-`开头的行,即文件的行
# 最后使用`wc -l`命令统计行数,即文件个数
ls -l | grep ^- | wc -l
# 统计指定文件夹文件个数
ls -l [/path/to/folder] | grep ^- | wc -l
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
#!/bin/bash
folder="/path/to/folder"
file_count=$(ls -l $folder | grep ^- | wc -l)
echo "文件夹 $folder 中的文件个数为: $file_count"
1
2
3
4
2
3
4
遍历文件夹下所有文件
#!/bin/bash
folder="/path/to/folder"
totalFiles=$(ls -l $folder | grep ^- | wc -l)
echo "total files: $totalFiles"
files=`ls $folder`
for file in $files
do
filename=`basename $file`
echo "filename: $filename"
echo "name: ${file%.*}"
echo "extension: ${file##*.}"
done
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
遍历文件夹下所有文件,每一分钟拷贝并重命名一个文件到目标文件夹
#!/bin/bash
parallelRunId="PR1"
srcFolder="/path/to/folder/src"
destFolder="/path/to/folder/dest"
logFolder="/path/to/folder/log"
logFile="${logFolder}/$(date +%Y%m%d%H%M%S).log"
cnt=0
totalFiles=$(ls -l $srcFolder | grep ^- | wc -l)
echo "total files: $totalFiles" |tee $logFile
files=`ls $srcFolder`
for file in $files
do
name=${file%.*}
extension=${file##*.}
newFilename="${name}_${parallelRunId}.${extension}"
echo "cp ${srcFolder}/${file} ${destFolder}/${newFilename}" |tee $logFile
cp "${srcFolder}/${file}" "${destFolder}/${newFilename}"
cnt=$(($cnt+1))
if [ $cnt -lt $totalFiles ]
then
sleep 1m
else
echo "copy complete" |tee $logFile
break
fi
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
读取文件夹下所有文件
#!/bin/bash
folder="/path/to/folder"
files=`ls $folder`
for file in $files
do
content=`cat ${folder}/${file}`
echo "File: $file"
echo "Content: $content"
done
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
读取文件夹下所有文件,截取部分内容
# 输出行号同时输出内容
grep -n "要匹配的字符串" 文件名
# 输出行号并不输出内容
# 注意是单引号
awk '/要匹配字符串/{print NR}' 文件名
1
2
3
4
5
6
2
3
4
5
6
#!/bin/bash
declare -A poNumMap=()
srcFolder="/path/to/folder/src"
destFolder="/path/to/folder/dest"
files=`ls $srcFolder`
for file in $files
do
#start=`grep -n "AK1" ${srcFolder}/${file}`
#end=`grep -n "AK9" ${srcFolder}/${file}`
start=$(awk '/AK1/{print NR}' ${srcFolder}/${file})
end=$(awk '/AK9/{print NR}' ${srcFolder}/${file})
#echo "from ${start} to ${end}"
poNum=`grep -m 1 "AK2" ${srcFolder}/${file} | sed 's/AK2\*850\*//g'`
if [ ! -n "${poNumMap[$poNum]}" ]
then
poNumMap[$poNum]=1
else
poNumMap[$poNum]=$((${poNumMap[$poNum]}+1))
fi
sed -n "${start},${end}p" ${srcFolder}/${file} > "${destFolder}/${poNum}_${poNumMap[$poNum]}.txt"
done
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
使用正则删除文件
rm -rf `ls -a|grep -E "_PR1.txt"`
1