LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-16-2018, 04:20 AM   #1
avyadav555
LQ Newbie
 
Registered: Apr 2016
Posts: 20

Rep: Reputation: Disabled
script for output which having same name as directory.


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.
 
Old 11-16-2018, 05:37 AM   #2
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
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##*/}.
 
Old 11-16-2018, 06:26 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by avyadav555 View Post
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.
 
Old 11-16-2018, 06:53 AM   #4
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by avyadav555 View Post
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...

Last edited by l0f4r0; 11-16-2018 at 09:39 AM.
 
1 members found this post helpful.
Old 11-16-2018, 02:29 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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
Code:
for D in */; do wc -l "$D"/* > "$D".txt; done
 
Old 11-17-2018, 05:39 AM   #6
avyadav555
LQ Newbie
 
Registered: Apr 2016
Posts: 20

Original Poster
Rep: Reputation: Disabled
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
 
Old 11-17-2018, 09:40 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by avyadav555 View Post
I want to read both files separately
How is it related to the original question? How do you want to read those files at all?
 
Old 11-17-2018, 10:34 AM   #8
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by avyadav555 View Post
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.
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.

to create a file try using 'touch'

Last edited by BW-userx; 11-17-2018 at 10:47 AM.
 
Old 11-17-2018, 10:37 AM   #9
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by avyadav555 View Post
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 //
what is this suppose to do?
Code:
${D%/}

Last edited by BW-userx; 11-17-2018 at 11:04 AM.
 
Old 11-19-2018, 07:03 AM   #10
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by avyadav555 View Post
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 View Post
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...

Last edited by l0f4r0; 11-19-2018 at 07:05 AM.
 
Old 11-19-2018, 08:16 AM   #11
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Quote:
Originally Posted by l0f4r0 View Post
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 /

Last edited by BW-userx; 11-19-2018 at 08:24 AM.
 
Old 11-19-2018, 08:34 AM   #12
l0f4r0
Member
 
Registered: Jul 2018
Location: Paris
Distribution: Debian
Posts: 900

Rep: Reputation: 290Reputation: 290Reputation: 290
Quote:
Originally Posted by BW-userx View Post
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:
Code:
for D in /*
 
Old 11-19-2018, 07:01 PM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
As was mentioned, the trailing / should be chopped
Code:
for D in */; do d=${D%/}; wc -l "$d"/* > "$d".txt; done
The /* does not include /.[!.]* filenames.
But instead of
Code:
wc -l "$d"/* "$d"/.[!.]*
it is perhaps better to use find
Code:
for D in */
do
  d=${D%/}
  find "$d" -mindepth 1 -maxdepth 1 -type f -exec wc -l {} + > "$d".txt
done
 
  


Reply

Tags
script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How I copy files with same name but in different case in same directory in linux ? Virendra Nath Red Hat 3 07-25-2013 11:53 AM
Multiple directories with exactly the same name in the same directory! pandersson61 Linux - General 3 05-16-2013 02:10 AM
How to find a file which having same name and created on different date on different lalitshaktawat Linux - Server 2 04-25-2013 06:09 AM
Two files with the same name in the same directory? RileyTheWiley Linux - Newbie 4 01-12-2009 05:21 AM
at same directory, is it cannot have same name text file and folder name? hocheetiong Linux - Newbie 6 11-10-2007 10:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration