Shell script to build directory tree and files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to build directory tree and files
# 1  
Old 04-13-2013
Shell script to build directory tree and files

Hi all,

I'm trying at the moment to write a shell script to build a directory tree and create files within the built directories. I've scoured through sites and text books and I just can't figure out how to go about it.

I would assume that I need to use loops of some sort, but I can't seem to get it.

For example, I would have parent directory I create called "mydir" then in my dir I have 2 subdirectories "Work" and "Games". Then in "Games" I would have text files "game1.txt and game2.txt".

Any help would be greatly appreciated. Thanks!
# 2  
Old 04-13-2013
Try looking at the man pages for the mkdir and touch utilities.
# 3  
Old 04-13-2013
You might not need any loops.

At a minimum, you could do something using the mentioned mkdir and touch commands. Put them in a shell script, such as mkdirs.sh, like:
Code:
mkdir /home/mydir/
mkdir /home/mydir/games/
mkdir /home/mydir/work/

touch /home/mydir/games/game1.txt

Then run "chmod 755 mkdirs.sh" to set permissions. Then run "./mkdirs.sh" to run the script.
This User Gave Thanks to hanson44 For This Post:
# 4  
Old 04-14-2013
How would I additionally get the shell script to create a java file with a basic "Hello World" output?

---------- Post updated at 11:19 PM ---------- Previous update was at 10:29 PM ----------

I tried doing:

touch helloworld.java
echo "public class Helloworld { public static void main (String args []) { System.out.println(\"Hello World\"); }} >> Helloworld.java

But when I do javac on it, I get no output. Instead it tells me "Could not find the main class: Helloworld.java. Program will exit"

Last edited by Libertad; 04-14-2013 at 01:45 AM..
# 5  
Old 04-14-2013
Quote:
Originally Posted by Libertad
How would I additionally get the shell script to create a java file with a basic "Hello World" output?

---------- Post updated at 11:19 PM ---------- Previous update was at 10:29 PM ----------

I tried doing:

touch helloworld.java
echo "public class helloworld { public static void main (String args []) { System.out.println("Hello World"); }} >> helloworld.java

But when I do javac on it, I get no output.
You don't get any output because you have mismatched double-quotes on the above echo command. Did you get a command prompt before you entered the javac command? Or, is it still waiting for you to complete the string to be echoed? Do you understand that javac doesn't run your java program; it only compiles it?

Why do I get the strong impression that you're asking us to complete a homework assignment for you?

Last edited by Don Cragun; 04-14-2013 at 01:49 AM.. Reason: fix typo
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 04-14-2013
I'm simply trying to learn the basics of unix

I made a typo in my above code. I actually do have the matching double-quotes. I also realized that javac only compiles it and that I do actually need to run the java file.

It tells me that it cannot find the main class, which I have looked up and from my understanding, it cannot find jdk and so it is giving me that error?
# 7  
Old 04-14-2013
Quote:
Originally Posted by Libertad
I'm simply trying to learn the basics of unix

I made a typo in my above code. I actually do have the matching double-quotes. I also realized that javac only compiles it and that I do actually need to run the java file.

It tells me that it cannot find the main class, which I have looked up and from my understanding, it cannot find jdk and so it is giving me that error?
If you didn't have a jdk you would have gotten a diagnostic from you shell saying something like shellname: javac not found. The diagnostic you got sounds more like you tried to run the program using:
Code:
java *.class

Did you try:
Code:
ls -l

after you ran:
Code:
javac *.java

?

Did you find a file named helloworld.class? Did you try running the command java helloworld?

If you don't show us the ACTUAL commands that you run and the exact messages that are printed, any help we may try to give can only be wild guesses.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete all files with specific extension in directory tree

I'm sure this has been asked many times, but a search didn't turn up a definitive best method for this (if there ever is such a thing). I have been using rsync to back up my main data directory, but I have accumulated a large number of older backups that I don't need. All of the files I don't... (14 Replies)
Discussion started by: LMHmedchem
14 Replies

2. Shell Programming and Scripting

Simple directory tree diff script

I have had some issues with a data drive and have copied all of the data to a new drive. The size used is not the same on both drives with a 3GB difference (less on the new drive). There are millions of files on the data drive, so it is not an easy task to determine if there are some files missing... (17 Replies)
Discussion started by: LMHmedchem
17 Replies

3. Shell Programming and Scripting

How to find files in directory tree by date

I'm using a directory naming convention to organize files as exemplified here: 2012/Aug/week-20-Aug/23-Thu/tuv.txt 2012/Aug/week-27-Aug/30-Thu/abc.txt 2012/Sep/week-27-Aug/01-Sat/def.txt 2012/Sep/week-03-Sep/07-Fri/xyz.txt How do I write a command that will list the file names abc.txt and... (4 Replies)
Discussion started by: siegfried
4 Replies

4. Shell Programming and Scripting

remove a whole directory tree WITH files inside?

Assume I want to remove a whole directory tree beginning with /foo/bar/ The directory or sub-directories may contain files. The top directory /foo/bar/ itself should not be deleted. rm -f- r /foo/bar does not work because it requires a directory tree without files. How does it work... (3 Replies)
Discussion started by: pstein
3 Replies

5. Shell Programming and Scripting

Need to build Shell Script to search content of a text file into a folder consist several files

Have to read one file say sourcefile containing several words and having another folder containing several files. Now read the first word of Sourcefile & search it into the folder consisting sevral files, and create another file with result. We hhave to pick the filename of the file in which... (3 Replies)
Discussion started by: mukesh.baranwal
3 Replies

6. Shell Programming and Scripting

How we can create the master file through shell to show the tree structure of the directory?

Can we create the master file that show the whole tree structure of the directory till a particular folder? Database that contains four sub repository Sybase,sql,oracle,mysql and sql and oracle contains two subrepostories Siebel and plsql and each repositories contains three folders... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

7. UNIX for Dummies Questions & Answers

Copy directory tree with files

Iam in the process of copying a directory with thousands of directories and files into a new directory. I need to preserve permissions, owner, group, date and timestamps, everything. Iam using AIX and would need help of writing the command whether it is cp-RP or cpio. Apprecaite your... (3 Replies)
Discussion started by: baanprog
3 Replies

8. UNIX for Dummies Questions & Answers

Move all files in a directory tree to a signal directory?

Is this possible? Let me know If I need specify further on what I am trying to do- I just want to spare you the boring details of my personal file management. Thanks in advance- Brian- (2 Replies)
Discussion started by: briandanielz
2 Replies

9. Shell Programming and Scripting

Recursively copy only specific files from a directory tree

Hi I am a shell-script newbie and am looking to synchronize certain files in two directory structures. Both these directory-trees are in CVS and so I dont want the CVS directory to be copied over. I want only .sh and .pl files in each subdirectory under these directory trees to be... (3 Replies)
Discussion started by: sharpsharkrocks
3 Replies

10. Shell Programming and Scripting

Read from fileList.txt, copy files from directory tree

Hi, hopefully this is a fairly simple Q&A. I have a clean file list of approximately 180 filenames with no directory or slashes in front of the filename nor any extension or dot ".". I would like to read from this list, find these files recursively down through directory trees, copy the files... (1 Reply)
Discussion started by: fxvisions
1 Replies
Login or Register to Ask a Question