Input/Output and error redirection in Linux: Linux basic (part 2)

HackPeas Freelancers
3 min readJul 3, 2022

Redirection in Linux:

In Linux, there is a feature of redirection, by which we can change the input/output device or destination while executing the shell command.

In Linux, there are two kinds of output streams standard output ( symbolize as “1” in commands ) and standard error ( symbolize as “2” in commands )

Redirection of output:

so currently we have a directory with files regex.txt , test2.txt and test.txt

$ ls # this command will give the list of files/directory in current folder

so we are going to redirect the output of “ls” command to a new file called output.txt

$ ls 1>output.txt

here 1 symbolizes the output of the given command and > means redirecting the output of this command to “output.txt”

Note: in Linux 1 means output, also known as standard output

as you can see, a new file is created with the name “output.txt”, which contains all the file names, including its own name too.

Redirection of error

while executing a shell command, sometimes it gives an error, for example below command

$ ls does_not_exsist.txt

it gives an error “No such file or directory”

if try to redirect the output to a file named “file.txt”, it redirects nothing,

file.txt is empty, because, there is no output generated from this command, only the error. but if we look for the below command

$ cat *

only_root.txt file can be read by root only user, so it is giving error, while file.txt is readable so it content is printed on screen,

lets redirect output to file.txt and error to error.txt

$ cat * 1>ouput.txt 2>error.txt

1> is redirecting input , while 2> redirecting error

Use of &

say we want to redirect output of 1 and 2 at the same destination

so we need to write command in this way

$ command 1>output.txt 2>&1

the above commands mean, redirect 2 to the same destination where 1 is redirected.

If you like the blog, please give me a clap and follow me for more content.

For more CTFs and bug bounty writeup, or content related to ethical hacking, android penetration testing. follow me on:

Medium: https://hackpeas.medium.com

Youtube: https://www.youtube.com/channel/UC17W_Ircv7EmIIdbJeOQ_BQ
Instagram: https://www.instagram.com/hackpeas/
Linkedin: https://www.linkedin.com/in/viraj-vaishnav-19b0a61aa/
Twitter: https://twitter.com/VirajVaishnav16

Thank You..

--

--

HackPeas Freelancers

We provide the best technical services on a reasonable budget