Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash equivalent of Python's os.path.normpath?

What's the bash equivalent to os.path.normpath? Specifically I'm interested in removing the leading ./ given when executing find.

matt@stanley:~/src/libtelnet-0.20/test$ find
.
./Makefile
./Makefile.in
./Makefile.am
...
like image 373
Matt Joiner Avatar asked Jun 18 '11 01:06

Matt Joiner


People also ask

What is os path Normpath?

os.path. normpath (path) Normalize a pathname by collapsing redundant separators and up-level references so that A//B , A/B/ , A/./B and A/foo/../B all become A/B . This string manipulation may change the meaning of a path that contains symbolic links. On Windows, it converts forward slashes to backward slashes.

How do I find absolute path in bash?

In this case, first, we need the current script's path, and from it, we use dirname to get the directory path of the script file. Once we have that, we cd into the folder and print the working directory. To get the full or absolute path, we attach the basename of the script file to the directory path or $DIR_PATH.

How do I find Python path?

In order to obtain the Current Working Directory in Python, use the os. getcwd() method. This function of the Python OS module returns the string containing the absolute path to the current working directory.

What does os path dirname (__ file __) do?

path. dirname() method in Python is used to get the directory name from the specified path.


1 Answers

Well, for that, you can simply pipe the output through sed, you don't have to normalise the entire path:

your_command_goes_here | sed 's?^\./??'

That will get rid of all ./ sequences at the start of a line.

The following transcript shows this in action:

pax$ find -name 'qq*sh'
./qq.ksh
./qq.sh
./qq.zsh
./qq2.sh
./qq2.zsh
./qqq/qq.sh

pax$ find -name 'qq*sh' | sed 's?^./??'
qq.ksh
qq.sh
qq.zsh
qq2.sh
qq2.zsh
qqq/qq.sh

As you can see, I have a fairly intuitive naming standard for my temporary shell scripts :-)

like image 100
paxdiablo Avatar answered Sep 22 '22 13:09

paxdiablo