42

Gnome-screenshot 3.1.2 uses filenames like this one

Screenshot at 2011-07-31 12:13:04.png

making it rather easy to see when it was taken.

The problem is that it uses the colon ( : ) character - making it impossible to access such an image from Windows.

Considering I take most of my screenshots so I can send them to Windows users this has caused some problems.

Is there any way to change the default name that is used by the screenshot tool to replace the ":" with "." or "," ?

Chriskin
  • 3,290

9 Answers9

19

It is not actually possible change it easily, maybe you can help with this bug report.

(for the curious, check the source code. Currently, relevant line is 134)

Update: The bug was fixed upstream on 2015-01-28, replacing colons with dashes, which helps. But they didn't take the space out unfortunately.

nealmcb
  • 3,715
fain182
  • 646
11

gnome-screenshot doesn't allow setting a global default for the screenshot filenames, but you can use the -f option to specify a filename on the commandline.

We can use that to write a small bash script that will take the place of the original gnome-screenshot binary and will execute the original with the correct filename parameter.

Note that you will have to have root privileges for the following operations, so prefix each command with sudo or open a root shell with sudo -i.

  1. Move the original gnome-screenshot binary out of the way:

    dpkg-divert --add --rename --divert /usr/bin/gnome-screenshot.real /usr/bin/gnome-screenshot
    
  2. Open /usr/bin/gnome-screenshot in an editor (you should see a new empty file):

    editor /usr/bin/gnome-screenshot
    
  3. Paste the following two lines into the editor:

    #!/bin/bash  
    gnome-screenshot.real -f "$HOME/Pictures/Screenshots/$(date +%F_%H-%M-%S).png" $@
    

    You can insert any path you like after the -f; just make sure to enclose it in quotation marks and to keep the $@ after it.
    In this example, the screenshots will be stored with filenames like /home/yourusername/Pictures/Screenshots/2011-07-31_12-13-04.png. See man date for details on the date +%… syntax.

  4. Save the file and close the editor (e.g. using Ctrl + X in Nano or :wq in Vim).

  5. Make the newly created script executable:

    chmod a+x /usr/bin/gnome-screenshot
    
n.st
  • 1,470
9

Instead of installing additional software and writing this code in the screenshot tool, here's a workaround to rename the files afterwards.

All you need to do is to navigate to the directory and run the following command

rename 's/\:/\-/g' *.png

This will replace all colons with dashes in all file names ending in .png of the ones present in the current directory.

gertvdijk
  • 69,427
9

There is a simple and dirty way to rename all the screenshot taken immediately after the creation, but you will need to install inotify-tools ( apt-get install inotify-tools ) and then you could run this command:

while true; do inotifywait -e CREATE ~/Pictures && rename 's/\:/\./g' Pictures/Screenshot*.png; done;

While this command will run, every time you save a new screenshot in Pictures/, the script will rename every file containing :, substituting : with .

(Maybe you want change the directory, I don't know which directory Gnome 3 uses) If you really like it, you can start this command in a script every time gnome starts.

fain182
  • 646
5

Shutter is another application for taking screenshots which allows you to customize the file name: you can install it from Ubuntu Software Center.

1

If:

  1. you are familiar with binary editing tools such as bless(see ubuntu package for example or github),
  2. and you know C string format modifiers,
  3. and you know the difference between 0x0 and "0",
  4. and you do not mind messing around with binary files installed by your Linux distribution of choice,

then you can modify the file name that is used by gnome-screenshot.

When binary editing gnome-screenshot, you will find two format strings:

  1. %Y-%m-%d %H-%M-%S followed by a null byte,
  2. and Screenshot from %s.%s followed by a null byte.

In the second format string, the first %s is the date, the second %s is the file extension.

From here, you can, for example, overwrite %Y-%m-%d %H-%M-%S with %F-%H%M%S followed by a null byte and Screenshot from %s.%s by shot %s.%s followed by a null byte: screenshots will then be named "shot 2018-05-05_174857.png" for example.

Adapt to your preferences.

There are two constraints:

  1. the new date format and the new file name format must not be longer than the original ones,
  2. your customizations will be destroyed if you install a new version of gnome-screenshot, for example when updating your system.

Warnings:

  • Tested on Ubuntu 16.04 LTS, your mileage may vary.
  • This is a hack, not a clean way of modifying the default name generated by gnome-screenshot: make a backup before proceeding.
  • Posted without any warranty of any kind.
EKP
  • 11
0

Here's mine. I expanded on n.st's answer.

Create a shell script to call gnome-screenshot

Save this to the file: /usr/bin/area_screenshot

You may need to use sudo. So use vi to create it. sudo vi /usr/bin/area_screenshot

Copy the code below into it.

#!/bin/bash

screenshot_dir="$HOME/Documents/screenshot"
current_year_dir="$screenshot_dir/$(date +%Y)"
current_month_dir="$current_year_dir/$(date +%Y_%m)"
fileout="$current_month_dir/$(date +%Y_%m%d_%H%M%S).png"

# Step 1: Check for screenshot directory 
[ -d "$screenshot_dir" ] || mkdir "$screenshot_dir"

# Step 2: Check year and month directory
[ -d "$current_year_dir" ] || mkdir "$current_year_dir"
[ -d "$current_month_dir" ] || mkdir "$current_month_dir"

# Step 3: Take area screenshot, and save to the current month 
[ -d "$current_month_dir" ] && /usr/bin/gnome-screenshot -a -f "$fileout" $@

Then, mark the file as executable. chmod ugo+x /usr/bin/area_screenshot

Then, in your keyboard shortcuts, set area_screenshot to the Printscreen button. You will need to create a custom shortcut for this (maybe someone else can link an example to do this).

What does this do?

It will create a screenshot at HOME/Documents/YEAR/YEAR_MONTH/filex.png.

Where filex.png is in the format YYYY_MMDD_HHMMSS.png. So for example, 2019_1220_121314.png.

How is this helpful? I find this technique is quite useful to take screenshots of comments and articles. Over time, I capture a lot, so it's very convenient to get it automatically categorized into subfolders. Then over the years, they keep accumulating, and the year subfolder keeps it nicely organized. I tend to put the current month in my favorites link (in Windows), and manually update it every month, as I usually only need to look at the current month.

It would be great if Ubuntu, Fedora, etc., made something like this as the standard on Linux. Please take my code example, and make it so! It would help everyone by keeping their screenshots nicely organized.

0

To expand on this answer that applies to Debian, this pertains to Ubuntu 20.04 "Focal Fossa":

I set a custom keyboard shortcut to be:

gnome-screenshot [--flags] --file=file:///home/User/Pictures/Screenshots/$(date +%F_%H-%M-%S).jpg

(If you are copying this, replace User with your own username.)

--flags could be any of the options. You can learn more about gnome-screenshot from this guide.

I set the shortcut to be triggered with the PRINT SCR button and it works like a charm for me now.

Nmath
  • 12,664
0

In Debian 8.6 KDE → System settings → Custom shortcuts, I have set Trigger → PrtScn, and Action →

gnome-screenshot -p -f "$(date +%F_%H-%M-%S)_D.png"

and it works fine.