Solved

How to append  folders with linux shell script implementation

Posted on 2013-05-11
13
524 Views
Last Modified: 2013-05-12
Hello,

   I have a lot of folders, lets say them directories. I want to add  a folder into those directories using shell script. How can I do? Thanks.
0
Comment
Question by:valgrind
  • 7
  • 6
13 Comments
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39158617
Do you mean all first level directories below a given root directory?

If so, use this:

cd /root/dir

ls -l |grep ^d |awk '{print "mkdir", $NF "/newdir"}' | sh

Or do you have the directory names in a file, let's say "dirlist"?

Then use this:

xargs -I{} mkdir "{}/newdir" < dirlist
0
 

Author Comment

by:valgrind
ID: 39158648
I am unfamiliar with shell script, but I suppose it must use for loop,Am I wrong? Let me explain my aim giving an example:

 I have  many folders with various names.:
/<directory1_with_name1>
/<directory1_with_name2>
....
/<  directory1_with_nameN >

I want to add a specific folder, which is named "myfolder" , into each of those folders.
I hope it makes sense.
How can I do it?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39158660
There are implicit loops in my commands.

The first one prints out the mkdir command for every directory found and pipes it to sh for execution, and xargs in the second suggestion runs an implicit loop over the lines contained in the input file.

Your example doesn't specify a criterion which directories to process.

Every directory under a given folder? Down the whole tree or only the first level? Every directory starting with a common string?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:valgrind
ID: 39158698
yes, every directory under a given folder.
0
 

Author Comment

by:valgrind
ID: 39158703
not with a common string and only first level.

and   I wonder how can it be done down the whole tree:)
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39158712
OK, to take into account that there might be spaces in directory names let's use "find" and "xargs",
Both have an option to use binary zeroes as field separators ("-print0" resp. "-0") instead of spaces:

find /given/folder -type d -maxdepth 1 -print0 | xargs -0 -i{} echo mkdir "{}/myfolder"

And don't worry, there is a (yet not visible) loop implicitly constructed by "xargs".

To process the whole tree just omit the "-maxdepth 1" option of "find".
0
 

Author Comment

by:valgrind
ID: 39158719
I think this is doing new directory, and adding it to the folders?
My aim is copying a folder and its contents into to all those folders.
I am doing another work now, so I can not try your solution, sorry for that.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39158744
You wrote "add a folder" - that's ambiguous (to say the least).

Anyway, only a slight enhancement needed:

find /given/folder -type d -maxdepth 1 -print0 | xargs -0 -i{} cp -rp "/path/to/source/myfolder" "{}"
0
 

Author Comment

by:valgrind
ID: 39158775
Thank you, and sorry for ambiguoity.

I will try, and reply.
0
 

Author Comment

by:valgrind
ID: 39158817
Hello,

The script is:

#!/bin/bash
find -type d -maxdepth 1 -print() | xargs -() -i{} cp -rp "cse244directorstr""{}"

I had some errors:

egemen@egemen-VirtualBox:~/cse244gruplar/currentdir$ ./myscript.sh
./myscript.sh: line 2: syntax error near unexpected token `('
./myscript.sh: line 2: `find -type d -maxdepth 1 -print() | xargs -() -i{} cp -rp "cse244directorstr""{}"'
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39158824
It's

"-print0" and "xargs -0"

where in both cases "0" means the digit "zero"!

And in the below line there must be a space before "{}"! Very important!

... "cse244directorstr"    "{}"

Further, you must specify the start directory for "find",
and shouldn't you specify the full path to the directory to be copied to the target folders?

find /start/dir-type d -maxdepth 1 -print0 | xargs -0 -i{} cp -rp "/path/to/cse244directorstr"  "{}"
0
 

Author Comment

by:valgrind
ID: 39158872
I thought  start directory       or/and   "path/to......" can be evaluated as current directory if I omit relevant expressions. I was wrong. If these two were current directory, it would be nice.

But somehow it is usefull for me . I separated folders and "myfolder" so that they are not in current directory together.

Thank you.
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 39159223
"find" requires a starting directory, there's no default. To specify the current directory use the dot ".":

find . -type d ... ...

In the second case you're right - without a path specification the current directory will be searched.

Thx for the points!

wmp
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Access_log 17 148
Like to learn Linux, need advice for a good book and setup. 3 55
how to print sub line with  sed /awk /perl in bash file 11 48
Samba Question 11 75
Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question