Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Is there a way that I can combine the following commands (the last is a zsh function) into a single bash script?

  • find /path/to/directory/*/* ! -name "*.js" -type f -delete
  • find /path/to/directory/*/* ! -name "*jquery*" -type f -delete
  • find /path/to/directory/*/* ! -name "*jquery*" -type d -delete
  • for dir (/path/to/directory/*(/)) rm -f $dir/*.js(OL[2,-1])
share|improve this question
    
fc -l -1 -4 >./single_bash_script – mikeserv Aug 1 '14 at 15:36
    
Sorry I'm a newb at bash. Could you be a little more specific in your explanation? – user334455 Aug 1 '14 at 15:39

1 Answer 1

up vote 2 down vote accepted

If you simply want to write a script that executes the commands one after the other, you have two real options:

  1. Write a bash script, and for the last command, invoke zsh to execute it (have one shell invoke another shell).
  2. Write a zsh script which in turn runs the four commands, forgetting about bash.

While bash is a very common shell on Linux, there is nothing magical about bash as such. You can just as easily write a zsh script instead; the #! line at the beginning says which program should be used to execute the script.

Hence, if you want to go with option 2 above (which I likely would), just make a plain text file that starts with #!/bin/zsh (or wherever zsh happens to be on your system), and is followed by the commands. You'd get something very much like this:

#!/bin/zsh
find /path/to/directory/*/* ! -name "*.js" -type f -delete
find /path/to/directory/*/* ! -name "*jquery*" -type f -delete
find /path/to/directory/*/* ! -name "*jquery*" -type d -delete
for dir (/path/to/directory/*(/)) rm -f $dir/*.js(OL[2,-1])

There shouldn't be anything more than that to it. Note: depending on the idiosyncrasies of zsh, you may or may not need to use a bit more quoting. I prefer to always put "quotation marks" around variables that are being expanded unless I am positive that it's not needed (and even then often do), just in case.

share|improve this answer
    
In zsh, unlike in sh and direct derivatives (bash, ksh, etc.), you only need double quotes around variable expansion when the whole word may be empty: $foo is the value of foo except that it's removed when that value is empty (instead of yielding an empty word). – Gilles Aug 1 '14 at 21:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.