How to get the name of a new file through the terminal?

I am trying to create a macro for Keyboard Maestro for OS X by following these steps:

  1. Get the name of the new file in the directory on my disk, depending on the created date;
  2. Insert the text "newest file:" plus the name of the newest file.

One of its options is โ€œRun the shell script,โ€ so I thought it would do it in 1. After several attempts by Google, I came up with the following:

cd /path/to/directory/ ls -t | head -n1 

This is correct, and returns the first file name. However, it looks like it also includes a line break, which I don't want. As for 2: I can output the text "newest file:" with another action in the application and insert the file name behind it. But I wonder if you can return the "random text" + the result of the ls .

So my question is: can I only do this with the ls ? And how do I get only the name of the last file without any rows or returns?

+5
source share
6 answers

Since you are already using channels, just add one more:

 ls -t | head -n1 |awk '{printf("newest file: %s",$0)}' 

(Note that "printf" does not include "\ n" at the end, which eliminates line breaks)

Edit:

With Arkku's suggestion to exit awk after the first line, it looks like this:

 ls -t | awk '{printf("newest file: %s",$0);exit}' 
+10
source
 cd /path/to/directory/ echo -n "random text goes here" $(ls -t | head -n1) 

If you want, you can add more text at the end in the same way.

+4
source

Using ls output is bad practice.

find -type f -printf '%T+ %p\n' | sort -r | head -n 1

+3
source

You can do this in bash in one statement, for example:

 echo -n "newest file: $(ls -t |head -n1)" 

You can also delete this new line without echo :

 ls -t |head -n1 |tr -d '\n' 

Make sure that ls does not output colors for streams other than tty (i.e. indicate color by ls --color=never or ls --color=auto or not at all).

The ls solution will output files of any type, sorted by modification time. If you only need regular files or donโ€™t need directories, you can use find and xargs :

 echo -n "newest file: $(find . -maxdepth 1 -type f -print0 |xargs -0 ls -t)" 
+2
source

You cannot do this with ls . However, since echo usually built into the shell, it does not actually add any overhead to the script. To get only the file name, I would suggest:

 echo -n "newest: $(ls -t1 | head -n1)" 

If for some reason you really want to eliminate head , then I suppose you could go for something like:

 ls -t1 | ( read n; echo -n "newest: $n") 

( read is built into the shell, head not.)

Please note that these solutions do not return to subdirectories, as this was not asked in the question. In fact, subdirectories can be printed as the latest "file".

-1
source

Since this appears first when I search for โ€œfind a new file on the mac shellโ€, then I thought I would send it to others to help ...

If you try to find a file only in this directory, thatโ€™s good, but I found that the search works very well, and you can learn something new in this process. However, I had to find the GNU search on my mac to use the line of code that diimdeep posted above.

If you run Homebrew, what do you need if you spend time in the terminal or perhaps one of the alternatives. You can run "brew install findutils" Once this is finished, the code above will work as follows ...

 sudo /usr/local/opt/findutils/bin/gfind -type f -printf '%T+ %p\n' | sort -r | head -n 1 

Just change the last bit to 10 if you want more. gfine will take the path if you need to specify this. I usually connect to the folder in which I want to start. Find automatically recursively.

-1
source

Source: https://habr.com/ru/post/1309832/


All Articles