Reopen voters: flock() doesn't work on named pipes, this is not a dupe.  I was trying to do the following with named pipes:  mkfifo example.txt ( printf "456"; printf "abc"; printf "\n"; ) &gt; example.txt &amp; ( printf "456"; printf "abc"; printf "\n"; ) &gt; example.txt &amp; ( printf "456"; printf "abc"; printf "\n"; ) &gt; example.txt &amp;   If I cat the example.txt sometimes I got:   456abc 456abc 456abc   Other times I got:  456456456abcabc  abc   I was hoping the writes would be atomic. Is there any way to achieve atomic writes?
124066
After going through the bash ,  question and  one it's still not clear to me how can I perform atomic write (append) operations to a file in bash. I have a script that is run in multiple instances and at some point must write data to a file:  echo "$RESULT" &gt;&gt; `pwd`/$TEMP_DIR/$OUT_FILE   How is it possible to make all write operations from all concurrently running scripts to that file atomic (so that data from one instance doesn't overlap data from another)?
27909
I was trying to concatenate text files in sub-folders and tried:  cat ./{mainfolder1,mainfolder2,mainfolder3}/{subfolder1}/book.txt &gt; out$var   However this did not return anything. So, tried adding a non existing 'subfolder2'  cat ./{mainfolder1,mainfolder2,mainfolder3}/{subfolder1,subfolder2}/book.txt &gt; out$var   And this time it did work out, concatenating the files successfully. Why does this happens?
99360
Back in university, when we had to submit an assignment in CS, we would have to perform a series of steps including running script, date, whoami, etc., and then running our program.  The script command would pipe all text sent to the display to both the display and a specified file as well.  Ever since, I have been looking for a Dos and/or Windows version, but have come up empty. Some programs can be redirected to a file, but then the display is not echoed, and some programs don’t seem to work with redirection at all.  Any ideas?    Edit:  So far, the answers that I’ve gotten seem to work exactly like the standard redirection commands (&lt;, >, |). These do not work with all programs. For example, the Microsoft C++ compiler CL.EXE. If you run cl /? through a redirection command or pipe it through another program (such as TEE), you will not get the header/banner text.  Another example is a program I wrote a while back in Pascal (I think the last compile was in FreePascal). The help text does not get redirected at all. I have seen this occur with other programs as well like MKISOFS. It has a long help text, but cannot be paused by piping it through MORE or redirected to a file!  I have wondered about this for many years. I used to think that it may be because the text is being written directly to the screen (eg port B800) or something, but I have yet to pin down the cause, let alone find a program that can do this job.
80943
I want to pipe file names to other programs, but they all choke when the names contain spaces.  Let's say I have a file called.  foo bar   How can I get find to return the correct name?  Obviously I want:  foo\ bar   or:  "foo bar"   EDIT: I don't want to go through xargs, I want to get a correctly formatted string out of find so that I can pipe the string of file names directly to another program.
4886
If I understand the cat manual correctly:       concatenate files and print on the standard output   cat will take files as argument and print them on standard output. What I don't get is if I use the command:    cat img.png &gt; copy.png   I will obtain 2 png files identical while if I just    cat img.png     I have all chance that my terminal get messed up and misinterpret what I type.     How's that possible?   Binary values are still binary data. Why it does not simply shows a series of 0 and 1 or the interpretation of those binary data in ASCII or whatever the encoding in terminal is?   Is this behavior also possible by cating a text file containing strange characters?   Should a mechanism to prevent this behavior  like try{}catch{} statement should be implemented?
15297
How can I write the same content to many text files by using cat or echo in only one command?  For example I want to write "hello" to file1 and file2. I tried:   echo "hello" &gt;&gt; file1 file2   but it didn't work. How can I do it?
101077
Is there any short way to save the pipe output to the same file that it's processed. For example, this is what I am actually doing  $ cat filename | sort | uniq &gt; result $ rm -f filename $ mv result filename   I was wondering if there was a way to do it in just one line (not appending those commands using &amp;&amp;)  This is not the way, but to get an idea  $ cat filename | sort | uniq &gt; filename
26804
I'm so confused with GNU sed, POSIX sed, and BSD sed's myriad of options.  How do I replace literal \n with the newline character using these three sed types?
56892
I want to reopen the stdin and stdout (and perhaps stderr while I'm at it) filehandles, so that future calls to printf() or putchar() or puts() will go to a file, and future calls to getc() and such will come from a file.  1) I don't want to permanently lose standard input/output/error. I may want to reuse them later in the program.  2) I don't want to open new filehandles because these filehandles would have to be either passed around a lot or global (shudder).  3) I don't want to use any open() or fork() or other system-dependent functions if I can't help it.  So basically, does it work to do this:  stdin = fopen("newin", "r");   And, if it does, how can I get the original value of stdin back? Do I have to store it in a FILE * and just get it back later?
26953
I ran into notes for a server I used to work on back in the day but the redirection does not look right. Maybe someone can confim?  These were entries in my crontab entered by a third-party vendor who maintained the app/database that ran in this HP-UX server.  15 21 * * 0-6 /usr/sys/bin/stop  q  &gt;/stopout   2&lt;&amp;1 # 30 21 * * 0-6 /usr/sys/force     q  &gt;/out       2&lt;&amp;1 # 55 23 * * 0-6 /usr/sys/start     q  &gt;/startout  2&lt;&amp;1 #   I found this in the Bash manual but this means that stderr and stdout is going out to >/{stopout,out,startout} while stderr is copying stdout (which means stdout is twice being written to stopout,out,startout?)? I'm confused :)  3.6.8 Duplicating File Descriptors The redirection operator  [n]&lt;&amp;word is used to duplicate input file descriptors. If word expands to one or more  digits, the file descriptor denoted by n is made to be a copy of that file  descriptor. If the digits in word do not specify a file descriptor open for  input, a redirection error occurs. If word evaluates to ‘-’, file descriptor n  is closed. If n is not specified, the standard input (file descriptor 0) is  used.
123988
I have a program which produces useful information on stdout but also reads from stdin. I want to redirect its standard output to a file without providing anything on standard input. So far, so good: I can do:  program &gt; output   and don't do anything in the tty.  However, the problem is I want to do this in the background. If I do:  program &gt; output &amp;   the program will get suspended ("suspended (tty input)").  If I do:  program &lt; /dev/null &gt; output &amp;   the program terminates immediately because it reaches EOF.  It seems that what I need is to pipe into program something which does not do anything for an indefinite amount of time and does not read stdin. The following approaches work:  while true; do sleep 100; done | program &gt; output &amp; mkfifo fifo &amp;&amp; cat fifo | program &gt; output &amp; tail -f /dev/null | program &gt; output &amp;   However, this is all very ugly. There has to be an elegant way, using standard Unix utilities, to "do nothing, indefinitely" (to paraphrase man true). How could I achieve this? (My main criteria for elegance here: no temporary files; no busy-waiting or periodic wakeups; no exotic utilities; as short as possible.)
21123
Would like to have each line in a file repeated a fixed number of times.  e.g. have each line repeated four times:  a b c   becomes:  a a a a b b b b c c c c   I've done some searching, and there are a lot of questions and answers along the lines of doing the reverse, e.g. merging duplicate lines into single lines, and maybe a few about doubling lines by printing them again.  It would be easy to do this in C, but I wish I knew more about the native commands so I wouldn't have to resort to these kinds of one-off throw-aways all the time.
105543
From man watch:     Non-printing characters are stripped   from program output.  Use "cat -v" as   part of the command pipeline if you   want to see them.   So how do I use cat -v if I want to see the colored output from:  watch ls -al --color
51340
I want to watch a file until a bit of text appears  I found this answer:   but when I tried it on ubuntu, it doesn't exit:  $ echo one &gt; test.txt $ echo two &gt;&gt; test.txt $ echo three &gt;&gt; test.txt $ echo four &gt;&gt; test.txt $ cat test.txt | sed '/w/ q' one two $   works as expected.  however when I try to tail the file  $ tail -f test.txt | sed '/w/ q' one two   it never exits.  tail does not stop even though the pipe is broken.  Does anybody know how to make tail exit when sed exits?
33213
I have the following printf function:  $ printf '%s %s %s\t%s\n' 100644 blob 8e1e f1.txt 100644 blob 9de7 f2.txt | git mktree   Can anyone please explain what it produces and why? I expected to have equal number of formatting options %s for each argument string but I have a lot more arguments strings here.
39916
Observation: I have an executable named foo, located in /b/foo. It is compiled against an old header of a dynamic library, causing it to segfault when executed:  $ foo Segmentation fault. // Expected behaviour.   Now, I compile a new version of foo to /a/foo against the new dynamic library that should execute just fine. Directory a/ is in my $PATH before b/, so /a/foo should be selected:  $ which foo /a/foo   When I execute foo, the following happens:  $ foo Segmentation fault.   Therefore, it seems that /b/foo gets executed, whereas "which" tells me that /a/foo should be executed. To make things weirder, when I run the full path $(which /a/foo), things run fine:  $ /a/foo OK!  $ cp /a/foo . $ ./foo OK!     To go yet one step further, if I now delete /a/foo:  $ rm /a/foo   Then /b/foo must surely be chosen, right?  $ which foo /b/foo $ foo bash: /a/foo: No such file or directory $ $(which foo) Segmentation fault. // Expected result.   Nope!  Fix: Source .bash_profile and .bashrc and the problem disappears.  Reproducibility: Every time. Just remove /a/foo, source ~/.bash_profile, create /a/foo, and the above observation re-occurs.  Question: Does anyone have an idea what went wrong here?  Hypothesis: "which" is up-to-date, but the system chooses based on "what was used to be the case". In my example above, /a/foo did not yet exist when the terminal was opened: I had only just created it. Therefore, when /a/foo was created, "which" did detect /a/foo, but the system still chose /b/foo, because it was somehow out of sync? But why is the system out of sync?
44867
The statement below should print afc abc but it prints abc abc instead. I tried many times but every time same result Don't know why this happens could anyone explain and help me?  echo abc|xargs -i{} echo $(echo {}|sed 's/b/f/g') {}