Bash Script to Mkdir on Each Line of a File That Has Been Split by a Delimiter

How do I split a string on a delimiter in Bash?

You can set the internal field separator (IFS) variable, and then let it parse into an array. When this happens in a command, then the assignment to IFS only takes place to that single command's environment (to read ). It then parses the input according to the IFS variable value into an array, which we can then iterate over.

This example will parse one line of items separated by ;, pushing it into an array:

IFS=';' read -ra ADDR <<< "$IN"
for i in "${ADDR[@]}"; do
# process "$i"
done

This other example is for processing the whole content of $IN, each time one line of input separated by ;:

while IFS=';' read -ra ADDR; do
for i in "${ADDR[@]}"; do
# process "$i"
done
done <<< "$IN"

Split a folder into multiple subfolders in terminal/bash script

This solution can handle names with whitespace and wildcards and can be easily extended to support less straightforward tree structures. It will look for files in all direct subdirectories of the working directory and sort them into new subdirectories of those. New directories will be named 0, 1, etc.:

#!/bin/bash

maxfilesperdir=20

# loop through all top level directories:
while IFS= read -r -d $'\0' topleveldir
do
# enter top level subdirectory:
cd "$topleveldir"

declare -i filecount=0 # number of moved files per dir
declare -i dircount=0 # number of subdirs created per top level dir

# loop through all files in that directory and below
while IFS= read -r -d $'\0' filename
do
# whenever file counter is 0, make a new dir:
if [ "$filecount" -eq 0 ]
then
mkdir "$dircount"
fi

# move the file into the current dir:
mv "$filename" "${dircount}/"
filecount+=1

# whenever our file counter reaches its maximum, reset it, and
# increase dir counter:
if [ "$filecount" -ge "$maxfilesperdir" ]
then
dircount+=1
filecount=0
fi
done < <(find -type f -print0)

# go back to top level:
cd ..
done < <(find -mindepth 1 -maxdepth 1 -type d -print0)

The find -print0/read combination with process substitution has been stolen from another question.

It should be noted that simple globbing can handle all kinds of strange directory and file names as well. It is however not easily extensible for multiple levels of directories.

How to delete from a text file, all lines that contain a specific string?

To remove the line and print the output to standard out:

sed '/pattern to match/d' ./infile

To directly modify the file – does not work with BSD sed:

sed -i '/pattern to match/d' ./infile

Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:

sed -i '' '/pattern to match/d' ./infile

To directly modify the file (and create a backup) – works with BSD and GNU sed:

sed -i.bak '/pattern to match/d' ./infile

mkdir' in a shell file cannot create directory: Permission denied

You have to give the script execution permission:

chmod +x path_to_the_copy.sh

Bash script to list files periodically

You can try

ls -1 | awk '
{
if (! ((NR-1)%4000)) {
if (j) close(fnn)
fn=sprintf("folder%02d",++j)
system("mkdir "fn)
fnn=fn"/file.txt"
}
print >> fnn
}'

Explanation:

  • NR is the current record number in awk, that is: the current line number.
  • NR starts at 1, on the first line, so we subtract 1 such that the if statement is true for the first line
  • system calls an operating system function from within awk
  • print in itself prints the current line to standard output, we can redirect (and append) the output to the file using >>
  • All uninitialized variables in awk will have a zero value, so we do not need to say j=0 in the beginning of the program

How to split a file-path in shell script? As per my need

Maybe have a look at the -p option to mkdir - it creates intervening directories automagically...

mkdir -p directory1/src/main/java


Related Topics



Leave a reply



Submit