0

I want to capture the output of the command below in a variable.

Command:

find . -iname 'FIL*'.TXT

The output is :

./FILE1.TXT

I want to capture './FILE1.TXT' into 'A' variable. But when I am trying

A=`find . -iname 'FIL*'.TXT`

then this command is displaying the data of the file. But I want ./FILE1.TXT value in the variable A.

2
  • Please improve the formulation of your question. A file name is not the same as a file content (and some files have no or several names since files are inodes) Commented Dec 9, 2013 at 5:59
  • 2
    It isn't obvious why the command in back-ticks is copying the data in your file into your variable rather than the name of the file. It is generally better to use the $(...) notation for Command Substitution, but shouldn't affect things. You could use -print as part of the find command, but it shouldn't matter. It might be that you're mishandling the code that reports the file name; it should be just echo "$A" or printf "%s\n" $A or something similar. Be wary of spaces (or, worse, newlines) in file names. Commented Dec 9, 2013 at 6:36

3 Answers 3

1
# ls *.txt
test1.txt  test.txt
# find ./ -maxdepth 1 -iname "*.txt"
./test1.txt
./test.txt
# A=$(find ./ -maxdepth 1 -iname "*.txt")
# echo $A
./test1.txt ./test.txt

You can ignore -maxdepth 1 if you want to. I had to use it for this example.

Or with a single file:

# ls *.txt
test.txt
# find ./ -maxdepth 1 -iname "*.txt"
./test.txt
# A=$(find ./ -maxdepth 1 -iname "*.txt")
# echo $A
./test.txt
Sign up to request clarification or add additional context in comments.

Comments

1

Do you try ?

A="`find . -iname 'FIL*'.TXT`"

and

A="`find . -iname 'FIL*'.TXT -print`"

Comments

0

A file does not have any value, but does have a content. Use the following to display that content.

  find . -iname 'FIL*'.TXT -exec cat {} \;

If you want all the contents (of all such files) in a variable, then

  A=$(find . -iname 'FIL*'.TXT -exec cat {} \;)

BTW you could have used

  find . -iname 'FIL*.TXT' -print0 | xargs -0 cat 

If you want the names of such files in a variable, try

  A=$(find . -iname 'FILE*.txt' -print)

BTW, on some several recent interactive shells (zsh, bash version 4 but not earlier versions) just write

  A=**/FILE*.txt

My feeling is that the ** feature is by itself worth switching to a newer shell, but it is just my opinion.

Also, don't forget that files may have several or no names. Read about inodes ...

4 Comments

But I want the file name because file name is dynamic. It can be different after FIL word.
The above commands are displying the file content. I don't want to see the file content. Could you please suggest different solution.
hey, the last one is not working. cat: */FILE.txt: No such file or directory
The double star **/ is an extension in some shells, like zsh or bash version 4 but not version 3. You could consider using zsh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.