Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to write a bash shell script that will read the data from a given (as an argument) file, and for each row in the file, make a sub-directory under the given (as an argument) directory.

The script is named studentDir and the input file is named studentInfo.txt.
Here is two lines from it:

Sara Smith Freshman Marketing
James Lucas Junior Engineering

On the command line the user would write the script name, the txt file name, and the directory where the new directories should be stored. I think it would be like this:

$ studentDir studentInfo.txt .

Which will create the sub-directories in the current directory.

The directories should be named LLLLFF.
For example:


(The other info from the input file is not needed)

I think to get the names I would use:

DIRNAME=$(cut -d' ' -f2 | cut -c1-4 ; cut -d' ' -f1 | cut -c1-2) 
mkdir "$DIRNAME" 

But I'm not sure what command would create a directory for each row, and how I could do it recursively for each one.

share|improve this question
Is your question how to iterate over each line in the file, or how to use the output from your cut command as the directory name? P.S. Recursion is rarely the answer (and in this case it is indeed not). Recursion is evil. Stay away from recursion :-) –  Patrick Apr 21 at 4:17
Hmm the question is how to use the output from the cut command as the directory name. I just need to make sure it does it for every line of the text file –  Lacey Apr 21 at 4:47
Do none of your users have a middle name? –  terdon Apr 21 at 11:07

3 Answers 3

awk solution

awk '{print substr($2,1,4) substr($1,1,2)}' file

gives you the directory names.

mkdir $(awk '{print substr($2,1,4) substr($1,1,2)}' file)

creates the directories (if they are not too many). For huge numbers (10,000+) you can use:

awk '{print substr($2,1,4) substr($1,1,2)}' file | xargs --delimiter=\\n mkdir

bash solution

awk is not necessary, the shell can do that itself:

while read first last rest; do mkdir -p "${last:0:4}${first:0:2}"; done <file
share|improve this answer
I think I follow. I've never used awk though. Is there a way to do it in bash? And I can replace 'file' with $1 I believe? Since it would be the first command-line parameter? –  Lacey Apr 21 at 4:35
@JackieLawrence file is the name of the file awk is supposed to read. –  Hauke Laging Apr 21 at 4:42
Using the While commands, couldn't I replace 'echo' with mkdir? So it would create the directory with that name rather than outputting. and for the <file , does the name need to be specified within the script, or will it reference the file that the user inputs? Thats what I meant with the $1 earlier. The user can input the file that they want to use this script with. –  Lacey Apr 21 at 5:01
@JackieLawrence echo was for my testing only, forgot to change that. file is a literal name. You can replace that by e.g. "$1" but I would always recommend to use something like "$file" instead in case the parameter definition of the script changes. –  Hauke Laging Apr 21 at 5:24

You could use perl. Saying:

perl -lanE 'mkdir substr(@F[1],0,4) . substr(@F[0],0,2)' studentInfo.txt

would create the desired directories.

share|improve this answer
I haven't learned to use perl yet either. Is there a way to do it in bash? –  Lacey Apr 21 at 4:49

I believe Hauke Laging's bash solution is the most appropriate here and deserves to stand on it's own so that votes can be correctly attributed to it.

Thererfore, I'm reproducing it here.



set -o nounset  # don't allow implicit vars
set -o errexit  # exit on first error

file=${1:-studentInfo.txt}  # default file var if not specified

while read first last rest; do
  mkdir -p "${last:0:4}${first:0:2}";
done < $file


$ chmod u+x studentDir
$ ./studentDir newStudentInfo.txt
share|improve this answer
It already does stand on its own, in Hauke's answer. This answer does not add anything new. –  terdon Apr 21 at 11:07
I wanted to separate it from the awk solution which I didn't really like and hence didn't want to vote for. –  dexterous Jul 15 at 12:43
Please don't do this. Repeating information already present in another answer is at best useless, and at worst simple plagiarism and rep-whoring. Votes apply for the entire answer, if an answer gives multiple solutions and you prefer one, you still vote for the answer precisely because it contains the one you like. –  terdon Jul 15 at 13:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.