Sign-in / Sign-up
Your question
Solved

LS with an exclude works, fails as script though

Tags:
  • Command Prompt
Last response: in Open Source Software
March 31, 2011 4:37:36 PM

Ok, I just did something similar, I use different types of LS all the time at work to get lists of filenames.

I just recently did one that does an exclude, it worked when ran, so I saved it as a RUNTOREFRESH.sh and put it in crontab to run every hour on the 58th minute.

Now it worked when I ran it, but the script fails returning this error...
RUNTOREFRESH.sh: 1: Syntax error: "(" unexpected

The script is...
ls -RA1 !(Archive) > ./result.csv

It doesn't like the exclude, but I am unsure why. It accepts it if ran as a normal command directly.

I am obviously noob to scripting and really I just started doing some small stuff like file move's and listing > files because I was doing some much manually at work because no one knows anything about linux at all. If someone could give a quick reasoning as to why its failing as well I would really appreciate it.

More about : exclude works fails script

Best solution

March 31, 2011 4:51:07 PM

I'm not quite sure what you are trying to exclude here. To ignore some files when using the "ls" command you need to use the -I switch (or --ignore=) and then provide the pattern that you wish to ignore.
Share
March 31, 2011 5:01:14 PM

I was told to use
!(Directory|Filename.example|anotherexample.sh)

Go ahead and try it, it does work. If I run my statement normally in the terminal, it produces the results.csv file exactly as I wanted, with Archive folder excluded. However when I run as a script sh RUNTOREFRESH.sh I receive the above error... I will try it with --ignore, I was just wondering why it fails in a script but not directly input into the terminal and of course how to fix it.

Thanks for the reply let me play with the --ignore a sec...
m
0
l
Related resources
Can't find your answer ? Ask !
March 31, 2011 5:42:29 PM

Possibly the different way that bash and sh interpret regular expressions. I wasn't aware that !(xxx) worked in either of them (it sounds more like a C expression to me), but I'll take your word for it. I'd go for the ls facilities rather than rely upon shell expansion, which may differ between different shells.
m
0
l
March 31, 2011 6:05:08 PM

ls -1RA --ignore=Archive --ignore=results.csv --ignore=RUNTOREFRESH.sh > ./resultsxxx.csv

Is what I tried. It does work too. Now I have ran into another slight problem...

It used to produce...

FOLDER/SUBFOLDER 03_07_2011:
Facts.docx
Myths.docx
Animals.docx
Books.docx
People.docx


Now it produces...

.:
./FOLDER/SUBFOLDER 03_07_2011:
Facts.docx
Myths.docx
Animals.docx
Books.docx
People.docx


Now it included implied characteristics like .: and on the folder ./ which I really didn't want because the info is copy and pasted. Now I thought the -A option removed implied characteristics. What am I missing?
m
0
l
March 31, 2011 6:52:56 PM

Not quite sure. Perhaps the --ignore option overrides the -A one. What if you add an

--ignore=.*

?
m
0
l
March 31, 2011 7:11:34 PM

No that didn't do anything actually but it just hit me, and appears to work

| grep -v "./"

I still have the .: but that doesn't bother me to bad that's just once at the top. What I showed u was like a 1% sample so the actual ./FOLDERNAME does have to go

IJack, I would like to thank you. You definitely helped me solve my problem.
m
0
l
March 31, 2011 7:12:34 PM

Best answer selected by landsavage.
m
0
l
March 31, 2011 7:28:45 PM

One final refinement. If you pipe the output through

tail -n +1

it will print all but the first line, which would eliminate the ".:".
m
0
l