我想将包含100行或更多行的文件复制到另一个文件夹:
$ cd "/home/john/folder a"
$ wc -l *
10 file1.txt
50 file2.txt
100 file3.txt
150 file4.txt
我想将文件file3.txt
和file4.txt
(包含100行或更多行的文件)复制到文件夹/home/john/folder b
。
答案 0 :(得分:2)
试试这个:
declare -i numfile
for f in *; do
numfile=$([ -f "$f" ] && cat "$f" | wc -l )
[ $numfile -ge 100 ] && cp "$f" otherdir
done
对于当前目录中的每个文件,numfile
被分配了文件的行数。
如果numfile
大于或等于100,则会将文件复制到otherdir
。
修改:
正如William Pursell所提到的,更强大的方法是在执行比较和复制命令之前测试该项目是否为文件:
for f in *; do
if [ -f "$f" ]; then
[ "$(wc -l < $f)" -ge 100 ] && cp "$f" otherdir;
fi
done
答案 1 :(得分:1)
还有一个:
# Assuming that we are in source folder ...
cp $(wc -l *|grep -Eo '[0-9]{3,} (.*)'|head -n -1|cut -d ' ' -f 2) /dev/null "/home/john/folder b"
head
删除wc
打印的总计行,而/dev/null
会处理您没有任何文件的情况符合标准。
当然,这个解决方案 - 就像这里介绍的其他解决方案一样 - 会让你遇到问题,如果你的源目录包含那么多文件,那么将超出最大命令行长度。
答案 2 :(得分:0)
尝试这样的事情(POSIX sh):
#!/bin/sh
SOURCE_FOLDER="/home/john/folder a"
COPY_TO="/home/john/folder b"
for dir in "$SOURCE_FOLDER" "$COPY_TO"; do
if [ ! -d "$dir" ]; then
echo "Directory ${dir} does not exist." >&2
exit 1
fi
done
if [ "x`ls -A "$SOURCE_FOLDER"`" = "x" ]; then
echo "Directory '${SOURCE_FOLDER}' is empty." >&2
exit 1
fi
for file in "$SOURCE_FOLDER"/*; do
LINES=`wc -l < "$file"`
echo "File ${file} has ${LINES} lines..."
if [ "$LINES" -ge 100 ]; then
echo "Copying ${file}..."
cp -a "$file" "${COPY_TO}/"
fi
done
这里是Bash版本的Bash版本(你说的是Unix,而不是Linux,所以你可能想要顶级版本):
#!/bin/bash
SOURCE_FOLDER="/home/john/folder a"
COPY_TO="/home/john/folder b"
for dir in "$SOURCE_FOLDER" "$COPY_TO"; do
if [[ ! -d "$dir" ]]; then
echo "Directory ${dir} does not exist." >&2
exit 1
fi
done
if [[ -z "$(ls -A "$SOURCE_FOLDER")" ]]; then
echo "Directory '${SOURCE_FOLDER}' is empty." >&2
exit 1
fi
for file in "$SOURCE_FOLDER"/*; do
LINES="$(wc -l < "$file"')"
echo "File ${file} has ${LINES} lines..."
if [[ "$LINES" -ge 100 ]]; then
echo "Copying ${file}..."
cp -a "$file" "${COPY_TO}/"
fi
done
我测试了这样:
$ mkdir "folder a"
$ mkdir "folder b"
$ chmod +x script.sh
$ cd folder\ a/
$ seq 1 1000 > file1.txt
$ seq 1 1000 > file2.txt
$ seq 1 100 > file4.txt
$ seq 1 100 > file3.txt
$ seq 1 99 > file4.txt
$ seq 1 1 > file5.txt
$ seq 1 20 > file6.txt
$ cd ..
$ ./script.sh
File /.../dev/scratch/stack/folder a/file1.txt has 1000 lines...
Copying /.../dev/scratch/stack/folder a/file1.txt...
File /.../dev/scratch/stack/folder a/file2.txt has 1000 lines...
Copying /.../dev/scratch/stack/folder a/file2.txt...
File /.../dev/scratch/stack/folder a/file3.txt has 100 lines...
Copying /.../dev/scratch/stack/folder a/file3.txt...
File /.../dev/scratch/stack/folder a/file4.txt has 99 lines...
File /.../dev/scratch/stack/folder a/file5.txt has 1 lines...
File /.../dev/scratch/stack/folder a/file6.txt has 20 lines...
我鼓励您逐行逐步完成,弄清楚脚本的每个部分是如何工作的,以帮助您将来完成类似的任务。
我将在这里解释一些事情:
wc -l < "$file"
仅向我们提供文件的行数,不包含文件名。[ "$LINES" -ge 100 ]
为真。echo "..." >&2
输出一行到标准错误而不是标准输出。cp -a
复制文件,同时保留所有属性,例如所有者,权限和修改时间。