•
•
•
•
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
![]() |
| |
•
•
Join Date: Nov 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Need Help! Could someone please check the two tiny bash scripts to see if they work?
#1
Dec 2nd, 2007
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:
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:
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
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
•
•
Join Date: Nov 2007
Posts: 4
Reputation:
Rep Power: 0
Solved Threads: 0
Re: Need Help! Could someone please check the two tiny bash scripts to see if they work?
#3
Dec 2nd, 2007
![]() |
•
•
•
•
•
•
•
•
DaniWeb Shell Scripting Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Previous Thread: Starting, Stopping, Updating Shell Script
- Next Thread: BASH script - sales per each associate (3 files)


Hybrid Mode