[SOLVED] script for output which having same name as directory.
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
This looks like homework.
The current directory path is held in the PWD environment variable. The subdirectory can be obtained with the parameter expansion ${PWD##*/}.
hi fellas!!!!
i am trying to write a script in which the output should be named after its parent directory.
Ex. i have 3 directory named as 2018-10-01, 2018-11-11, 2018-06-05 . In each directory having n number of files.
I run the command as below : for D in */; do wc -l > response.txt; done
I got output in each folder but with same name ie response.txt
THE THING I WANT HERE response.txt SHOULD BE NAMED AFTER IT'S PARENT DIRECTORY'S NAME.
Don't yell; all caps and bolding isn't pleasant. And you've been asking about scripting for two years now...your previous posting history indicates that. After two years, have you not yet learned how to define a variable? There are LOTS of easily-found bash scripting tutorials, and like allend said...this looks like homework.
I run the command as below : for D in */; do wc -l > response.txt; done
I got output in each folder but with same name ie response.txt
Hmm...you say you are getting some output with that command??
Actually, what you should get is just a new empty response.txt file (only one and not in the different folders themselves) because the redirection is processed. But the command itself should return nothing as is...
The loop variable D cycles thru the directories. Use $D inside the loop!
In your sample the wc -l seems to miss a filename.
The following uses $D to construct filenames for wc -l and an output file
hi used the command ==>> for D in /; do wc -l ${D}/ > ${D%/}Request;done which is working fine . But in each directory there are two file named as .CUD and .CUR , I want to read both files separately . Please suggest
i am trying to write a script in which the output should be named after its parent directory.
Ex.
i have 3 directory named as 2018-10-01, 2018-11-11, 2018-06-05 . In each directory having n number of files.
I run the command as below : for D in */; do wc -l > response.txt; done
I got output in each folder but with same name ie response.txt
THE THING I WANT HERE response.txt SHOULD BE NAMED AFTER IT'S PARENT DIRECTORY'S NAME.
this can be tricky. D is just a var, it could just as well be G to get the same info.
depending on where your dir are in the tree, you have to adjust your code to get the dir you want then it is just as simple as creating a file with its same name then putting that file where ever you want it.
that is a 3 step process.
1. first develop your code until you get it returning the exact dir(s) you want to use.
2. then using that information, further develop your code to create a file by the same name.
3. then further develop your code to create the files somewhere else, ie where you tell them to be created.
this is doing exactly what you are telling it to do, it does not show you using that guild line for development of code.
Code:
for D in */; do wc -l > response.txt; done
put the output of wc -l into a file called 'response.txt' and to over write it every time that is ran.
hi used the command ==>> for D in /; do wc -l ${D}/ > ${D%/}Request;done which is working fine . But in each directory there are two file named as .CUD and .CUR , I want to read both files separately . Please suggest
1. they cannot be in the same dir, having the same name therefor what do you think needs to be done to find out where they are located?
2. you cannot by any means read two files at the same time. one then the other.
3. I do not even see how you are getting files in that code.
Code:
userx@minko:~$ for D in /; do wc -l ${D}/ > ${D%/}Request; cat Request ; done
wc: //: Is a directory
0 //
hi used the command ==>> for D in /; do wc -l ${D}/ > ${D%/}Request;done which is working fine .
Why did you replace for D in */ with for D in /? That command was working properly... Do you want to list contents of root folder?
What are you trying to achieve with your wc -l? You know you cannot apply that command to a folder, right?
Quote:
Originally Posted by BW-userx
what is this suppose to do?
Code:
${D%/}
OP wants to trim the shortest substring ending with a "/" in $D (read from right to left)
It's OK for me as long as $D is a folder and so contains an ending "/", hence */ in the for loop...
Why did you replace for D in */ with for D in /? That command was working properly... Do you want to list contents of root folder?
What are you trying to achieve with your wc -l? You know you cannot apply that command to a folder, right?
OP wants to trim the shortest substring ending with a "/" in $D (read from right to left)
It's OK for me as long as $D is a folder and so contains an ending "/", hence */ in the for loop...
I fingured that might be it, but I wanted to be sure, as you forgot the star *
going from right to left shortest distance to first /
Code:
${D%/*}
chops off everything to the right of the / including the /
I fingured that might be it, but I wanted to be sure, as you forgot the star *
going from right to left shortest distance to first /
Code:
${D%/*}
chops off everything to the right of the / including the /
Yes, BW-userx, you're right
I should have mentioned that ${D%/} can only trim the ending one-character "/".
If OP wants to trim the shortest substring ending with a "/" in $D (read from right to left), (s)he must rather write as you did: ${D%/*}
But I'm not sure OP needs that
And just to clarify for OP:
Code:
for D in /
has nothing to do with the root folder. It's just a one-run loop with one-element, namely "/"
If OP wants to iterate through root folder, (s)he needs:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.