miguel_rossi
miguel_rossi

Reputation: 53

Split multiple files

I have a directory with hundreds of files and I have to divide all of them in 400 lines files (or less). I have tried ls and split, wc and split and to make some scripts. Actually I'm lost.

Please, can anybody help me?

EDIT:

Thanks to John Bollinger and his answer this is the scritp we will use to our purpose:

#!/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

Upvotes: 0

Views: 2772

Answers (1)

John Bollinger
John Bollinger

Reputation: 180171

Since you seem to know about split, and to want to use it for the job, I guess your issue revolves around using one script to wrap the whole task. The details are unclear, but something along these lines is probably what you want:

#!/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

Upvotes: 2

Related Questions