0

I am trying to copy the first two items in my 'Downloads' directory using only the terminal.

I open up zsh, cd into my 'Downloads' directory and start typing. The below reflects what is shown in the terminal:

% ls -lt | head -3
file1.csv
file2.csv  (exactly the files I want)
  
% ls -lt | head -3 > ToBeCopied.txt
% vim ToBeCopied.txt

total 24625744
-rw-r--r--   1 Aaron  staff            0 22 Apr 15:28 ToBeCopied.txt
-rw-r--r--@  1 Aaron  staff        42042 22 Apr 15:16 file1.csv

What happened to file2.csv?

  • The redirection to stdout executed before the `ls` command started. That file is newer than any other file in that folder. It made file2.csv appear later in the list. Redirect your output somewhere else and you'll be fine. – Jeff Holt Apr 22 '21 at 14:45
  • "somewhere else" being "not to a file in the same directory" – Marlon Richert Apr 22 '21 at 19:43
  • @JeffHolt I thought the flow for `ls -lt | head -3` would be: 1. list the files in current directory according to time 2. Pipe that output to the `head` command –  Apr 23 '21 at 15:13
  • @AaronLee See [this](https://stackoverflow.com/q/12942042/1707353) for more information on operator precedence. I would SMH if any shell deviated from what was established 51 years ago. If you search SO for questions asked/week about writing a shell for class, you'll better understand why things work the way they do. – Jeff Holt Apr 23 '21 at 15:42

0 Answers0