LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-11-2009, 06:13 AM   #1
ls86
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Rep: Reputation: 0
Running a command on all files within a directory


Hi All,

I have a bunch of css and js files that I want to minify.These reside in different directories.

I want to minify them using the YUI compressor.The compressor is a jar file, and it is possible to compress a file using the following command:

java -jar yuicompressor-2.4.2.jar -v -o button.css button.css

here the source file is button.css and I am just overwriting the current contents with the minified contents.

I was wondering if anyone can tell me if given a parent directory how I can recurse through all the sub directories - minifying all the css/js files that I find by applying the above command.

Thanks in advance for any suggestions.
 
Old 09-11-2009, 06:21 AM   #2
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
You'll be wanting the find command:
Code:
find /parent/directory -iname "button.css" -exec java -jar yuicompressor-2.4.2.jar -v -o button.css "{}" \;
I can't test this as I don't have yuicompressor on my systems, but in essence, you find anything whose name matches button.css, then you execute the command you want to apply to the found file using the -exec option. After that, {} represents the found file in the command, and you have to terminate the find command with \;. I'd test the command by adding an echo before the yuicompressor command on some non-mission-critical files first:
Code:
find /parent/directory -iname "button.css" -exec echo java -jar yuicompressor-2.4.2.jar -v -o button.css "{}" \;
This way, it'll spit out the commands to stdout as though you were typing them in manually. This means you can check to make sure it's doing what you expect. Then, when you're happy with the command, take out the echo.

As I said, be careful, because as soon as you whip out the echo, it'll actually start doing things to files. I'd highly recommend having a backup before you start this.

Last edited by pwc101; 09-11-2009 at 07:00 AM. Reason: important typo
 
Old 09-11-2009, 06:22 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Assuming all the css/js files have extensions .css and .js then try
Code:
/usr/bin/find <top level directory name> -name '*.css' -o -name '*.js' -exec echo <your command> {} \;
The find command replaces {} with the name of each matching file in turn. After testing, when you are confient the find command is generating the required commands, run it again without the "echo".

EDIT: pwc101 got there first while I was typing

Last edited by catkin; 09-11-2009 at 06:24 AM.
 
Old 09-11-2009, 06:26 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Recursion can be performed by the command find. A simple way to do that is a loop over the result of the find command, that is a list of all .css and .js files inside the source directory tree. For example:
Code:
while read file
do
  echo java -jar yuicompressor-2.4.2.jar -v -o "$file" "$file"
done < <(find /path/to/parent/dir -type f \( -name \*.css -o -name \*.js \))
Here the find command searches all the files with suffixes .css OR .js under /path/to/parent/dir and passes the list of files to the while loop using process substitution. I deliberately put an echo in front of the java command for testing purposes: the loop will just print out the commands to be executed without actually run them. In this way you can check the sanity of the code and if the result you get matches exactly what do you expect.

Edit: beaten by pwc101 and catkin! Too slow this time!

Last edited by colucix; 09-11-2009 at 06:29 AM.
 
Old 09-11-2009, 06:43 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by colucix View Post
Edit: beaten by pwc101 and catkin! Too slow this time!
Interesting to reflect on the similarities and differences between the 3 essentially similar solutions. Pwc101 used -iname rather than -name thus picking up any *.CSS *.cSs files etc. Colucix used -type f so we don't mess with any directories, pipes, device files etc. Colucix also used a script which allows easy extensibility for logging etc.
 
Old 09-11-2009, 07:00 AM   #6
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Using -iname is a habit I've developed, though probably not always a great idea. I should add -type f more frequently as I rarely work with directories in this manner. Still, the solution is likely to be a combination of the three
 
Old 09-15-2009, 12:42 AM   #7
ls86
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Thank you very much for all your suggestions.Its very encouraging when you get 3 great replies to your very first post.

I found all the suggestions given to be very useful,but I finally went with colucix's suggestion:

while read file
do
echo java -jar yuicompressor-2.4.2.jar -v -o "$file" "$file"
done < <(find /path/to/parent/dir -type f \( -name \*.css -o -name \*.js \))

it was exactly what I wanted
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
command tofind files by extension through a directory tree, and copy them all to the budword Linux - Newbie 2 06-27-2009 12:48 PM
Running a script on all files in a directory tree jdwilder Linux - Newbie 3 12-04-2008 10:20 AM
List Only Files In a Directory beginning with "k" using the ls command mg92865 Linux - Newbie 3 05-12-2008 06:58 PM
chmod command to change all files in directory paul_mat Linux - Newbie 2 01-10-2005 09:05 PM
Running .sh files from the command line zulu1_hl Linux - Newbie 3 11-01-2002 12:05 PM


All times are GMT -5. The time now is 12:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration