Linux "redirects"

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,526
Reaction score
3,292
Credits
31,543

Understanding Linux Redirects and Related Commands​

Linux provides powerful tools for redirecting input and output, allowing you to control where data goes and comes from. Here are some of the most commonly used redirection operators and commands:

1. Standard Output (> and >>)​

  • >: Redirects standard output to a file, overwriting the file if it exists.
    Code:
    echo "Hello, World!" > output.txt

  • >>: Appends standard output to a file, preserving the existing content.
    Code:
    echo "Hello again!" >> output.txt

2. Standard Input (<)​

  • <: Redirects standard input from a file.
    Code:
    cat < input.txt

3. Pipe (|)​

  • |: Passes the output of one command as input to another command.
    Code:
    ls -l | grep "txt"

4. Background Execution (&)​

  • &: Runs a command in the background.
    Code:
    sleep 60 &

5. Logical AND (&&)​

  • &&: Executes the second command only if the first command succeeds.
    Code:
    mkdir new_directory && cd new_directory

6. nohup​

  • nohup: Runs a command immune to hangups, with output redirected to nohup.out by default.
    Code:
    nohup long_running_command &

7. Standard Output and Error (stdout and stderr)​

  • stdout: Standard output, usually the terminal.
  • stderr: Standard error, used for error messages.
You can redirect both stdout and stderr to a file:

Code:
command > output.txt 2>&1

Or redirect them separately:

Code:
command > stdout.txt 2> stderr.txt
 


Key to know is that rules of Linux (and Unix) would state that 1 (normal output) has all the normal text,
and 2 (error output) ONLY has the error output.

command > stdout.txt 2> stderr.txt

Is a great command to check if that is respected. But, you must be looking at a command call that has something going wrong ! If all works ... you won't see a lot. The error output then typically is empty,
 
Can you state a case of where you would want to run this exact command:

Delaying Execution in Scripts: When writing shell scripts, you might want to introduce a delay between commands. For instance, if you need to wait for a service to start before running another command, you can use sleep 60 & to delay the execution by 60 seconds.
Background Processes: The & at the end of the command runs sleep 60 in the background, allowing you to continue using the terminal for other tasks while the delay is in effect. This is useful if you want to run multiple commands simultaneously without waiting for each one to finish.
Automated Tasks: In automation scripts, you might need to pause for a specific duration before proceeding to the next task. For example, you can use sleep 60 & to wait for a minute before checking the status of a process or service
Rate Limiting: If you're running a script that makes network requests or performs actions that need to be rate-limited, you can use sleep 60 & to introduce a delay between requests to avoid overwhelming the server or service
 
Delaying Execution in Scripts: When writing shell scripts, you might want to introduce a delay between commands. For instance, if you need to wait for a service to start before running another command, you can use sleep 60 & to delay the execution by 60 seconds.
Background Processes: The & at the end of the command runs sleep 60 in the background, allowing you to continue using the terminal for other tasks while the delay is in effect. This is useful if you want to run multiple commands simultaneously without waiting for each one to finish.
Automated Tasks: In automation scripts, you might need to pause for a specific duration before proceeding to the next task. For example, you can use sleep 60 & to wait for a minute before checking the status of a process or service
Rate Limiting: If you're running a script that makes network requests or performs actions that need to be rate-limited, you can use sleep 60 & to introduce a delay between requests to avoid overwhelming the server or service

No, that is incorrect.
Using "sleep" indeed creates a delay ... but NOT when you fork the process into the background !

Where did you get this exact text from ? You should note they don't understand how process forking works ..

If you use SLEEP with a & at the end, it IMMEDIATELY returns to the prompt, or the next command, and thus doesn't wait.

It does wait as a process, but as a forked process on which NO OTHER PROCESS is waiting. It just completes (60 seconds later) and then just completes itself, without ANY interaction with the process it was spawned from.

That was the reason I asked you the question, but it missed the point.
 
That was the reason I asked you the question, but it missed the point.

I stand corrected. That may an impractical example, but the point was, it puts the process in the background.
 
I stand corrected.

Well, don't forget to tell these guys they don't understand how either & or sleep works
The "sleep" command is one of the few commands that make zero sense to use in the background.
I mean, you CAN ... but you gotta use some next level programming like evaluating process ID's and such, I think of tools that use multi-level locking systems and evalutate time-outs and such. And even then.

If you just want to use SLEEP, don't use &

And run these commands to understand what really happens:

date
sleep 60 &
date



In the text displayed in that post, there is more than 1 thing that is totally incorrect. They show that they don't understand how SLEEP and & works. If ever you want to use & you use it with a tool that actually does something, "sleep" is the actual worst example I can think of.
 
Last edited:
Code:
#!/bin/bash

# Start a background sleep process
sleep 60 &

# Capture the PID of the background process
SLEEP_PID=$!

# Continue with other tasks
echo "Starting other tasks..."

# Simulate other tasks with a loop
for i in {1..5}; do
    echo "Task $i"
    sleep 1
done

# Wait for the background sleep process to complete
wait $SLEEP_PID

echo "Background sleep process completed."
 
Code:
#!/bin/bash

# Start a background sleep process
sleep 60 &

# Capture the PID of the background process
SLEEP_PID=$!

# Continue with other tasks
echo "Starting other tasks..."

# Simulate other tasks with a loop
for i in {1..5}; do
    echo "Task $i"
    sleep 1
done

# Wait for the background sleep process to complete
wait $SLEEP_PID

echo "Background sleep process completed."

Yeah, couldn't be simplier !
 
It might be worth mentioning here that stdout and stderr can be redirected with a pipe | by using |& much like >& would redirect output to a file.

Signed,

Matthew Campbell
 



Top