Linked Questions

59
votes
1answer
5k views

How is this command legal ? “> file1 < file2 cat”

Assuming file2 already exists, the command > file1 < file2 cat appears to be copying the content of file2 to file1. But I cannot understand this structure. I understand that "Nothing" is ...
41
votes
2answers
9k views

How does `cat <> file` work?

cat < file prints the contents of file to stdout. cat > file reads stdin until Ctrl+D is detected and the input text is written to file. cat <> file, at least in my version of Bash, ...
29
votes
5answers
81k views

How to put a newline special character into a file using the echo command and redirection operator?

I would like to create a file by using the echo command and the redirection operator, the file should be made of a few lines. I tried to include a newline by "\n" inside the string: echo "first ...
26
votes
3answers
68k views

Can sed save its output to a file?

> can do it. echo "text" > file tee can do it. echo "test" | tee file Can sed do it without using either of the above? Is it possible to save the output of a sed command to a file without ...
13
votes
6answers
14k views

split file into two parts, at a pattern

How to split a large file into two parts, at a pattern? Given an example file.txt: ABC EFG XYZ HIJ KNL I want to split this file at XYZ such that file1 contains lines up-to XYZ and rest of the ...
22
votes
2answers
2k views

cat gives different error when opening non-existing file

Both of the following commands try to open a non-existing file foo, but the error messages are a little different. What could be the reason? $ cat foo cat: cannot open foo $ cat < foo -bash: foo: ...
15
votes
5answers
2k views

Are `if` and `then` actually programs

I've read that semicolon is used to separate programs: $ echo 3; ls -la Does it mean that if, then and else are separate programs here? $ if [ $VARIABLE == abcdef ] ; then echo yes ; else echo no ; ...
20
votes
3answers
2k views

What does `<&-` do?

I copied a snippet of Bash to background an ssh command executed remotely: ssh user@remote <<CMD some process <&- >log 2>error & CMD What does <&- do? My guess is that ...
9
votes
6answers
10k views

Why does the command shuf file > file leave an empty file, but similar commands do not? [duplicate]

I know this is sort of a duplicate of another question (Why this sort command gives me an empty file?) but I wanted to expand on the question in response to the answers given. The command shuf ...
26
votes
1answer
5k views

Where are bash line continuations after && and || documented?

I have seen this construct in scripts a lot and used it myself, but it bothers me that I can't seem to find it in the documentation. Example: [ -f file1 ] && [ -f file2 ] && echo "...
16
votes
2answers
9k views

What does “< /dev/null” mean?

I understand > /dev/null redirects things to /dev/null which acts like a blackhole. However, I don't understand what < /dev/null means. I saw some script written like this: nohup myprogram > ...
11
votes
4answers
7k views

Run the same command multiple times in background

How is it possible to run multiple commands and background them using bash? For example: $ for i in {1..10}; do wait file$i &; done where wait is a custom binary. Right now I get an error: ...
11
votes
8answers
6k views

sed in-place line deletion on full filesystem?

Due to an application bug as yet undiagnosed, I have several hundred servers with a full disk. There is one file that has been filled up with duplicate lines—not a log file, but a user environment ...
5
votes
5answers
10k views

How to check if several files exist?

I have a shell script that looks for a file, /tmp/bbsnode1, and if the existence of that file is true, it deletes it. What I'd like to do is if multiple files exist (/tmp/bbsnode2, /tmp/bbsnode3, and /...
15
votes
4answers
1k views

Precedence of Pipe (|) and logical and (&&) in bash

The classic scenario with Operator Precedence, you have a line like : (cd ~/screenshots/ && ls screenshot* | head -n 5) And you don't know if it's parsed ((A && B) | C) or (A &&...

15 30 50 per page