1

I would like to use the following command:

$ gnome-screenshot -caf=<file name>

How can I pass something auto-generated like Screenshot from 2017-12-07 20-22-56.png for <file name>?

  • I want to copy to clipboard and store as file at the same time. – orschiro Dec 7 '17 at 19:38
  • gnome-screenshot already auto-generates filenames. Seems weird to do your own auto-generation on top of that. – muru Dec 8 '17 at 2:43
1

You can use the date command for the task of including the actual date and time into the filename. Let's see what we can find in the date man page:

OPERANDS

   The following operands shall be supported:

   +format   When the format is specified, each conversion specifier shall
             be replaced in  the  standard  output  by  its  corresponding
             value.  All  other  characters  shall be copied to the output
             without change. The output shall always be terminated with  a
             <newline>.

Conversion Specifications %a Locale's abbreviated weekday name.

             %A      Locale's full weekday name.

             %b      Locale's abbreviated month name.

             %B      Locale's full month name.

             %c      Locale's appropriate date and time representation.

             %C      Century  (a  year  divided by 100 and truncated to an
                     integer) as a decimal number [00,99].

             %d      Day of the month as a decimal number [01,31].

             %D      Date in the format mm/dd/yy.

             %e      Day of the month as a decimal number [1,31] in a two-
                     digit field with leading <space> character fill.

             %h      A synonym for %b.

             %H      Hour (24-hour clock) as a decimal number [00,23].

             %I      Hour (12-hour clock) as a decimal number [01,12].

             %j      Day of the year as a decimal number [001,366].

             %m      Month as a decimal number [01,12].

             %M      Minute as a decimal number [00,59].

             %n      A <newline>.

             %p      Locale's equivalent of either AM or PM.

             %r      12-hour  clock time [01,12] using the AM/PM notation;
                     in the POSIX locale,  this  shall  be  equivalent  to
                     %I:%M:%S %p.

             %S      Seconds as a decimal number [00,60].

             %t      A <tab>.

             %T      24-hour clock time [00,23] in the format HH:MM:SS.

             %u      Weekday as a decimal number [1,7] (1=Monday).

             %U      Week  of  the  year  (Sunday  as the first day of the
                     week) as a decimal number [00,53]. All days in a  new
                     year  preceding  the first Sunday shall be considered
                     to be in week 0.

             %V      Week of the year (Monday as  the  first  day  of  the
                     week)  as  a  decimal  number  [01,53].  If  the week
                     containing January 1 has four or more days in the new
                     year,  then it shall be considered week 1; otherwise,
                     it shall be the last week of the previous  year,  and
                     the next week shall be week 1.

             %w      Weekday as a decimal number [0,6] (0=Sunday).

             %W      Week  of  the  year  (Monday  as the first day of the
                     week) as a decimal number [00,53]. All days in a  new
                     year  preceding  the first Monday shall be considered
                     to be in week 0.

             %x      Locale's appropriate date representation.

             %X      Locale's appropriate time representation.

             %y      Year within century [00,99].

             %Y      Year with century as a decimal number.

             %Z      Timezone name, or no characters  if  no  timezone  is
                     determinable.

             %%      A <percent-sign> character.

             See  the  Base  Definitions  volume  of POSIX.1‐2008, Section
             7.3.5, LC_TIME for the conversion  specifier  values  in  the
             POSIX locale.

Since date can not have spaces formatted into its output you would have to use two date commands as shown below, first for the date you can use +\%Y.\%m.\%d as a parameter which will result in a date format like 2017.12.07 and for the time you can use +\%H:\%M:\%S which will result in a format like 20:37:18.

The clipboard option (-c) does not work together with the save as filename option (-f) of gnome-screenshot, so you have to choose either one. This command will do it (just edit the c back in if you need the clipboard, but then let the f and the filename out, both wont work together):

gnome-screenshot -af "Screenshot from $(date +\%Y.\%m.\%d) $(date +\%H:\%M:\%S).png"

It generates a file name like the following (just with the actual date and time you call this command at):

Screenshot from 2017.12.07 20:37:18.png

This will however store the file inside the directory you are currently in, to add a path like ~/Pictures you need to provide a full path since ~ does not get expanded inside the quotes. The following would save the files for example in your pictures folder:

gnome-screenshot -af "/home/$USER/Pictures/Screenshot from $(date +\%Y.\%m.\%d) $(date +\%H:\%M:\%S).png"

Or you get rid of the quotes which then would mean you can use the tilde (~) but then you have to escape all spaces in the name:

gnome-screenshot -af ~/Pictures/Screenshot\ from\ $(date +\%Y.\%m.\%d)\ $(date +\%H:\%M:\%S).png

See man date and man gnome-screenshot as well as Bash reference Manual for further details.

  • Nice! This stores in ~. Can we put it in ~/Pictures? – orschiro Dec 7 '17 at 19:44
  • 2
    @orschiro Yes, you can add path too, but it won't copy to clipboard simultaneously. – pomsky Dec 7 '17 at 19:46
  • For some reasons, this one segfaults. orschiro@x230 ~ $ gnome-screenshot -af "~/Pictures/Screenshot from $(date +\%Y.\%m.\%d) $(date +\%H:\%M:\%S).png" ** Message: Unable to select area using GNOME Shell's builtin screenshot interface, resorting to fallback X11. Segmentation fault (core dumped). – orschiro Dec 7 '17 at 19:46
  • @orschiro Try gnome-screenshot -af ~/Pictures/Screenshot\ from\ $(date +\%Y.\%m.\%d)\ $(date +\%H:\%M:\%S).png. – pomsky Dec 7 '17 at 19:53
  • This is great! It just seems that you cannot use -cf at the same time. Can you confirm? – orschiro Dec 7 '17 at 20:06

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.