LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-05-2009, 01:28 PM   #1
incudie
LQ Newbie
 
Registered: Jul 2003
Location: Oxnard/CA/USA
Distribution: Debian
Posts: 12

Rep: Reputation: 0
Create a directory in all sub directories


Hopefully someone knows how to do this because I am not having any luck in finding or figuring out an answer.

I want to create 3 directories in all the sub directories of a folder. Example:

$usera/
$userb/
$userc/

I want to create the directories:
home/
documents/
music/

into each of there folders but in bulk, say I had 200 users and I did not want to have to type the command in for each user.

Thank you!

--Incudie
 
Old 01-05-2009, 01:41 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
You user directories have a '$' at the start of the directory name, or these are variable which hold the names of the users?

Also, it is typical to have a directory called /home which contains a sub-directory for each user on the system. If the user already exists, /home/<username> should already exist. You want to create a sub-directory inside this called "home" (e.g. /home/bob/home)? That seems perverse.

You should make sure that you set the owner of the directories you create so that they are owned by the users which you are creating them for. Unless you do this, the users will likely not be able to use the directories you make.

Here's an example with what I think makes a more sane description of the problem:

Problem: I have three users on my system, "bob", "connie" and "stang". Their home directories are in the usual place (i.e. /home/bob /home/connie and /home/stang).

In each of their home directories, I want to make a sub-directory called "documents", and another called "music". How can I do this so they can use these directories?

Answer. Log in as root and do this (# is the root prompt here):
Code:
# for user in bob connie stang; do 
  mkdir /home/$user/{music,documents}
  chown $user /home/$user/{music,documents}
done
Note that when you create user accounts, you can use the useradd tool with the -m option to copy a "skeleton" user directory for them. See the useradd manual page for more details (search for "skel").
 
Old 01-05-2009, 01:47 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Let's assume that /home contains only directories for each user (the normal situation)

Code:
for user in $(ls /home); do mkdir /home/$user/home; mkdir /home/$user/documents; mkdir /home/$user/music; done
 
Old 01-05-2009, 01:49 PM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I was BSing with a colleague while matthew was typing---his solution is more complete and elegant than mine......
 
Old 01-05-2009, 01:51 PM   #5
incudie
LQ Newbie
 
Registered: Jul 2003
Location: Oxnard/CA/USA
Distribution: Debian
Posts: 12

Original Poster
Rep: Reputation: 0
Hi matthewg42,

Thanks for the reply, the user thing was a bad example on my part, (I thought it would have been easier to understand).

The reality is I have a bunch of folders (one for each of our customers)

customers/customerA
customers/customerB

In each customer folder there are sub directories labeled "Documentation 2002/captures" "Documentation 2003/captures" etc etc.
In the past everyone creates a new folder for each year for each customer.

What I'm trying to figure it out is how to create the following structure in each customer folder

Documentation 2009
--> Captures
--> Projects
--> Archive
--> Diagrams

I did not know I could run commands like that (what you posted above). Is that just bash?

Thanks,
 
Old 01-05-2009, 01:59 PM   #6
incudie
LQ Newbie
 
Registered: Jul 2003
Location: Oxnard/CA/USA
Distribution: Debian
Posts: 12

Original Poster
Rep: Reputation: 0
Ooo Thank you very much you guys!

I got the general idea and was able make the correct command for my needs.

Thanks again!
 
Old 01-05-2009, 02:24 PM   #7
incudie
LQ Newbie
 
Registered: Jul 2003
Location: Oxnard/CA/USA
Distribution: Debian
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
Let's assume that /home contains only directories for each user (the normal situation)

Code:
for user in $(ls /home); do mkdir /home/$user/home; mkdir /home/$user/documents; mkdir /home/$user/music; done
Hey pixellany,

Based on your example, what should be done if the username is spaced such as:

"John Doe"

I have tried quotes on "$user" to no avail.

Thanks
 
Old 01-05-2009, 02:29 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Try this:
for user in $(ls /home); do echo $user; done

What we are looking for is how "John Doe" gets rendered in the list. If you get--eg--:
mike
sam
John
Doe
then the quoting is not going to work.

The best thing in Linux (Unix, and all progeny) is dont have spaces in file/directory names.
 
Old 01-05-2009, 02:36 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Here's a hint:

Code:
[mherring@mystical play]$ touch "the file"
[mherring@mystical play]$ ls
garb  the file  words
[mherring@mystical play]$ for fil in $(ls); do echo $fil;done
garb
the
file
words
[mherring@mystical play]$ for fil in $(ls|sed 's/ /_/'); do echo $fil;done
garb
the_file
words
You can use a loop like this to rename anything that has a space in it, but if you are dealing with directories having the same name as users, that's not going to be a good thing.

In your code, you can simply use SED again to change (from my example) "the_file" back to "the file".
 
Old 01-05-2009, 03:09 PM   #10
incudie
LQ Newbie
 
Registered: Jul 2003
Location: Oxnard/CA/USA
Distribution: Debian
Posts: 12

Original Poster
Rep: Reputation: 0
Awesome thanks for the explanation.

I am playing around with adding the inverted sed statement.

Is it possible to do something like:

$for fil in $(ls|sed 's/ /_/'); do mkdir $fil|sed 's/_/ /')/{Captures,Archive}; done

