1

I am trying to learn bash scripting and I'm using Ubuntu Linux. I have written a simple Bash file to count the number of files in current directory. I have written the following script in a file:

#! /bin/bash

ls -1 | wc -l  

And saved the file with the name countFile.

But when I am trying to execute the script using ./countFile it is not executing. It shows the following error:

bash: ./countFile: Permission denied  

The countFile is in my home directory so why I haven't the permission. Am I doing something wrong or missing some important thing? Moreover, the ls -1 | wc -l command gives me the correct output when I run it from the terminal.
So how can I run the countFile script?

4
  • 1
    You need permission to execute the file. chmod +x countFile Commented Mar 24, 2015 at 10:59
  • @Biffen Does noone use numbers for permissions anymore ? Commented Mar 24, 2015 at 11:18
  • 1
    @JID It depends on whether you want to set all permissions to something specific or just modify certain ones. Commented Mar 24, 2015 at 11:20
  • Why down votes? Some explanation may help to improve my questions for next time. Is it off-topics?. Commented Mar 24, 2015 at 11:28

2 Answers 2

9

While you are giving like this,

./countfile

You have to make that file as executable using chmod.

chmod +x countfile

Or else you can use the other interpreter like this.

sh countfile
Sign up to request clarification or add additional context in comments.

1 Comment

Or chmod +x so as not to change any of the other permissions.
-3

while executing the file we need a execute permission for that file, we can change the permission or we just run as

. countfile

hew . will represent the current working shell

1 Comment

Not a good way to ‘execute’ a script; it might soil your shell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.