LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-10-2022, 01:35 PM   #1
1s440
Member
 
Registered: Mar 2018
Posts: 266

Rep: Reputation: Disabled
bash find


Hi all,

I wanted to find the "temp.txt" files recursively in directories and subdirecotires and then display the output of temp.txt as well.

#!/bin/bash
filename='temp.txt'
n=1
while read line; do
# reading each line
echo "$line"
n=$((n+1))
done < $filename

I am not sure if i can just add find command to the above script.
 
Old 06-10-2022, 02:05 PM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,026

Rep: Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343Reputation: 7343
Quote:
Originally Posted by 1s440 View Post
Hi all,

I wanted to find the "temp.txt" files recursively in directories and subdirecotires and then display the output of temp.txt as well.
#!/bin/bash
filename='temp.txt'
n=1
while read line; do
# reading each line
echo "$line"
n=$((n+1))
done < $filename
please use code tags: https://www.linuxquestions.org/quest...do=bbcode#code
Why don't you use: cat $filename ?

Quote:
Originally Posted by 1s440 View Post
I am not sure if i can just add find command to the above script.
Why not?
 
Old 06-10-2022, 02:22 PM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Looks a lot like homework to me.

fwiw, you can use find to simply find all files named temp.txt (option: -name), and execute a command like cat on each (option: -exec).
Please read:
Code:
man find
Please use CODE tags for full command output and code (see my signature).
 
Old 06-10-2022, 02:24 PM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,151
Blog Entries: 6

Rep: Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835Reputation: 1835
I wanted to find the "temp.txt" files recursively in directories
Code:
find . -name temp.txt
 
Old 06-10-2022, 11:26 PM   #5
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
[QUOTE=pan64;6359997]please use code tags: https://www.linuxquestions.org/quest...do=bbcode#code
Why don't you use: cat $filename ?


You mean
Code:
#!/bin/bash
filename='temp.txt’
find . -name $filename
cat $filename
 
Old 06-11-2022, 12:29 AM   #6
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
Looks a lot like homework to me.

fwiw, you can use find to simply find all files named temp.txt (option: -name), and execute a command like cat on each (option: -exec).
Please read:
Code:
man find
Please use CODE tags for full command output and code (see my signature).
It’s not a homework. I think this group is to help people when people get stuck at some point..
 
Old 06-11-2022, 12:31 AM   #7
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by teckk View Post
I wanted to find the "temp.txt" files recursively in directories
Code:
find . -name temp.txt
If I need to find files across the all the servers then I think bash script won’t help isn’t it ?
 
Old 06-11-2022, 12:52 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Now that smalltalk is over, can you ask your actual question?
 
1 members found this post helpful.
Old 06-11-2022, 01:09 AM   #9
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,818

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by 1s440 View Post
Hi all,

I wanted to find the "temp.txt" files recursively in directories and subdirecotires and then display the output of temp.txt as well.

#!/bin/bash
filename='temp.txt'
n=1
while read line; do
# reading each line
echo "$line"
n=$((n+1))
done < $filename

I am not sure if i can just add find command to the above script.
Your script appears to read a single file line by line, counting each line (but never using that result for anything). I'm not sure how you could insert a recursive find into that script either.

Code:
find . -type f -name temp.txt -exec cat {} \;
will display all the files named 'temp.txt' in and beneath the current directory.

If I was correct in assuming that you were trying to count all the lines in the files you find, to get that, you could use:
Code:
wc -l $( find . -type f -name temp.txt ) | grep total | awk '{print $1}'
Instead of using 'find' twice, use it once to capture the list of files in an array:
Code:
declare -a FILES
FILES=( $( find . -type f -name text.txt ) )
While it's not strictly necessary to declare the array, it's not a bad idea to document what that variable is used for (when you come back to make changes to your script next year).

Once you have an array of filepaths, you can use it in other commands:
Code:
for F in ${FILES[@]}
do
    cat ${F}
done
echo "$( wc -l ${FILES[@]} | grep total ) lines."
Putting it all together (and simplifying it a bit) and you'd get:
Code:
declare -a FILES
FILES=( $( find . -type f -name text.txt ) )
cat ${FILES[@]}
echo "$( wc -l ${FILES[@]} | grep total ) lines."
Suggested leisure reading: Advanced Bash-Scripting Guide -- An in-depth exploration of the art of shell scripting

Have a lot of fun.
 
Old 06-11-2022, 01:56 AM   #10
rnturn
Senior Member
 
