0

I am trying to do the below

touch ~/scratches/scr6.txt | cat ~/scr.txt > ~/scratches/scr6.txt | cat /dev/null > ~/scr.txt

I don't know why scr6.txt is always empty, however doing the below fill up the file

cat ~/scr.txt > ~/scratches/scr6.txt

I wonder why the first case is not working?

  • 1
    The touch command is the way to create new, empty files. – serenesat Aug 4 '15 at 20:14
  • 3
    Why the pipes ? touch doesn't generate any output, so why pipe it to cat, where cat isn't reading anything from the stdin pipe ? – steve Aug 4 '15 at 20:21
  • 3
    It would be better if you explain what are you trying to achieve. This chain of commands doesn't make much sense. – jimmij Aug 4 '15 at 20:30
  • 1
    So what did you expect actually? – user86969 Aug 4 '15 at 21:47
  • 1
    I should use && instead of | thanks to @steve – xiarnousx Aug 6 '15 at 16:50
1

Breaking down each of the steps:

  • touch ~/scratches/scr6.txt

    If file exists, updates the timestamp to now. If doesn't exist, creates it.

  • cat ~/scr.txt > ~/scratches/scr6.txt

    Reads ~/scr.txt and writes the contents to ~scratches/scr6.txt

  • cat /dev/null > ~/scr.txt

    Empties the ~/scr.txt file

As mentioned in the comments, using "&&" (AND) between the commands instead of "|" (PIPE) would be more appropriate.

  • 5
    Pretty close, except that it doesn't really happen in that order, necessarily. The | pipes and scr.txt and scr6.txt are all created first by the shell. Both of the txt files are truncated at creation time. The commands run in the pipelines are all started up concurrently. It's why the middle cat doesn't complain that scr6.txt isn't there - because the shell took care of all redirections on its own before calling up any of the commands. – mikeserv Aug 4 '15 at 20:28
  • 1
    @mikeserv thanks for the feedback. The use of pipes for this strikes me as odd/pointless/confusing, why not just join them all together with && instead of | ? – steve Aug 4 '15 at 21:05
  • 1
    @steve I will accept your answer if you mentioned use && instead of pipes. I was not aware of && thanks – xiarnousx Aug 6 '15 at 16:15
2

Okay, considering that scr.txt file contains some text before executing command and scr6.txt doesn't exist yet: the command

touch ~/scratches/scr6.txt | cat ~/scr.txt > ~/scratches/scr6.txt | cat /dev/null > ~/scr.txt
  1. Empty scr.txt file (cat /dev/null > ~/scr.txt)
  2. Overrides scr6.txt with contetns of scr.txt (nothing) (cat ~/scr.txt > ~/scratches/scr6.txt)
  3. Does nothing because scr6.txt already exists (touch ~/scratches/scr6.txt)

But the following command

cat ~/scr.txt > ~/scratches/scr6.txt

Just writes scr.txt contents to scr6.txt

So that's why both files are empty in 1st case and everything works like expected in 2nd case.

0

As I understand it, piped processes all start more-or-less at the same time. Generally, the processes in a pipeline will read from stdin or write to stdout (or both), and completion of the I/O will dictate when the process ends.

For processes that don't have any output (like touch) or any standard input (like cat when operating on a file), I would generally expect that their pipe connections would just be ignored by the program.

So what you would basically have is three commands running in parallel:

touch ~/scratches/scr6.txt, cat ~/scr.txt > ~/scratches/scr6.txt, and cat /dev/null > ~/scr.txt.

It's probably OS-dependent whether command 2 would fail with an error if ~/scr.txt doesn't already exist, since it would be in a race condition with command 3. Commands 1 and 2 are also in a race condition, but the race would only wind up affecting the file's ctime/mtime/atime.

-1

In simple words:

The touch command is the way to create new, empty files. It is also used to change the timestamps (i.e., dates and times of the most recent access and modification) on existing files and directories.

In the first command it creates an empty file named scr6.txt.

But in second command it create a file scr6.txt and writes all the contents of scr.txt to this file.

  • That's the anticipated result, OP is asking why that isn't occurring – Creek Aug 5 '15 at 2:12
  • OP has mentioned I don't know why scr6.txt is always empty, so I guess he is not aware about touch command. And my answer explain that. OP has not mentioned what (s)he is expecting. – serenesat Aug 5 '15 at 5:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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