Skip to content

Instantly share code, notes, and snippets.

@marcosdiez
Created September 22, 2020 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcosdiez/35ecd78f6bba011f618bb2530699449b to your computer and use it in GitHub Desktop.
Save marcosdiez/35ecd78f6bba011f618bb2530699449b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import os
def show_paths():
uid = os.getuid()
folder = "/proc"
blacklist = [ "/proc/net" , "/proc/self", "/proc/thread-self" ]
output = []
for proc_path in os.scandir(folder):
if not proc_path.is_dir():
continue
if proc_path.stat().st_uid != uid:
continue
if proc_path.path in blacklist:
continue
cwd = "{}/cwd".format(proc_path.path)
try:
cwd_value = os.readlink(cwd)
except PermissionError:
continue
if cwd_value not in output:
output.append(cwd_value)
return output
def max_length(the_list):
max_length = 0
for elem in the_list:
this_length = len(elem)
if this_length > max_length:
max_length = this_length
return max_length
if len(sys.argv) < 2:
print("This program shows the path in which each bash is located, so you can easily go to the same path in a different termnial")
print("")
paths = show_paths()
length = max_length(paths)
mask = "cd {path:" + str(length) + "} # {counter}"
counter = 0
for path in paths:
print(mask.format(counter=counter, path=path))
counter += 1
print("")
sys.exit(1)
else:
n = int(sys.argv[1])
print(show_paths()[n])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment