-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathorganize_files.sh
More file actions
64 lines (53 loc) · 1.47 KB
/
organize_files.sh
File metadata and controls
64 lines (53 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/bash
# Javier Ferrándiz Fernández - 27/07/2023 - https://github.com/javisys
# Directory to organize
src_dir="$HOME"
# Function to move files to their respective subfolders
organize_files() {
local file="$1"
local file_type
# Get the file type using the "file" command
file_type=$(file -b --mime-type "$file")
# Removing blanks from the file type
file_type=$(echo "$file_type" | tr -d '[:space:]')
case "$file_type" in
image/*)
dest_dir="$src_dir/images"
;;
audio/*)
dest_dir="$source_dir/music"
;;
video/*)
dest_dir="$source_dir/videos"
;;
application/pdf)
dest_dir="$source_dir/documents"
;;
Otros)
dest_dir="$source_dir/others"
;;
q)
exit 0
;;
*)
echo "You have not chosen any function"
exit 1
;;
esac
# Create the destination folder if it does not exist
[ ! -d "$dest_dir" ] && mkdir -p "$dest_dir"
# Move the file to the destination folder
mv "$file" "$dest_dir/"
}
# Check if the source directory exists
if [ ! -d "$src_dir" ];
then
echo "The source directory does not exist."
exit 1
fi
# Moving files to subfolders according to their type
find "$src_dir" -type f -print0 | while IFS= read -r -d '' file;
do
organize_files "$file"
done
echo "The organization has been completed."