Skip to content

Commit

Permalink
Create 2024-05-30-files-by-type.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ibbsbbr authored May 30, 2024
1 parent b4b9be1 commit 4e93f1b
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions _posts/2024-05-30-files-by-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
categories:
- Code
tags:
- linux
- scripts
comment:
info: aberto.
date: '2024-05-30'
type: post
layout: post
published: true
slug: files-by-type
title: 'Organize files by their extensions'

---

```bash
#!/bin/bash

# Check if the folder name is provided as an argument
if [ -z "$1" ]; then
echo "Usage: $0 <folder>"
exit 1
fi

FOLDER="$1"

# Check if the provided argument is a directory
if [ ! -d "$FOLDER" ]; then
echo "Error: $FOLDER is not a directory."
exit 1
fi

# Change to the specified directory
cd "$FOLDER" || exit

# Get a list of unique file extensions
file_extensions=$(find . -maxdepth 1 -type f | sed -n 's/.*\.\([a-zA-Z0-9]*\)$/\1/p' | sort | uniq)

# Create directories for each file extension and move files
for ext in $file_extensions; do
mkdir -p "$ext"
find . -maxdepth 1 -type f -name "*.$ext" -exec mv {} "$ext" \;
done

echo "Files have been organized by file type."
```

### How to Use the Script

1. Save the script to a file, for example, `organize_files.sh`.
2. Make the script executable:
```bash
chmod +x organize_files.sh
```
3. Run the script with the target folder as an argument:
```bash
./organize_files.sh <folder>
```

0 comments on commit 4e93f1b

Please sign in to comment.