My first tutorial!
Please forgive me if this is not the right section of the Forum for this one, more than happy to change it or remove it if it's off topic.
I've created a quick Python script that automatically organise the download folder (or whatever folder needs to be organised) based off the file type.
A downloaded .jpg file will be moved automatically in a folder called "images", for example, but the script is open to be personalised as much as you want!
Here's the code:
# afm.py - this is the name i gave to my script -
import os
from shutil import move
# directory paths
user = os.getenv('USER')
root_dir = '/home/USER/Downloads/'.format(user)
image_dir = '/home/USER/Downloads/images/'.format(user)
documents_dir = '/home/USER/Downloads/documents/'.format(user)
software_dir = '/home/USER/Downloads/software/'.format(user)
video_dir = '/home/USER/Downloads/video/'.format(user)
audio_dir = '/home/USER/Downloads/audio/'.format(user)
others_dir = '/home/USER/Downloads/others/'.format(user)
# category wise file types
doc_types = ('.doc', '.docx', '.txt', '.pdf', '.xls', '.ppt', '.xlsx', '.pptx')
img_types = ('.jpg', '.jpeg', '.png', '.svg', '.gif', '.tif', '.tiff', '.psd')
software_types = ('.exe', '.pkg', '.dmg', '.deb', '.flatpak', '.snap')
video_types = ('.mov', '.mkv', '.mp4')
audio_types = ('.mp3', '.wav')
def get_non_hidden_files_except_current_file(root_dir):
return [f for f in os.listdir(root_dir) if os.path.isfile(f) and not f.startswith('.') and not f.__eq__(__file__)]
def move_files(files):
for file in files:
# file moved and overwritten if already exists
if file.endswith(doc_types):
move(file, '{}/{}'.format(documents_dir, file))
print('file {} moved to {}'.format(file, documents_dir))
elif file.endswith(img_types):
move(file, '{}/{}'.format(image_dir, file))
print('file {} moved to {}'.format(file, image_dir))
elif file.endswith(software_types):
move(file, '{}/{}'.format(software_dir, file))
print('file {} moved to {}'.format(file, software_dir))
elif file.endswith(video_types):
move(file, '{}/{}'.format(video_dir, file))
print('file {} moved to {}'.format(file, video_dir))
elif file.endswith(audio_types):
move(file, '{}/{}'.format(audio_dir, file))
print('file {} moved to {}'.format(file, audio_dir))
else:
move(file, '{}/{}'.format(others_dir, file))
print('file {} moved to {}'.format(file, others_dir))
if __name__ == "__main__":
files = get_non_hidden_files_except_current_file(root_dir)
move_files(files)
You can add as much as file type you want in the script, and as said before, modify the folders path.
For simplicity, just save the .py file in the folder you want to organise, and create also a log folder (to keep track of what the script does, the script will create a log.txt file).
To run it, you have two options:
- Launch the script from the terminal with the following:
python3 NAMEOFTHESCRIPT.py >>~/YOURFOLDER/LOGFOLDER/log.txt
- Automate the script with Crontab adding this line in the end of the file (i did a 2 minutes script):
-
Open your terminal
-
Run crontab -l (this will tell you if a cron file s already present)
-
Run crontab -e (this will let you open and edit the cron file - i selected nano editor for edit)
-
Add the following line in the end of the file:
*/2 * * * * cd ~/Downloads/ && python3 NAMEOFTHESCRIPT.py >>~/YOURFOLDER/LOGFOLDER/log.txt
-
Press CTRL + O to write the file and save it
-
Press CTRL + X to exit
-
Run this command to verify the file:
$ sudo ls -l /var/spool/cron/crontabs
I hope this can be helpful and feel free to reach out if needed!