Shell question

Hi,

I have this piece of code but I can not understand it in details and it is difficult to find in net. What is the function of the $, type, d and prefix here?

This script wants to get each file from their directory.

Thanks in advance.

for i in $(find  /file directory/ -type d | grep file name)
do 
prefix=`echo $i | cut -f5 -d"/"`
out=$prefix.out
echo $prefix

find /file directory/ -type d => list out all the directories (-type d) in /file directory/
grep file_name => finds the file_name from the list
$(command) => pretend the result of the command to be a variable

But I don't think this script will "get each file from their directory". You will have to change -type d to -type f then.

Best of Luck :slight_smile:

1 Like

-type d means only find directories.
Go to a directory tree and experiment:

$ find . -type f -print
$ find . -type d -print

prefix is a variable here. It's holds a value.
For example, if the file is called taco.txt the prefix is taco.

The $ is used to refer to a variable.
So you say "x=1" to store 1 to the variable.
But you say "echo $x" to display the stored value.

The other $ is used with $(command) syntax to use the output from a command.

To learn, try putting in echo commands to see what happens with intermediate variables. For example, "echo $i".

1 Like

What shell are you using?

I'm a bit confused with what it is trying to do, but here goes:-

for i in $(find  /file directory/ -type d | grep file name)
do 
prefix=`echo $i | cut -f5 -d"/"`
out=$prefix.out
echo $prefix

for i in starts a loop between the following do & done (which you haven't got, so it will fail with a syntax error, unless this is just a snippit.

The $( through to ) set up the conditions for the loop by executing the contents.

It then gets a bit murky. I'm hoping that the "file directory" and "file name" are actually single items, not with spaces in them, else the meaning changes completely. If they are actually filedirectory and filename then the find command will search from filedirectory for any other directories because of the option -type d

You loop then executes for each occurrence.

The prefix= is an odd statement, but it is getting the 5th field of the directory name found. So if you had the following, you would get this output:-

/filedirectory/a/b        no output
/filedirectory/a/b/c      c
/filedirectory/a/b/c/d    c

The first field here is before the first / so that's why c is the only thing displayed on the second line. Because you are after the 5th field, you never get the d from the third line. If you do want that level and all subsequent, change the cut to be cut -f5- -d "/"

You then assign variable out to be the value determined above appended with .out and then never use it, just displaying the line determined above.

So, if you have the directory structure alluded to above, you would get the final output of this:-

blank line
c
c

What are you trying to achieve overall? There is probably a better (less processing/IO) and neater way to do it.

I hope that this helps.

Robin
Liverpool/Blackburn
UK

1 Like

Thanks alot for your explanations. I am using Shell in Ubuntu 12.04.
I have already finished the task I needed to do with this script but as I have not written it, I could not understand it well but now, it's more clear.