Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/6143100/how-do-i-open-a-file-in-its-default-program-linux">How do I open a file in its default program - Linux</a> should help you with the first part of your question:</p> <pre><code>xdg-open ~/Downloads/$filename </code></pre> <p>As mentioned in other answers, it's best not to trust the output of <code>ls</code> in scripts, especially if you have unusual characters like newlines in your filenames. One way to robustly get a list of filenames in a script is with the <code>find</code> command, and null-delimiting them into a pipe.</p> <p>So to answer your question with a one-liner:</p> <pre><code>find ~/Downloads -maxdepth 1 -type f -printf "%C@ %p\0" | sort -zrn | { read -d '' ts file; xdg-open "$file"; } </code></pre> <p>Breaking it down:</p> <ul> <li>The <code>find</code> command lists files in the <code>~/Download</code> directory, but doesn't descend any deeper into subdirectories. The filenames are printed with the given printf format, which lists a numerical timestamp, followed by a space, followed by a null delimiter. Note the <a href="http://explainshell.com/explain?cmd=find%20~/Downloads%20-maxdepth%201%20-type%20f%20-printf%20%22%25C@%20%25p%5C0%22" rel="nofollow noreferrer">printf format specifiers for <code>find</code></a> are different to those for regular printf</li> <li>The <a href="http://explainshell.com/explain?cmd=sort%20-zrn" rel="nofollow noreferrer"><code>sort</code> command</a> numerically sorts (-n) the resulting null-delimited list (-z) by the first field (numerical timestamp). Sort order is reversed (-r) so that the latest entry is displayed first</li> <li>The <code>read</code> command reads the timestamp and filename of the first file in the list into the <code>ts</code> and <code>file</code> variables. <code>-d ''</code> tells <code>read</code> to use null delimiters.</li> <li>The file is opened using <code>xdg-open</code>.</li> </ul> <p>Note the <code>read</code> and <code>xdg-open</code> commands are in a <a href="http://tldp.org/LDP/abs/html/special-chars.html#CODEBLOCKREF" rel="nofollow noreferrer">curly bracket inline group</a>, so the <code>file</code> variable is in scope for both. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload