Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get part of path using linux commands

Tags:

bash

path

get

pwd

Need get part of path, for example "/home/server/folder1/rev.1111/bin" Needed part is "rev.1111" I`ll try to parse by PWD & grep commands, but I am newbie on linux and I cant do this.

like image 205
Roman Avatar asked Aug 15 '11 13:08

Roman


People also ask

How do you find a specific path in a file Linux?

Firstly, we use the dirname command to find the directory in which a file is located. Then we change the directory using the cd command. Next, we print the current working directory using the pwd command. Here, we have applied the -P option to show the physical location instead of the symbolic link.

How do I find the path of a directory in Linux?

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 get the path of a directory in Unix?

pwd prints the user's current working directory. ls [path] prints a listing of a specific file or directory; ls on its own lists the current working directory.


2 Answers

pwd | awk -F/ '{print $(NF-1)}'
like image 75
Sean Bright Avatar answered Oct 22 '22 08:10

Sean Bright


Using the basename & dirname commands:

basename $(dirname $(pwd))
like image 13
Nicolae Dascalu Avatar answered Oct 22 '22 09:10

Nicolae Dascalu