0

I'm writing a script that will print the file names of every file in a subdirectory of my home directory. My code is:

foreach file (`~/.garbage`)
     echo "$file"
end

When I try to run my script, I get the following error:

home/.garbage: Permission denied.

I've tried setting permissions to 755 for the .garbage directory and my script, but I can't get over this error. Is there something I'm doing incorrectly? It's a tcsh script.

user1754045
  • 175
  • 1
  • 6
  • 15
  • 1
    the backticks are trying to invoke the program `~/.garbage`. You probably don't want to do that – Anya Shenanigans Mar 04 '13 at 10:50
  • I removed the ticks and it worked. However now I have another issue. The for loop only executes once, but there are 3 files in .garbage. Is my directory formatting still incorrect? – user1754045 Mar 04 '13 at 10:56
  • `~/.garbage/*` is probably what you're looking for - there is no implicit search of a directory, so it's only using the name of the path. you probably took some code that previously did `ls ~/.garbage`, with the backticks, and this actually lists the contents of the directory – Anya Shenanigans Mar 04 '13 at 11:05
  • The asterisk prints the correct files with the path. Is there a way to only print the file name of every file in .garbage? Or do I need to do something with grep or sed? – user1754045 Mar 04 '13 at 21:24

2 Answers2

0

Why not just use ls ~/.garbage

or if you want each file on a separate line, ls -1 ~/.garbage

Mark Armstrong
  • 440
  • 1
  • 3
  • 11
0

backtic will try to execute whatever is inside them. You are getting this error since you are giving a directory name within backtic.

You can use ls ~/.garbage in backtics as mentioned by Mark or use ~/.garbage/* in quotes and rely on the shell to expand the glob for you. If you want to get only the filename from a full path; use the basename command or some sed/awk magic