如何为 gnome-screenshot 自动生成文件名?

如何为 gnome-screenshot 自动生成文件名?

我想使用以下命令:

$ gnome-screenshot -caf=<file name>

我如何传递像Screenshot from 2017-12-07 20-22-56.pngfor这样自动生成的东西<file name>

答案1

您可以使用该date命令将实际日期和时间包含在文件名中。让我们看看我们可以在date手册页

操作数

   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>.

转换规范 %a 区域设置的缩写星期名称。

             %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.

由于date其输出中不能有空格格式化,因此您必须使用两个日期命令,如下所示,首先对于日期,您可以用作+\%Y.\%m.\%d参数,这将导致日期格式如下2017.12.07,对于时间,您可以使用+\%H:\%M:\%S它将导致格式如下20:37:18

剪贴板选项 ( -c) 不能与 的另存为文件名选项 ( -f)一起使用gnome-screenshot,因此您必须选择其中之一。此命令可以执行此操作(c如果您需要剪贴板,只需编辑 重新加入,然后让f和文件名退出,两者不能一起使用):

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

它会生成一个如下的文件名(仅包含您调用此命令的实际日期和时间):

Screenshot from 2017.12.07 20:37:18.png

但是,这会将文件存储在您当前所在的目录中,要添加路径,~/Pictures您需要提供完整路径,因为~不会在引号内展开。以下将保存文件,例如,在您的图片文件夹中:

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

或者,您可以去掉引号,这样您就可以使用波浪符号 ( ~),但随后您必须转义名称中的所有空格:

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

man dateman gnome-screenshotBash 参考手册了解更多详情。

相关内容