Skip to content

Commit

Permalink
Implement new file_folder scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
endormi authored Jun 23, 2023
2 parents 02e8e6b + b534bfa commit 987565e
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .flake8
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
ignore:
E402,
E501,
E701
E701,
F401
exclude =
.git
venv
Expand Down
3 changes: 3 additions & 0 deletions GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ for example, `-c` and `-f`, to separate scripts into folders.
4 -c -gtl: gnome terminal profile load
4 -f -bf: bf (backup folder)
4 -f -c: clean (clean system of logs and trash)
4 -f -d: check for duplicate files
4 -f -del: del (delete file or directory)
4 -f -dir: directory size calculator
4 -f -e: exists (check if file or dir exists)
4 -f -ext: check file extensions inside a folder
4 -f -f: check file permissions
4 -f -fd: fd (number of files and folders)
4 -f -fl: file updated
4 -f -fs: file size
Expand Down
3 changes: 3 additions & 0 deletions docs/source/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,12 @@ for example, `-c` and `-f`, to separate scripts into folders.
4 -c -gtl: gnome terminal profile load
4 -f -bf: bf (backup folder)
4 -f -c: clean (clean system of logs and trash)
4 -f -d: check for duplicate files
4 -f -del: del (delete file or directory)
4 -f -dir: directory size calculator
4 -f -e: exists (check if file or dir exists)
4 -f -ext: check file extensions inside a folder
4 -f -f: check file permissions
4 -f -fd: fd (number of files and folders)
4 -f -fl: file updated
4 -f -fs: file size
Expand Down
9 changes: 9 additions & 0 deletions man/tilux.1
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,24 @@ bf (backup folder).
.BR 4 " " -f " " -c
clean (clean system of logs and trash).
.TP
.BR 4 " " -f " " -d
check for duplicate files.
.TP
.BR 4 " " -f " " -del
del (delete file or directory).
.TP
.BR 4 " " -f " " -dir
directory size calculator.
.TP
.BR 4 " " -f " " -e
exists (check if file or dir exists).
.TP
.BR 4 " " -f " " -ext
check file extensions inside a folder.
.TP
.BR 4 " " -f " " -f
check file permissions.
.TP
.BR 4 " " -f " " -fd
fd (number of files and folders).
.TP
Expand Down
35 changes: 35 additions & 0 deletions sys/file_folder/dir_size_calculator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env python3

import os
import sys
import time


def calculate_directory_size(directory):
total_size = 0
for root, dirs, files in os.walk(directory):
for file in files:
file_path = os.path.join(root, file)
if os.path.isfile(file_path):
total_size += os.path.getsize(file_path)
return total_size


def main():
directory = input("Enter the directory to calculate its size (default is current directory): ") or '.'

if os.path.exists(directory):
print(f"Total directory size: {calculate_directory_size(directory)} bytes")
else:
print(f"{directory} doesn't exist.")


if __name__ == '__main__':
if len(sys.argv) == 2:
import tools.catch_exception
from tools.logos import Logo

Logo('Dir size calculator')

time.sleep(1)
main()
64 changes: 64 additions & 0 deletions sys/file_folder/duplicate_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/usr/bin/env python3

import os
import sys
import time
import hashlib


def find_duplicate_files(directory):
file_hashes = {}
duplicates = []

exclude_dirs = [
'.bundle', '.cache', '.config', '.eggs', '.env', '.git', '.github', '.idea', '.mypy_cache', '.tox', '.venv',
'$RECYCLE.BIN', '__pycache__', '_Build', '_Pvt_Extensions', '_UpgradeReport_Files', 'AppData',
'ClientBin', 'cover', 'coverage', 'develop-eggs', 'dist', 'doc', 'docs/_build', 'docs/build', 'eggs', 'ENV',
'env', 'env.bak', 'GeneratedArtifacts', 'Generated_Code', 'htmlcov', 'lib64', 'lib/bundler/man', 'local',
'logs', 'modulesbin', 'node_modules', 'pkg', 'profile_default', 'public', 'publish', 'sdist', 'share/python-wheels',
'spec/reports', 'target', 'tempbin', 'test/tmp', 'test/version_tmp', 'tmp', 'var', 'vendor/bundle', 'venv',
'venv.bak'
]

for root, dirs, files in os.walk(directory):
for exclude_dir in exclude_dirs:
if exclude_dir in dirs:
dirs.remove(exclude_dir)
for file in files:
file_path = os.path.join(root, file)
with open(file_path, 'rb') as f:
file_hash = hashlib.md5(f.read()).hexdigest()
if file_hash in file_hashes:
duplicates.append((file_path, file_hashes[file_hash]))
else:
file_hashes[file_hash] = file_path

return duplicates


def main():
directory = input("Enter the directory to search (default is current directory, leave blank for default): ") or '.'

