拆分多个文件

时间:2015-05-27 15:47:25

标签: linux bash shell

我有一个包含数百个文件的目录,我必须将它们全部划分为400行文件(或更少)。 我尝试过ls和split,wc和split并制作一些脚本。 其实我迷路了。

拜托,有人可以帮助我吗?

编辑:

感谢John Bollinger和他的answer这是我们将用于我们目的的scritp:

#!/bin/bash

# $# -> all args passed to the script
# The arguments passed in order:
# $1 = num of lines (required)
# $2 = dir origin (optional)
# $3 = dir destination (optional)

if [ $# -gt 0 ]; then
  lin=$1
  if [ $# -gt 1 ]; then
    dirOrg=$2
      if [ $# -gt 2 ]; then
        dirDest=$3
        if [ ! -d "$dirDest" ]; then
          mkdir -p "$dirDest"
        fi
      else
        dirDest=$dirOrg
      fi
  else
    dirOrg=.
    dirDest=.
  fi
else
  echo "Missing parameters: NumLineas [DirectorioOrigen] [DirectorioDestino]"
  exit 1
fi

# The shell glob expands to all the files in the target directory; a different
# glob pattern could be used if you want to restrict splitting to a subset,
# or if you want to include dotfiles.
for file in "$dirOrg"/*; do
  # Details of the split command are up to you.  This one splits each file
  # into pieces named by appending a sequence number to the original file's
  # name.  The original file is left in place.
  fileDest=${file##*/}
  split --lines="$lin" --numeric-suffixes "$file" "$dirDest"/"$fileDest"
done
exit0

1 个答案:

答案 0 :(得分:0)

由于您似乎了解split,并希望将其用于工作,我想您的问题围绕着使用一个脚本来包装整个任务。细节尚不清楚,但这些内容可能是您想要的:

#!/bin/bash

# If an argument is given then it is the name of the directory containing the
# files to split.  Otherwise, the files in the working directory are split.
if [ $# -gt 0 ]; then
  dir=$1
else
  dir=.
fi

# The shell glob expands to all the files in the target directory; a different
# glob pattern could be used if you want to restrict splitting to a subset,
# or if you want to include dotfiles.
for file in "$dir"/*; do
  # Details of the split command are up to you.  This one splits each file
  # into pieces named by appending a sequence number to the original file's
  # name.  The original file is left in place.
  split --lines=400 --numeric-suffixes "$file" "$file"
done