looping through and deleting header in specific directories [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












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
done


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







share|improve this question













closed as off-topic by Rui F Ribeiro, Jeff Schaller, G-Man, schily, Kiwy Jun 27 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 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 at 22:04











  • @RuiFRibeiro He showed what he worked out so far and asks for specific help. Seems reasonable to me.
    – Minix
    Jun 26 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 at 3:14














up vote
2
down vote

favorite












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
done


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







share|improve this question













closed as off-topic by Rui F Ribeiro, Jeff Schaller, G-Man, schily, Kiwy Jun 27 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 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 at 22:04











  • @RuiFRibeiro He showed what he worked out so far and asks for specific help. Seems reasonable to me.
    – Minix
    Jun 26 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 at 3:14












up vote
2
down vote

favorite









up vote
2
down vote

favorite











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
done


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







share|improve this question













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
done


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









share|improve this question












share|improve this question




share|improve this question








edited Jun 26 at 23:11









Minix

2,02351734




2,02351734









asked Jun 26 at 21:49









Roshni

112




112




closed as off-topic by Rui F Ribeiro, Jeff Schaller, G-Man, schily, Kiwy Jun 27 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.




closed as off-topic by Rui F Ribeiro, Jeff Schaller, G-Man, schily, Kiwy Jun 27 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 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 at 22:04











  • @RuiFRibeiro He showed what he worked out so far and asks for specific help. Seems reasonable to me.
    – Minix
    Jun 26 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 at 3:14












  • 3




    I'm voting to close this question as off-topic because homework.
    – Rui F Ribeiro
    Jun 26 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 at 22:04











  • @RuiFRibeiro He showed what he worked out so far and asks for specific help. Seems reasonable to me.
    – Minix
    Jun 26 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 at 3:14







3




3




I'm voting to close this question as off-topic because homework.
– Rui F Ribeiro
Jun 26 at 22:01




I'm voting to close this question as off-topic because homework.
– Rui F Ribeiro
Jun 26 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 at 22:04





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 at 22:04













@RuiFRibeiro He showed what he worked out so far and asks for specific help. Seems reasonable to me.
– Minix
Jun 26 at 23:23




@RuiFRibeiro He showed what he worked out so far and asks for specific help. Seems reasonable to me.
– Minix
Jun 26 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 at 3:14




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 at 3:14










1 Answer
1






active

oldest

votes

















up vote
1
down vote













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:



#!/bin/bash

WORK_DIR="$1"
shift # shifts $2 to $1
FILE_END="$1"
shift
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
done
else
echo "No sub directories specified"
exit 1
fi


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.






share|improve this answer























  • +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 at 3:04










  • @G-Man Good catch. I forgot that I changed the option name.
    – Minix
    Jun 27 at 5:54

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













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:



#!/bin/bash

WORK_DIR="$1"
shift # shifts $2 to $1
FILE_END="$1"
shift
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
done
else
echo "No sub directories specified"
exit 1
fi


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.






share|improve this answer























  • +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 at 3:04










  • @G-Man Good catch. I forgot that I changed the option name.
    – Minix
    Jun 27 at 5:54














up vote
1
down vote













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:



#!/bin/bash

WORK_DIR="$1"
shift # shifts $2 to $1
FILE_END="$1"
shift
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
done
else
echo "No sub directories specified"
exit 1
fi


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.






share|improve this answer























  • +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 at 3:04










  • @G-Man Good catch. I forgot that I changed the option name.
    – Minix
    Jun 27 at 5:54












up vote
1
down vote










up vote
1
down vote









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:



#!/bin/bash

WORK_DIR="$1"
shift # shifts $2 to $1
FILE_END="$1"
shift
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
done
else
echo "No sub directories specified"
exit 1
fi


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.






share|improve this answer















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:



#!/bin/bash

WORK_DIR="$1"
shift # shifts $2 to $1
FILE_END="$1"
shift
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
done
else
echo "No sub directories specified"
exit 1
fi


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.







share|improve this answer















share|improve this answer



share|improve this answer








edited Jun 27 at 5:54


























answered Jun 26 at 23:22









Minix

2,02351734




2,02351734











  • +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 at 3:04










  • @G-Man Good catch. I forgot that I changed the option name.
    – Minix
    Jun 27 at 5:54
















  • +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 at 3:04










  • @G-Man Good catch. I forgot that I changed the option name.
    – Minix
    Jun 27 at 5:54















+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 at 3:04




+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 at 3:04












@G-Man Good catch. I forgot that I changed the option name.
– Minix
Jun 27 at 5:54




@G-Man Good catch. I forgot that I changed the option name.
– Minix
Jun 27 at 5:54


Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?