Hi
How to create a directory structure with getting input from a file.
I have file in that following lines are written.
./activemq-4.1.2/activemq-core-4.1.2.jar
./activemq-4.1.2/backport-util-concurrent-2.1.jar
./camel-1.4.0/apache-camel-1.4.0.jar
./camel-1.4.0/lib/activation-1.1.jar
./camel-1.4.0/lib/camel-amqp-1.4.0.jar
./camel-1.4.0/lib/camel-atom-1.4.0.jar
./camel-1.4.0/lib/camel-bam-1.4.0.jar
./camel-1.4.0/lib/camel-core-1.4.0.jar
./camel-1.4.0/lib/camel-csv-1.4.0.jar
./camel-1.4.0/lib/camel-cxf-1.4.0.jar
Now how to create that directory structure with empty file name.
Thanks
Joy:confused:
Assuming you want to create the directories:
./activemq-4.1.2/
./activemq-4.1.2/
etc...
sed 's!\(.*/\).*!mkdir -p \1!' file | sh
Regards
Thanks Franklin52 for replying.
Can we do this with awk and cut rather than sed . I am not familiar with sed and its usage.
Thanks
Joy:o
With awk you can something like:
awk -F"/" '{print "mkdir -p "$1"/"$2}' file | sh
Regards
Hi
I have tried similar like this, but one problem is there. it is creating 1 or 2 level folders(but it can be 'n' level).
Thanks
Joy
The input file you post has only one level..!? Never mind, this should work:
awk 'BEGIN{FS=OFS="\"}{$NF="";print "mkdir -p $0}' file | sh
Regards
era
September 23, 2008, 8:31am
7
awk -F/ -v OFS=/ '{ $NF="" }1' | uniq | xargs mkdir -p
Franklin was faster, our solutions are almost identical.
fimblo
September 23, 2008, 3:16pm
8
Another way of doing this would be:
# Assume file with paths to files are in file /tmp/somefiles
for filename in $(cat /tmp/somefiles) ; do
mkdir -p $(dirname $filename)
done
This should work in ksh and bash.
/fimblo
franklin52:
Assuming you want to create the directories:
./activemq-4.1.2/
./activemq-4.1.2/
etc...
sed 's!\(.*/\).*!mkdir -p \1!' file | sh
Regards
Franklin52 can u pls explain the sed command. I have never used this command. I want to learn abt it . Pls help me to understand this.
I got from man pages that it is use to replace strings. But how it is working here.
Thanks in Advance
Joy
era
September 29, 2008, 9:03am
10
It replaces the file name with the part of the file name which comes before the last slash, i.e. the directory part of the name.
Hi Joy,
sed 's!\(.*/\).*!mkdir -p \1!' file | sh
Here in the mentioned code Franklin52 has used ! as a delimiter ( may be thats confusing you).
And the sed body goes like this.
Sed "DELIM" pattern "DELIM" Command "DELIM" input
sed s "DELIM" \(.*/\).* "DELIM" mkdir -p \1 "DELIM" file
And the \(.*/\).* part matches strings between "/"a-z"/" n no of times.
Hope you understood that,
Keep Smiling,
Harsha.
Thanks Harsha for helping me. But I have one doubt here, how \(.*/\).* is equal to "/"a-z"/".
In sed you can save portions of a string with \(.\) and recall them back with \1, \2, \3 etc.
Here within \( and \) we save a portion from the start . of a line until the last /.
Regards