Get last subfolder on a path...
So lets say I create a bash script and I assign a folder variable like:
#!/bin/bash .... Myvariable_FOLDER_PATH=/PATH/TO/SPECIFIC/SUBFOLDER is there any way to make another variable with just the specific SUBFOLDER (?some command?)----> will make My_variable_subfolder = SUBFOLDER so if I do echo $Myvariable_FOLDER_PATH i get: /PATH/TO/SPECIFIC/SUBFOLDER and echo $My_variable_subfolder i get: /SUBFOLDER The tree and path names will change so I jjust want the last subfolder that I will access to access its files. Thanks... |
Just use parameter substitution:
Code:
Myvariable_FOLDER_PATH=/PATH/TO/SPECIFIC/SUBFOLDER Code:
My_variable_subfolder=${Myvariable_FOLDER_PATH##*/} |
basename
The basename command always gives you the last component of a fully qualified path. This can be a directory or a file. e.g. "basename /var/named/chroot/var/named" returns "named". |
Even though using the basename command probably makes the script more readable, using parameter substitution is probably faster since you don't start another process.
|
All times are GMT -5. The time now is 09:12 PM. |