if not os.path.isdir(directory):
print(f"{directory} doesn't exist.")
return

duplicate_files = find_duplicate_files(directory)
if duplicate_files:
print("\nDuplicate files found:")
for file1, file2 in duplicate_files:
time.sleep(.2)
print(f"{file1} - {file2}")
else:
print("No duplicate files found.")


if __name__ == '__main__':
if len(sys.argv) == 2:
import tools.catch_exception
from tools.logos import Logo

Logo('Duplicate files')

time.sleep(1)
main()
22 changes: 15 additions & 7 deletions sys/file_folder/extension_f.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import time


def check_files_with_extension(directory='.', extension=''):
def check_files_with_extension(directory, extension=''):
exclude_dirs = [
'.bundle', '.cache', '.config', '.eggs', '.env', '.git', '.idea', '.mypy_cache', '.tox', '.venv',
'.bundle', '.cache', '.config', '.eggs', '.env', '.git', '.github', '.idea', '.mypy_cache', '.tox', '.venv',
'$RECYCLE.BIN', '__pycache__', '_Build', '_Pvt_Extensions', '_UpgradeReport_Files', 'AppData',
'ClientBin', 'cover', 'coverage', 'develop-eggs', 'dist', 'doc', 'docs/_build', 'docs/build', 'eggs', 'ENV',
'env', 'env.bak', 'GeneratedArtifacts', 'Generated_Code', 'htmlcov', 'lib64', 'lib/bundler/man', 'local',
Expand All @@ -16,6 +16,8 @@ def check_files_with_extension(directory='.', extension=''):
'venv.bak'
]

found_extension = False

for root, dirs, files in os.walk(directory):
for exclude_dir in exclude_dirs:
if exclude_dir in dirs:
Expand All @@ -24,19 +26,25 @@ def check_files_with_extension(directory='.', extension=''):
if file.endswith(extension):
file_path = os.path.join(root, file)
with open(file_path, 'r'):
found_extension = True
print(f"Found file: {file_path}")
time.sleep(.2)

if not found_extension:
print(f"Extension '{extension}' files not found.")


def main():
directory = input("Enter the directory to search (default is current directory): ")
directory = input("Enter the directory to search (default is current directory, leave blank for default): ") or '.'

if not os.path.isdir(directory):
print(f"{directory} doesn't exist.")
return

extension = input("Enter the file extension to check: ")
if len(sys.argv) == 2: ce.__input__(extension)
print()

if not directory:
directory = '.'

check_files_with_extension(directory, extension)


Expand All @@ -45,7 +53,7 @@ def main():
import tools.catch_exception as ce
from tools.logos import Logo

Logo('Check file extension')
Logo('File extension')

time.sleep(1)
main()
49 changes: 49 additions & 0 deletions sys/file_folder/file_permissions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3

import os
import sys
import time


def check_file_permissions(directory):
exclude_dirs = [
'.bundle', '.cache', '.config', '.eggs', '.env', '.git', '.github', '.idea', '.mypy_cache', '.tox', '.venv',
'$RECYCLE.BIN', '__pycache__', '_Build', '_Pvt_Extensions', '_UpgradeReport_Files', 'AppData',
'ClientBin', 'cover', 'coverage', 'develop-eggs', 'dist', 'doc', 'docs/_build', 'docs/build', 'eggs', 'ENV',
'env', 'env.bak', 'GeneratedArtifacts', 'Generated_Code', 'htmlcov', 'lib64', 'lib/bundler/man', 'local',
'logs', 'modulesbin', 'node_modules', 'pkg', 'profile_default', 'public', 'publish', 'sdist', 'share/python-wheels',
'spec/reports', 'target', 'tempbin', 'test/tmp', 'test/version_tmp', 'tmp', 'var', 'vendor/bundle', 'venv',
'venv.bak'
]

for root, dirs, files in os.walk(directory):
for exclude_dir in exclude_dirs:
if exclude_dir in dirs:
dirs.remove(exclude_dir)
for file in files:
file_path = os.path.join(root, file)
permissions = oct(os.stat(file_path).st_mode & 0o777)
time.sleep(.2)
print(f"File: {file_path}, Permissions: {permissions}")


def main():
directory = input("Enter the directory to search (default is current directory, leave blank for default): ") or '.'

if not os.path.isdir(directory):
print(f"{directory} doesn't exist.")
return

print()
check_file_permissions(directory)


if __name__ == '__main__':
if len(sys.argv) == 2:
import tools.catch_exception
from tools.logos import Logo

Logo('File permissions')

time.sleep(1)
main()
22 changes: 15 additions & 7 deletions sys/file_folder/keyword.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import time


