Skip to content
Home » Get Organized – Use a Bash Script to Clean Up Your Download Folder

Get Organized – Use a Bash Script to Clean Up Your Download Folder

Organizing your download folder can be a tedious task, especially if you have a lot of files with different extensions. But you can use automation to easily organize your download folder or any other folder with just one line of bash script! In this blog post, we will show you how to move files into sub directories based on their extension using a one-line bash script.

The Script

The bash script we will be using is very simple and easy to understand. It uses a for loop to iterate through the files in the download folder and then uses if statement to check if the file is a regular file. If the file is a regular file, it will then use the mkdir command to create a new directory based on the file extension and the mv command to move the file into the newly created directory. Here is the script:

for i in *; do if [ -f "$i" ]; then mkdir -p "${i##*.}"; mv -i -- "$i" "${i##*.}"; fi; done

This script will iterate through all the files in the current directory and move them into sub directories based on their extension. For example, if you have a file called example.txt in your download folder, it will be moved to a new folder called txt in your download folder.

Downloads folder organized based on file extensions

Tips

  1. Make sure to run the script in the correct directory! If you want to organize your download folder, make sure to navigate to the ~/Downloads directory before running the script.
  2. Be careful when running the script. Make sure to backup your files before running the script in case something goes wrong.
  3. The script will move all files, including hidden files. If you don’t want to move hidden files, you can add the not -name ".*" option to the if statement.
  4. The script creates new directories based on the file extension. If you want to organize your files based on something else, such as file size, you can modify the script accordingly.
  5. The script only works with files in the current directory, if you want to organize files in subdirectories of the current directory, you can add the mindepth 1 option to the for loop.

Challenge

Now that you’ve learned how to use this script, try organizing your own download folder and see how it works for you. If you have any questions or issues, feel free to leave a comment below. Happy organizing!

Leave a Reply

Your email address will not be published. Required fields are marked *

sixteen + 13 =