Link to home
Start Free TrialLog in
Avatar of valgrind

asked on

How to append folders with linux shell script implementation


   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.
Avatar of woolmilkporc
Flag of Germany image

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
Avatar of valgrind


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_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?
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?
yes, every directory under a given folder.
not with a common string and only first level.

and   I wonder how can it be done down the whole tree:)
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".
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.
Avatar of woolmilkporc
Flag of Germany image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you, and sorry for ambiguoity.

I will try, and reply.

The script is:

find -type d -maxdepth 1 -print() | xargs -() -i{} cp -rp "cse244directorstr""{}"

I had some errors:

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

"-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"  "{}"
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.
"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!