def find_files_with_keyword(directory='.', keyword=''):
def find_files_with_keyword(directory, keyword=''):
exclude_dirs = [
'.bundle', '.cache', '.config', '.eggs', '.env', '.git', '.idea', '.mypy_cache', '.tox', '.venv',
'.bundle', '.cache', '.config', '.eggs', '.env', '.git', '.github', '.idea', '.mypy_cache', '.tox', '.venv',
'$RECYCLE.BIN', '__pycache__', '_Build', '_Pvt_Extensions', '_UpgradeReport_Files', 'AppData',
'ClientBin', 'cover', 'coverage', 'develop-eggs', 'dist', 'doc', 'docs/_build', 'docs/build', 'eggs', 'ENV',
'env', 'env.bak', 'GeneratedArtifacts', 'Generated_Code', 'htmlcov', 'lib64', 'lib/bundler/man', 'local',
Expand All @@ -16,6 +16,8 @@ def find_files_with_keyword(directory='.', keyword=''):
'venv.bak'
]

found_keyword = False

for root, dirs, files in os.walk(directory):
for exclude_dir in exclude_dirs:
if exclude_dir in dirs:
Expand All @@ -25,19 +27,25 @@ def find_files_with_keyword(directory='.', keyword=''):
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
file_contents = f.read()
if keyword in file_contents:
found_keyword = True
print(f"Found keyword '{keyword}' in file: {file_path}")
time.sleep(.2)

if not found_keyword:
print(f"Keyword '{keyword}' not found in any files.")


def main():
directory = input("Enter the directory to search (default is current directory): ")
directory = input("Enter the directory to search (default is current directory, leave blank for default): ") or '.'

if not os.path.isdir(directory):
print(f"{directory} doesn't exist.")
return

keyword = input("Enter the keyword or phrase to search for: ")
if len(sys.argv) == 2: ce.__input__(keyword)
print()

if not directory:
directory = '.'

find_files_with_keyword(directory, keyword)


Expand All @@ -46,7 +54,7 @@ def main():
import tools.catch_exception as ce
from tools.logos import Logo

Logo('Files by Keyword')
Logo('Files by keyword')

time.sleep(1)
main()
3 changes: 3 additions & 0 deletions tools/tilux/command_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@
'-f': {
'-bf': -> { TiluxHelpers.sys("bash #{PATH}/sys/file_folder/bf.sh tilux") },
'-c': -> { TiluxHelpers.sys("bash #{PATH}/sys/file_folder/clean.sh tilux") },
'-d': -> { TiluxHelpers.sys("python3 #{PATH}/sys/file_folder/duplicate_file.py tilux") },
'-del': -> { TiluxHelpers.sys("ruby #{PATH}/sys/file_folder/del.rb tilux") },
'-dir': -> { TiluxHelpers.sys("python3 #{PATH}/sys/file_folder/dir_size_calculator.py tilux") },
'-e': -> { TiluxHelpers.sys("ruby #{PATH}/sys/file_folder/exists.rb tilux") },
'-ext': -> { TiluxHelpers.sys("python3 #{PATH}/sys/file_folder/extension_f.py tilux") },
'-f': -> { TiluxHelpers.sys("python3 #{PATH}/sys/file_folder/file_permissions.py tilux") },
'-fd': -> { TiluxHelpers.sys("ruby #{PATH}/sys/file_folder/fd.rb tilux") },
'-fl': -> { TiluxHelpers.sys("ruby #{PATH}/sys/file_folder/file_updated.rb tilux") },
'-fs': lambda do
Expand Down
6 changes: 6 additions & 0 deletions tools/tilux/print_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ def systemf_print
puts '::FF::'
puts '-bf: bf (backup folder)'
puts '-c: clean (clean system of logs and trash)'
puts '-d: check for duplicate files'
puts '-del: del (delete file or directory)'
puts '-dir: directory size calculator'
puts '-e: exists (file or directory)'
puts '-ext: check file extensions inside a folder'
puts '-f: check file permissions'
puts '-fd: fd (number of files and folders)'
puts '-fl: file updated'
puts '-fs: file size'
Expand Down Expand Up @@ -128,9 +131,12 @@ def help_print
puts '4 -c -gtl: gnome terminal profile load'
puts '4 -f -bf: bf (backup folder)'
puts '4 -f -c: clean (clean system of logs and trash)'
puts '4 -f -d: check for duplicate files'
puts '4 -f -del: del (delete file or directory)'
puts '4 -f -dir: directory size calculator'
puts '4 -f -e: exists (check if file or dir exists)'
puts '4 -f -ext: check file extensions inside a folder'
puts '4 -f -f: check file permissions'
puts '4 -f -fd: fd (number of files and folders)'
puts '4 -f -fl: file updated'
puts '4 -f -fs: file size'
Expand Down

0 comments on commit 987565e

Please sign in to comment.