Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if PWD contains directory name

Tags:

I want to find out if the PWD contains a certain directory name in it, it should be able to test it being anywhere in the output.

For example I have structure paths like public/bower_components/name/ and also have paths which are just public.

I want to test because the contents of the folder name move into the public folder and the bower_components folder is removed.

Thanks

like image 638
zizther Avatar asked Jan 13 '14 17:01

zizther


People also ask

How do I find my current directory name?

To determine the exact location of the current directory at a shell prompt and type the command pwd. This example shows that you are in the user sam's directory, which is in the /home/ directory. The command pwd stands for print working directory.

How do I find the current directory in bash?

By default, bash shows just your current directory, not the entire path. To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd. This tells you that you are in the user sam's directory, which is in the /home directory.

Which shell variable contains the name of the current directory?

The pwd display name of current or working directory. The current working directory as set by the cd command stored in $PWD shell variable.

How do I echo the current directory in Linux?

To print the current working directory, we use the pwd command in the Linux system. pwd (print working directory) – The pwd command is used to display the name of the current working directory in the Linux system using the terminal.


2 Answers

You can use BASH regex for this:

[[ "$PWD" =~ somedir ]] && echo "PWD has somedir" 

OR using shell glob:

[[ "$PWD" == *somedir* ]] && echo "PWD has somedir" 
like image 172
anubhava Avatar answered Oct 19 '22 09:10

anubhava


You can use case:

case "$PWD" in     */somedir/*) …;;     *) ;; # default case esac 

You can use [[:

if [[ "$PWD" = */somedir/* ]]; then … 

You can use regex:

if [[ "$PWD" =~ somedir ]]; then … 

and there are more ways, to boot!

like image 22
kojiro Avatar answered Oct 19 '22 10:10

kojiro