Getting the parent of a directory in Bash
dir=/home/smith/Desktop/Test
parentdir="$(dirname "$dir")"
Works if there is a trailing slash, too.
Get parent directory of shell script's directory
Run dirname twice (nested).
~$ dirname $PWD
/home
~$ dirname `dirname $PWD`
/
~$
How do you get the outermost parent directory in a file path in bash?
I'm a bit confused, I think this is all you want.
#!/bin/bash
echo "$1" | sed -e 's=^/==' -e 's=/.*=='
Or
#!/bin/bash
path=${1#/}
echo "${path%%/*}"
With the first case, since it is a one-liner, you really probably don't need an external script.
If the path can have a leading .
(e.g. ./file/path/can/be/huge.bash
), this will need to be slightly modified.
If the path can be huge, the last thing you surely want is to loop.
Retrieve parent directory of script
How about using dirname
twice?
APP_ROOT="$(dirname "$(dirname "$(readlink -fm "$0")")")"
The quoting desaster is only necessary to guard against whitespace in paths. Otherwise it would be more pleasing to the eye:
APP_ROOT=$(dirname $(dirname $(readlink -fm $0)))
How to extract the parent folder in bash script?
Like this, using pre-defined variable PWD
:
value="$PWD"
echo "you are here :$value"
echo "the folder is: ${value##*/}"
echo "the parent folder is $(basename "${PWD%/*}")"
You can replace the last line with:
dir="${PWD%/*}"
echo "the parent folder is ${dir##*/}"
The backquote (`) is used in the old-style command substitution, e.g.
foo=`command`
The foo=$(command)
syntax is recommended instead. Backslash handling inside $() is less surprising, and $() is easier to nest. See http://mywiki.wooledge.org/BashFAQ/082
How to use 'find' to return parent directory
one way of many:
find / -name 'myfile' -type f -exec dirname {} \;
How do I print all the parent directories in Bash
File:
$ cat subdirs
./dir1/dir2/dir3
./dir4/dir5
Script (ugly a bit, but works fine):
#!/bin/bash
while read line
do
counter=$(echo "${line}" | grep -o '/' | wc -l)
i=1
j=$(($i + 1))
echo "Original string: ${line}"
echo "Its parent directories:"
while [ "${counter}" -gt 0 ]
do
echo "${line}" | cut -d'/' -f$i-$j
counter=$(($counter - 1))
j=$(($j + 1))
done
echo "Next one if exists..."
echo ""
done < subdirs
Output:
Original string: ./dir1/dir2/dir3
Its parent directories:
./dir1
./dir1/dir2
./dir1/dir2/dir3
Next one if exists...
Original string: ./dir4/dir5
Its parent directories:
./dir4
./dir4/dir5
Next one if exists...
Related Topics
Run a Persistent Process via Ssh
On X64 Linux, Differencebetween Syscall, Int 0X80 and Ret to Exit a Program
Why Is Pr_Debug of the Linux Kernel Not Giving Any Output
How to Check Hz in the Terminal
Problems Installing R on Linux Centos 6.2
"Make" Command for Windows - Possible Options
How to Create an Rs256 Jwt Assertion with Bash/Shell Scripting
How to Prevent a Linux User Space Pthread Yielding in Critical Code
Arm Inline Asm: Exit System Call with Value Read from Memory
Bash: Head & Tail Behavior with Bash Script
Why Doesn't "History | Vim" Work
Why Does Gcc Force Pic for X64 Shared Libs