40

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 "," ?

9 Answers 9

17

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.

5
  • 3
    +1 for linking to source code. Now THAT is the linux way. (Although the file name code is actually here, now: git.gnome.org/browse/gnome-utils/tree/gnome-screenshot/…) Jul 16, 2012 at 19:35
  • 1
    Unfortunately, the link is dead: No repositories found
    – gertvdijk
    Feb 4, 2013 at 16:37
  • 2
    New URL: git.gnome.org/browse/gnome-screenshot/tree/src/… (retrieved at 2013-02-16 18:08 UTC+0800) Feb 16, 2013 at 10:08
  • I wish we could tweak this locally before good old GNOME developers wake up a little quicker to such important problems reported by users ;-)
    – Sadi
    Jan 18, 2014 at 13:49
  • 9
    Whoever thought it a good idea to hardcode screenshot names in C, requiring a recompile of the entire desktop environment? Had no one ever thought that a user might want to change the default naming? Now that there is dconf-editor and such, and gnome-screenshot uses it, why not have a dconf entry stting for "screenshot naming policy" which you could set to say Screenshot-%02d.png, and if empty, then go along with the hardcoded naming policy? Eh....
    – sdaau
    Mar 31, 2016 at 8:41
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
    
5
  • This works great. How would I make it so that the file name is prepended with the letters "SS" ?
    – user147176
    May 19, 2020 at 22:34
  • 1
    @user147176 You can change the filename template in step 3, e.g. add "SS" before the $(…) that produces the date/time string.
    – n.st
    May 20, 2020 at 5:25
  • Replacing a file managed by package management is a bad idea: a package update will replace your replacement with the original. So put the wrapper into /usr/local/bin or $HOME/bin instead, and make sure that directory is in your $PATH before /usr/bin. Dec 29, 2022 at 19:13
  • @ReinierPost Using dpkg-divert (step 1) ensures that the package manager is aware of the new location of the package-provided file, and will update that instead of our replacement version.
    – n.st
    Dec 29, 2022 at 23:37
  • @n.st Sure, but I would recommend against it. Dec 29, 2022 at 23:41
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.

3
  • 3
    +1 This is a neat little trick, but your regex should be 's/\:/\./g' Jul 16, 2012 at 19:46
  • another option of replacement: use the unicode character Feb 16, 2013 at 10:35
  • Note that screenshots are saved by default in ~/Pictures, not ~/Desktop.
    – MestreLion
    Apr 4, 2013 at 3:44
8

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.

2
  • Less optimal, but much easier workaround than proposed by fain182. +1
    – gertvdijk
    Feb 4, 2013 at 16:36
  • Btw, I'd consider putting this in a cron executed every minute. Well, I'll opt for n.st's solution instead, but this approach is, at least, very simple.
    – Balmipour
    Jul 10, 2020 at 7:56
5

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

5
  • 1
    Shutter is amazing by itself, and this is imho this is the best approach: instead of fixing bad filenames, do not produce them!
    – MestreLion
    Apr 4, 2013 at 3:21
  • 1
    For example, the filename in the Shutter may have the following wildcard: $nb_name_%Y-%m-%d-%H%M%S. You can set it in the Preferences. Jun 13, 2015 at 6:46
  • 2
    I love Shutter but for quick screenshots that don't need annotation, I still prefer to use the default Ubuntu Screenshot program because it instantly gives two options - Copy to Clipboard or Save as. Fast and easy. Dec 20, 2019 at 10:46
  • @VladimirS. Where did you find those format string values? I found this page searching for documentation on this.
    – Michael
    Apr 28, 2020 at 1:53
  • @Michael You can find it here (at least some of them) Apr 30, 2020 at 12:08
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.
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.

1
  • 1
    I wonder which Debian-version has been recent five years ago.
    – mook765
    Oct 13, 2016 at 16:03
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .