User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,440 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,616 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 771 | Replies: 2 | Solved
Reply
Join Date: Nov 2007
Posts: 4
Reputation: iggy78 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
iggy78 iggy78 is offline Offline
Newbie Poster

Help Need Help! Could someone please check the two tiny bash scripts to see if they work?

  #1  
Dec 2nd, 2007
#!/bin/bash
# Bash script that will produce a text file that has a list of the files in the present working directory
cd /bin
ls -d
ls -d >>file.txt


#!/bin/bash
#Bash script that will copy all the files in one directory to another directory
ls -d
mkdir /EgorDir
cp*.*ls-d /EgorDir
echo "Done"
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Frederick, MD
Posts: 4
Reputation: t_rectenwald is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
t_rectenwald's Avatar
t_rectenwald t_rectenwald is offline Offline
Newbie Poster

Re: Need Help! Could someone please check the two tiny bash scripts to see if they work?

  #2  
Dec 2nd, 2007
Hello,

For the first script, you do not want to use 'ls -d'. Please see the man page for ls (man ls). The -d flag to ls will list a directory itself, without recursing underneath it. With the -d flag, you actually need to pass the directory, or directories as arguments.

In order to get a listing of the files within a directory, just execute 'ls /directory_name' without any arguments at all. Some you may want to consider though are '-a' which shows hidden files, or '-l' which produces more detailed information for the files.

The redirection for the script, >>, will append data to the bottom of whatever file is specified. Chances are you would rather overwrite anything in the current file, if it exists. You can do this by just using >. So, the first script should look something like this:

#!/bin/bash
cd /bin
ls > /tmp/file.txt

Note, adding "/tmp" (or any directory that is writeable to you) is a good idea. Otherwise, your script will try to write the output file to the /bin directory itself, which you should not have write access to, and will thus fail.

For the second script, cd and ls should not be on the same line. To copy files from one directory to another in shell, you would want to use something like:

#!/bin/bash
cp -R /tmp/directory_1 /tmp/directory_2

The -R means "recursive," i.e. copy everything under that directory (including subdirectories) to the new directory. I hope some of this helps.

Regards,
Tom
Reply With Quote  
Join Date: Nov 2007
Posts: 4
Reputation: iggy78 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
iggy78 iggy78 is offline Offline
Newbie Poster

Re: Need Help! Could someone please check the two tiny bash scripts to see if they work?

  #3  
Dec 2nd, 2007
Tom,
Thank You so much. As You can see I'm a complete novice to scripting, so Your help is very much appreciated.
Thx,
Egor
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 1:42 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC