My requirement is that shell script should print the folder name of the file.
For example comp.class is present in D:/Practice/HelloWorldPractice/bin folder, I should get output after executing command is bin.
My command searches a file and prints the full path of that file . After that, it removes the filename from full path. At the end, it remove everything except folder name of file.
code
filesDirName="/D/Practice/HelloWorldSagar"
file=comp.class
echo $(find "${filesDirName}" -name ${file}) | awk -F '${file}' '{ print $1 }'| awk -F '${filesDirName}' '{ print $2 }'
output empty output
code
When I am entering below command in terminal it is working fine.... But when I am doing it through variables, I am not able to get output.
echo "$(find "/D/Practice/HelloWorldSagar" -name comp.class)" | awk -F 'comp.class' '{ print $1 }'| awk -F '/D/Practice/HelloWorldSagar' '{ print $2 }'
output
/bin/
$(basename $(dirname $f))
to get folder name of$f
). 1) there is non need to echo $(find ...) 2) I wouldn't use a directory name as separator. 3) use-F/
andNF
's awk variable.$NF
is last field and$(NF-1)
the one before. – Archemar Jan 06 '19 at 14:15