I want to loop through only specific directories and delete the first line of files given as input parameters in those directories.

My input parameters should be:

  • -i [input_directory] - root directory
  • -n [directory] directories to check for files
  • file ending

Run as

sh -i /main/folder/ -n (sub1|sub2|sub6) .txt

Script should only delete header for *.txt files in sub1, sub2 and sub6 folders under /main/folder/.

Now I have below script which loops through all folders and deletes header but I am not sure how to loop through only specified folder

for $file in $i/*/*.txt do
    sed '1,1d' $file

Please let me know if there will be any use if we change the way we input parameters.

closed as off-topic by Rui F Ribeiro, Jeff Schaller, G-Man, schily, Kiwy Jun 27 '18 at 13:09

  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    I'm voting to close this question as off-topic because homework. – Rui F Ribeiro Jun 26 '18 at 22:01
  • I am not asking to write the script but just wanted to know the approach. Only idea I got is loop through all directories by splitting based on the delimitter . I just wanted to know if we have any better approches – Roshni Jun 26 '18 at 22:04
  • @RuiFRibeiro He showed what he worked out so far and asks for specific help. Seems reasonable to me. – Minix Jun 26 '18 at 23:23
  • You can look for files in sub1, and then look for files in sub2, and then look for files in sub6.  In other words, you could loop over the subdirectories (sub1, sub2, and sub6).  Or you could do a single loop over all the files, and, for each one, check whether it is in one of the subdirectories you are interested in.  If there might be 1003 subdirectories (i.e., 1000 that you want to ignore), you should probably use the first approach.  But either could work. – G-Man Jun 27 '18 at 3:14

My intuition about commands that let you specify a working directory is, that it is the first parameter. That you have correct, although you could lose the -i command switch.

Secondly any options with multiple values (like the multiple specific sub-directories you want to search in) should be last. That way they don't interfere too much with the other ones.

I would propose to call your command like this:

~$ mycommand /my/root/dir "txt" -sdirs sub1 sub2 sub6

Bash and maybe sh as well has the shift builtin, which lets you shift through the given arguments. You could extract the necessary information from the commandline like so:


shift # shifts $2 to $1
if [ "-sdirs" = "$1" ]; then
    shift # get to the sub dirs
    while [ -n "$1" ]; do # tests if there is a sub dir
        # *do your magic here*
        # $1 should be a sub dir
        # $WORK_DIR/$1/*$FILE_END for example

        # to the next one, break if nothing left
        shift || break
    echo "No sub directories specified"
    exit 1

This is what I would do off the top of my head. More error handling is appropriate and left as an exercise to the reader.

  • +1 for “left as an exercise to the reader.”  But seriously, why are you checking for -n on the command line? – G-Man Jun 27 '18 at 3:04
  • @G-Man Good catch. I forgot that I changed the option name. – Minix Jun 27 '18 at 5:54

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