Calling a command recursively under subdirectories in TCSH

Posted on 2004-04-08
Last Modified: 2007-02-13
I have a question similar to

Where I want to have a script that I can run so that I can call any command recursively on subdirectories.

One example is that I can do something like

rec_cmd mycommand -f hello.*

where the rec_cmd will call "mycommand -f hello.*" on all subdirectories. The following solution was in bash. I'm looking for one under tcsh.

find -type d | awk "{print \"export STCD=`pwd` ; cd \" \$0 \" ; $1 ; cd $STCD\"}" | sh -x


Question by:edwardt
LVL 44

Accepted Solution

Karl Heinz Kremer earned 250 total points
ID: 10788941
Edward, why do you think you need a version for tcsh? If this script works for you, just use it. Chances are that you have Bash on your system. Tcsh is a nice shell for interactive work, but really has it's problems when used for scripts. You have to use the right tool for the job, and Bash (or sh) is definitely better than tcsh in this case. With tcsh you always have a hard time quoting, and without having tried it, I'm pretty sure that getting this into a tcsh format will result in some horrible combination of single and double quotes.
BTW: Your script may not work with all flavors of find. If you use
find . -type d -print | ...
your chances are much better that it will work on all systems.

Also, you can simplify the script:

find -type d | awk "{print \"( cd \" \$0 \" ; $1 )\"}" | sh -x

LVL 40

Expert Comment

ID: 10788955
That script, or similar, will work fine on your Linux system since it runs in a bash (Bourne shell equivalent) subshell. It's the #!/bin/bash at the beginning of the script file that starts a bash shell to execute the contents of the script file. In general people use Bourne shell for scripts since it is the most basic shell and is present on every version of Linux or Unix. That means that any scripts are portable, regardless of what Linux or Unix they are used on or what shell the user might happen to be using.

Author Comment

ID: 10921875
i was thinking tcsh because i use tcsh as my shell, and call functions using aliases, so if i did something like

rec_cmd command

that the command is an alias in my ~/.tcshrc

But I guess I can put that alias in my ~/.bashrc and it shoudl work too.



Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Squid Connection Pools 3 68
Linux/Apache File Ownership/Permissions 1 64
VMware Workstation 12 Player 16 74
centos commands 6 53
This document is written for Red Hat Enterprise Linux AS release 4 and ORACLE 10g.  Earlier releases can be installed using this document as well however there are some additional steps for packages to be installed see Metalink. Disclaimer: I hav…
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension ( This reminded me of questions tha…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question