looping through and deleting header in specific directories [closed]
Clash 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 filesfile 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.
linux sed
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.
add a comment |Â
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 filesfile 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.
linux sed
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.
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 insub1
, and then look for files insub2
, and then look for files inâ¯sub6
.â In other words, you could loop over the subdirectories (sub1
,sub2
, andsub6
).â 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
add a comment |Â
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 filesfile 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.
linux sed
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 filesfile 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.
linux sed
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.
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.
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 insub1
, and then look for files insub2
, and then look for files inâ¯sub6
.â In other words, you could loop over the subdirectories (sub1
,sub2
, andsub6
).â 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
add a comment |Â
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 insub1
, and then look for files insub2
, and then look for files inâ¯sub6
.â In other words, you could loop over the subdirectories (sub1
,sub2
, andsub6
).â 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
add a comment |Â
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.
+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
add a comment |Â
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.
+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
add a comment |Â
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.
+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
add a comment |Â
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.
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.
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
add a comment |Â
+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
add a comment |Â
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 insub2
, and then look for files inâ¯sub6
.â In other words, you could loop over the subdirectories (sub1
,sub2
, andsub6
).â 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