LS with an exclude works, fails as script though

landsavage

Distinguished
Jan 6, 2009
81
0
18,630
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.
 
Solution
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.

landsavage

Distinguished
Jan 6, 2009
81
0
18,630
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...
 
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.
 

landsavage

Distinguished
Jan 6, 2009
81
0
18,630
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?
 

landsavage

Distinguished
Jan 6, 2009
81
0
18,630
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.