-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename.sh
29 lines (24 loc) · 1 KB
/
rename.sh
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
#!/bin/bash
# Bash script to rename files in the current directory by adding a prefix based on creation time
# Get the current directory
directory=$(pwd)
# Get the list of files sorted by creation time
files=$(ls -1tr --time=creation "$directory")
# Initialize the prefix counter
counter=1
# Iterate over the sorted list and rename each file
# Extract the maximum counter value from existing files
max_counter=$(ls -1 | grep -o '^[0-9]\+_' | grep -o '^[0-9]\+' | sort -nr | head -n 1)
counter=$((max_counter + 1))
for file in $files; do
# Check if it's a regular file and not the script itself
if [ -d "$file" ] && [ "$file" != "rename.sh" ] && [ "$file" != ".gitnore" ] && [ "$file" != "README.md" ]; then
# Check if the file already has the correct prefix
if [[ ! "$file" =~ ^[0-9]+_ ]]; then
# Rename the file by adding the prefix
mv "$file" "${directory}/${counter}_$file"
# Increment the counter
counter=$((counter + 1))
fi
fi
done