Post

Thunar file manager action to move files into a date folder

My photos are stored on a Debian Linux server using ZFS. I have some scripts that automatically sorts new photos into a year-month folder called ‘TODO’ and from there I select some images and sort them into separate folders for each event. This leads to a simple folder structure like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
.
├── 2024
│   ├── 2024-01 - TODO
│   │   ├── IMG_0000.jpeg
│   │   ├── IMG_0001.jpeg
│   │   ├── IMG_0002.jpeg
│   │   ├── IMG_0003.jpeg
│   ├── 2024-01-01 - Some event
│   │   ├── IMG_0000.jpeg
│   │   ├── IMG_0001.jpeg
│   │   ├── IMG_0002.jpeg
│   │   ├── IMG_0003.jpeg
│   └── 2024-01-02 - Other cool stuff
│       ├── IMG_0000.jpeg
│       ├── IMG_0001.jpeg
│       ├── IMG_0002.jpeg
│       ├── IMG_0003.jpeg
...

One issue with this setup is that it is quite labor intensive to type out the date prefix for each of the folders. So let’s automate this task!

Requirements

  • The solution should work for the Thunar file manager that is included with Xfce.
  • When I select some files and right-click there should be a custom menu action Move to date folder.
  • The action will prompt the user for the desired folder name.
  • The action takes the modification date from the first file and translates that in to YYYY-mm-dd format.
  • The action then creates a directory with the format <date> - <Folder name>.
  • Finally, all selected files are moved into this new folder.

Implementation

Luckily it is quite easy to add custom actions to Thunar and plenty of examples can be found here.

Step 1 - Create the custom action

In Thunar, select the Edit menu and choose Configure custom actions.

Press the plus button to add a new action and configure it like this:

basic

The Command should be adapted to your situation, it will point to a shell script in a location of your choice on the file system. I recommend putting the script somewhere in your home directory. You can also customize the icon here.

Step 2 - Appearance conditions

Now select the Appearance condtions tab and configure it like this:

appearance

Step 3 - Save the shell script

Download the shell script here and save it to the location you specified in step 1.

⚠️ The script requires zenity to prompt for user input so make sure it is available. On Debian/Ubuntu based systems install with with apt install zenity.

⚠️ Don’t forget to run chmod 755 datefolder.sh to make it executable.

This is the actual script:

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
#!/bin/bash

# Check if Zenity is installed
if [ ! -x /usr/bin/zenity ]; then
	echo "Zenity is not installed, install it with 'apt install zenity'"
	exit 1
fi

echo "First file: $1"

# At least one file should be passed
if ! [ -e "$1" ]; then
	zenity --text "Can't find input file(s), aborting" --error
	exit 1
fi

# Determine date prefix
DATE_PREFIX=$(stat --format='%y' "$1" |cut -d ' ' -f 1)
if [ -z "$DATE_PREFIX" ]; then
	zenity --text "Can't determine date prefix, aborting" --error
	exit 1
fi

echo "Date prefix: $DATE_PREFIX"

# Ask the user to input a folder name
FOLDER_NAME=$(/usr/bin/zenity --entry --title 'Move into new folder' --text 'Please enter a name for the new folder')
if [ -z "$FOLDER_NAME" ]; then
	zenity --text "Need a folder name, aborting" --error
	exit 1
fi

# Create the new folder
NEW_FOLDER="$DATE_PREFIX - $FOLDER_NAME"
echo "New folder: $NEW_FOLDER"
mkdir -p "$NEW_FOLDER"
if [ ! -d "$NEW_FOLDER" ]; then
	zenity --text "Could not create new folder, aborting" -error
	exit 1
fi

# Move files into new folder
if ! mv -vn "$@" "$NEW_FOLDER"; then
	zenity --text "An error occurred while moving" --error
	exit 1
fi 

For troubleshooting purposes the script can also be ran from the terminal.

Step 4 - Demonstration

Select some image or movie files in Thunar and right-click on them like this:

appearance

Now select the new Move to date folder action and a prompt will show:

appearance

Enter some text and press enter. The result will be that the files are moved into a neat directory.

That’s it! 🎉

appearance

Step 5 - Location of custom action configuration (optional)

Adding the action and configuring it in steps 1 and 2 can also be done by editing your ~/.config/Thunar/uca.xml file and adding an action XML item:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
	<icon>applications-graphics</icon>
	<name>Move to date folder</name>
	<submenu></submenu>
	<unique-id>1737627876536062-1</unique-id>
	<command>/home/benjamin/bin/scripts/datefolder.sh %N</command>
	<description>Determine date of first files and move to new directory with date prefix</description>
	<range>*</range>
	<patterns>*</patterns>
	<audio-files/>
	<image-files/>
	<other-files/>
	<text-files/>
	<video-files/>
</action>
</actions>
This post is licensed under CC BY 4.0 by the author.