Registered: Jan 2003
Location: Illinois (SW Chicago 'burbs)
Distribution: openSUSE, Raspbian, Slackware. Previous: MacOS, Red Hat, Coherent, Consensys SVR4.2, Tru64, Solaris
Posts: 2,818

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
Quote:
Originally Posted by 1s440 View Post
If I need to find files across the all the servers then I think bash script won’t help isn’t it ?
Hmm... that information might have been nice to know up front.

You could accomplish what I think you want to do using a utility like Ansible (or something similar) assuming you have an inventory of "all the servers". But it doesn't make much sense to propose solutions when the problem hasn't been fully presented.

At this point, however, I think the discussion would benefit greatly from a clear description of the problem you are trying to solve. What other information can you provide? It appears that locating text files may only be a small part of what you are attempting to do.
 
Old 06-11-2022, 07:43 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Code:
for i in host1 host2; do
    echo "=== $i ==="
    ssh "$i" 'find / -type f -name temp.txt' </dev/null
done
 
Old 06-11-2022, 10:05 AM   #12
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rnturn View Post
Your script appears to read a single file line by line, counting each line (but never using that result for anything). I'm not sure how you could insert a recursive find into that script either.

Code:
find . -type f -name temp.txt -exec cat {} \;
will display all the files named 'temp.txt' in and beneath the current directory.

If I was correct in assuming that you were trying to count all the lines in the files you find, to get that, you could use:
Code:
wc -l $( find . -type f -name temp.txt ) | grep total | awk '{print $1}'
Instead of using 'find' twice, use it once to capture the list of files in an array:
Code:
declare -a FILES
FILES=( $( find . -type f -name text.txt ) )
While it's not strictly necessary to declare the array, it's not a bad idea to document what that variable is used for (when you come back to make changes to your script next year).

Once you have an array of filepaths, you can use it in other commands:
Code:
for F in ${FILES[@]}
do
    cat ${F}
done
echo "$( wc -l ${FILES[@]} | grep total ) lines."
Putting it all together (and simplifying it a bit) and you'd get:
Code:
declare -a FILES
FILES=( $( find . -type f -name text.txt ) )
cat ${FILES[@]}
echo "$( wc -l ${FILES[@]} | grep total ) lines."
Suggested leisure reading: Advanced Bash-Scripting Guide -- An in-depth exploration of the art of shell scripting

Have a lot of fun.
Thank you so much.
 
Old 06-11-2022, 10:17 AM   #13
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rnturn View Post
Your script appears to read a single file line by line, counting each line (but never using that result for anything). I'm not sure how you could insert a recursive find into that script either.

Code:
find . -type f -name temp.txt -exec cat {} \;
will display all the files named 'temp.txt' in and beneath the current directory.

If I was correct in assuming that you were trying to count all the lines in the files you find, to get that, you could use:
Code:
wc -l $( find . -type f -name temp.txt ) | grep total | awk '{print $1}'
Instead of using 'find' twice, use it once to capture the list of files in an array:
Code:
declare -a FILES
FILES=( $( find . -type f -name text.txt ) )
While it's not strictly necessary to declare the array, it's not a bad idea to document what that variable is used for (when you come back to make changes to your script next year).

Once you have an array of filepaths, you can use it in other commands:
Code:
for F in ${FILES[@]}
do
    cat ${F}
done
echo "$( wc -l ${FILES[@]} | grep total ) lines."
Putting it all together (and simplifying it a bit) and you'd get:
Code:
declare -a FILES
FILES=( $( find . -type f -name text.txt ) )
cat ${FILES[@]}
echo "$( wc -l ${FILES[@]} | grep total ) lines."
Suggested leisure reading: Advanced Bash-Scripting Guide -- An in-depth exploration of the art of shell scripting

Have a lot of fun.
Quote:
Originally Posted by rnturn View Post
Hmm... that information might have been nice to know up front.

You could accomplish what I think you want to do using a utility like Ansible (or something similar) assuming you have an inventory of "all the servers". But it doesn't make much sense to propose solutions when the problem hasn't been fully presented.

At this point, however, I think the discussion would benefit greatly from a clear description of the problem you are trying to solve. What other information can you provide? It appears that locating text files may only be a small part of what you are attempting to do.
Yes I also thought about using ansible to fetch for the text files but I am new to environment and find little complicated to understand it.
First login : user@server:
Second login: ssh server2
Third login: they use alias to connect to the server.. ( here the temp.txt files ) are stored.

Code:
user@server:~$ su - s1
ssh server
[s1@ts1/server ~]$ switchuser
switchuser@server ----------->
this is how we connect and from swichuser@server.. there are temp.txt files under /app/tmp/

in the sameway: we have many users say s2, s3, s4...

for ex: to find second temp.txt on s2, but this is manual..

Code:
user@server:~$ su - s2
ssh server
[s2@ts1/server ~]$ switchuser
switchuser@server ----------->


So I am totally confused.. ��

Last edited by 1s440; 06-12-2022 at 01:56 AM.
 
Old 06-11-2022, 12:46 PM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,879
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Code:
for i in user1@host1 user2@host2; do
    echo "=== $i ==="
    ssh "$i" 'find / -type f -name temp.txt' </dev/null
done
 
Old 06-11-2022, 04:24 PM   #15
1s440
Member
 
Registered: Mar 2018
Posts: 266

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Code:
for i in user1@host1 user2@host2; do
    echo "=== $i ==="
    ssh "$i" 'find / -type f -name temp.txt' </dev/null
done
Thank you.. but this would do for multiple users isn’t it ?
 
  


Reply



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
[SOLVED] Bash Issue with Find Script: find paths must precede expression: *.txt JockVSJock Programming 4 01-06-2014 09:03 PM
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 05:52 AM
bash my little bash alaios Linux - Newbie 4 01-10-2005 11:59 PM
bash + html + javascript or just bash ? rblampain Programming 4 12-01-2004 07:53 AM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:43 AM.

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