6 Replies - 841 Views - Last Post: 10 July 2012 - 06:41 PM Rate Topic: -----

#1 carsor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 31-March 09

I'm learning bash and I need some help

Posted 20 June 2012 - 11:21 AM

I'm trying to create a script that will do the following
A file named file.lst has been created on a linux machine that contains a list of directory names separated by a new line like the following:

dir1
dir2
documents
images
sources

Write a bash script which will iterate through the directory names and create them as subdirectories in /var/www/html/

for file in `find start -type f -print`
do
    echo $file
    fname=`basename $file`
    dir=`dirname $file`
    if [[ $fname =~ "file" ]]
    then
        # move it
        if [[ $dir =~ "dir1" ]]
        then
            mv $file var/www/html/dir1
        elif [[ $dir =~ "dir2" ]]
        then
            mv $file var/www/html/dir2
        else
            echo "unmatched dir $dir"
        fi
    fi
done


I really dont even knw if i'm on the right path.

This post has been edited by GunnerInc: 20 June 2012 - 05:50 PM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: I'm learning bash and I need some help

#2 carsor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 31-March 09

Re: I'm learning bash and I need some help

Posted 20 June 2012 - 04:30 PM

I dont know if this will work i came up with this and I dont know if I'm right. Somebody help
#!/bin/bash
SEARCH_FILE = 'grep -i "directory.lst*"'
for i in $SEARCH_FILE;
do
cat directory1 | xargs cp -t /var/www/html
cat directory2 | xargs cp -t /var/www/html
cat documents | xargs cp -t /var/www/html
cat images | xargs cp -t /var/www/html
cat sources | xargs cp -t /var/www/html

fi
done


this is still not working, can someone walk me to the right direction?

This post has been edited by carsor: 20 June 2012 - 04:37 PM

Was This Post Helpful? 0
  • +
  • -

#3 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: I'm learning bash and I need some help

Posted 20 June 2012 - 04:47 PM

first i would just focus on getting your script to read through the contents of that file and extract each directory name.
Was This Post Helpful? 1
  • +
  • -

#4 carsor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 31-March 09

Re: I'm learning bash and I need some help

Posted 20 June 2012 - 04:51 PM

That is the main problem, how do i get bash to read the contents of that file.lst.
Was This Post Helpful? 0
  • +
  • -

#5 carsor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 31-March 09

Re: I'm learning bash and I need some help

Posted 21 June 2012 - 12:32 AM

I know you can use awk to read the contents of that file but including awk into bash is the main problem i'm getting into
Was This Post Helpful? 0
  • +
  • -

#6 dorknexus  Icon User is offline

  • or something bad...real bad.
  • member icon

Reputation: 1255
  • View blog
  • Posts: 4,618
  • Joined: 02-May 04

Re: I'm learning bash and I need some help

Posted 21 June 2012 - 09:49 AM

A simple way to read line by line in bash would look something like this:

cat myfile.txt | while read line; do
    echo "got line: $line"
done



An alternative but very similar version:
while read line; do        
    echo "got line: $line"
done < myfile.txt 



hope this gets you pointed in the right direction!

EDIT: Also, man is your bestest friend!

man mkdir

This post has been edited by dorknexus: 21 June 2012 - 11:33 AM

Was This Post Helpful? 2
  • +
  • -

#7 ^_^physicist  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-January 09

Re: I'm learning bash and I need some help

Posted 10 July 2012 - 06:41 PM

If you haven't solved the problem yet, you may be able to do your task with this awk command:

 awk '{system("mkdir /var/www/html/") $1}' file_with_directory_names 


This assume your file that lists the directories to be made has the format:

dir1
dir2
dir3
dir4
...

i.e. single column, first column is the directory names.

If the format is in rows you can either do a loop over the fields (columns) or feed the script:

 tr "\n" "\t" file_with_directory_names | awk '{system("mkdir /var/www/html/") $1}' 


Hopefully this solves your problem, if you are trying to build a more complex directory tree, e.g. dir1/dir2/dir3/... this can be adapted, but it isn't immediately obvious.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1