1

I use SCP a lot to transfer log files from servers to a jumpbox where I can analyse and troubleshoot etc. If I have a cluster of servers and I want to create a set of subdirectories I do it like this:

mkdir -p /foo/bar-nnn/{mailserver,dnsserver,minecraftserver,syslogserver}

Lets's say 'bar-nnn' is a reference of sorts; be that a ticket number or incident etc. What I want to be able to do is run a script or a shell command which will prompt me for what 'bar-nnn' should be then go and create all the subfolders required.

I'm pretty sure I'm going to need a for loop but can't quite get my head around it.

2

Try this:

IFS= read -r -p "Folder name: " dir
mkdir -p "/foo/${dir}/"{mailserver,dnsserver,minecraftserver,syslogserver}
| improve this answer | |
  • Why ${dir} instead of "$dir", or is it the same? – Fiximan Feb 17 '16 at 13:45
  • That's done it! I used this to make it a oneliner: read -p "Folder name: " dir; mkdir -p /foo/${dir}/{mailserver,dnsserver,minecraftserver,syslogserver} – Moif Murphy Feb 17 '16 at 13:45
  • @Fiximan ${dir} and $dir is the same, but ${variable} convention is newer. – Artur Szymczak Feb 17 '16 at 13:47
  • @ArturSzymczak I see, I thought you had included arrays. – Fiximan Feb 17 '16 at 13:51
  • @Fiximan, in this example they are the same, but when ${dir}ectory vs $directory is where the distinction really makes a difference. The former being the value of $dir ending with 'ectory', the latter being the value of $directory. – Devon Feb 17 '16 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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