You should write scripts for your computer.
They could be your favorite language.
Put aliases in zshrc to call them
Here is “lsp.py”
Python
import os
import time
def get_recently_accessed_directories(directory='.', limit=50):
directories_with_time = []
try:
entries = os.listdir(directory)
except PermissionError:
return directories_with_time # Return an empty list if permission is denied
for entry in entries:
if entry.startswith('.'):
continue # Skip hidden files and directories
path = os.path.join(directory, entry)
if os.path.isdir(path): # Check if it's a directory
directories_with_time.append((path, os.path.getatime(path)))
# Sort by access time and limit to the top 'limit' entries
directories_with_time.sort(key=lambda x: x[1], reverse=True)
return directories_with_time[:limit]
def time_since_modification(directory):
mod_time = os.path.getmtime(directory)
seconds = time.time() - mod_time
if seconds < 60:
return f"{int(seconds)} seconds ago"
elif seconds < 3600:
return f"{int(seconds // 60)} minutes ago"
elif seconds < 86400:
return f"{int(seconds // 3600)} hours ago"
else:
return f"{int(seconds // 86400)} days ago"
def print_files_in_directory(directory='.'):
print("\nRegular ls output:")
for file in os.listdir(directory):
if file.startswith('.'):
continue # Skip hidden files
print(file)
def lsplus(directory='.'):
print("Top 5 most recently accessed directories:")
# Get the most recently accessed directories
for directory, _ in get_recently_accessed_directories(directory):
modification_time = time_since_modification(directory) # Only pass the directory path
print(f"{directory} - Last accessed: {modification_time}")
print_files_in_directory(directory)
# Call lsplus in the script's main section
if __name__ == "__main__":
lsplus()