Hope is to change it from "the file" to "the_file" then back to "the file"
 
Old 01-05-2009, 06:00 PM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Have you tried it?

The one thing that jumps out at me is that a pipe (|) requires that the two statements are designed to produce / receive data. For example: "mkdir $fil" does not produce an output that can be piped to anything.

How about:
mkdir $(echo $fil|sed 's/_/ /')

When building something like this, always test the pieces as you go.
 
Old 01-05-2009, 06:50 PM   #12
incudie
LQ Newbie
 
Registered: Jul 2003
Location: Oxnard/CA/USA
Distribution: Debian
Posts: 12

Original Poster
Rep: Reputation: 0
Hey Pixellany,

Thank you again, I understand what you mean and was able to do that as well.

I found a reference on google and did something similar and was able to accomplish what I needed.

Here is the script:

Code:
#!/bin/bash
SAVEIFS=$IFS
IFS=$(ls /media/smb/Customer)
for f in *
do
mkdir /media/smb/Customer/"$f"/"Documentation 2006"
mkdir /media/smb/Customer/"$f"/"Documentation 2006"/Captures
mkdir /media/smb/Customer/"$f"/"Documentation 2006"/Projects
mkdir /media/smb/Customer/"$f"/"Documentation 2006"/Archive
mkdir /media/smb/Customer/"$f"/"Documentation 2006"/Diagrams
mkdir /media/smb/Customer/"$f"/"Documentation 2007"
mkdir /media/smb/Customer/"$f"/"Documentation 2007"/Captures
mkdir /media/smb/Customer/"$f"/"Documentation 2007"/Projects
mkdir /media/smb/Customer/"$f"/"Documentation 2007"/Archive
mkdir /media/smb/Customer/"$f"/"Documentation 2007"/Diagrams
mkdir /media/smb/Customer/"$f"/"Documentation 2008"
mkdir /media/smb/Customer/"$f"/"Documentation 2008"/Captures
mkdir /media/smb/Customer/"$f"/"Documentation 2008"/Projects
mkdir /media/smb/Customer/"$f"/"Documentation 2008"/Archive
mkdir /media/smb/Customer/"$f"/"Documentation 2008"/Diagrams
done
IFS=$SAFEIFS
Thank you again!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying files and sub-directories of a directory except the directories named ".abc" sri1025 Linux - General 2 08-24-2010 08:53 AM
Count the number of files in a directory and sub-directories within that directory soumyajit.haldar Linux - Software 4 03-20-2007 06:22 AM
Samba: "homes" share, cannot create directories, can create files Herg Linux - Software 1 09-14-2006 08:48 AM
Bash script to strip a certain directory out of directories in a directory? rylan76 Linux - General 3 08-29-2006 11:35 AM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration