Posted: . At: 11:30 AM. This was 2 years ago. Post ID: 2672
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.

Using redirection on the UNIX command line.

Using redirection on the command line is very easy to do. The main strength of the UNIX command line is that you may output data from one command-line application to another. Here is one example.

ls -hula | less

This is using the pipe character to pipe the output of the ls(1) command to the less command, which will allow you to scroll through the output. To redirect the output of a program to a file, this command is used.

ps > psout.txt

To redirect the verbose console output of a file to another location when you are running a program from a terminal, this command line will work very well indeed. This redirects the stderr stream.

gedit 2> /dev/null

if you redirect the output to a file, then you may read the contents after you are finished with the program in question. Here we are redirecting the output of both stdout and stderr to the bit bucket /dev/null.

gedit &> /dev/null

Using piping to redirect the contents of a text file and parse it with sed(1).

cat blog.txt | sed "s/tHe/The/gi;"

Another redirection method to use with the bash shell. This is piping the output of one Linux command and then appending it into the output of another command.

┌──(john㉿DESKTOP-PF01IEE)-[~]
└─$ echo "Hello World" >| echo "Me" > out.txt

This is the result of this command.

┌──(john㉿DESKTOP-PF01IEE)-[~]
└─$ cat out.txt 
Hello World Me

There are more verbose explanations of this procedure on this website. The Linux Documentation Project. http://tldp.org/LDP/abs/html/io-redirection.html.

This is another interesting shell trick. Separate the parameters of a Linux command with single quotes.

┌──(john㉿DESKTOP-PF01IEE)-[~]
└─$ ls -h'u'l'a'
total 500K
drwxr-xr-x 11 john john 4.0K Feb  9 10:38 .
drwxr-xr-x  3 root root 4.0K Feb  8 06:21 ..
-rw-------  1 john john  44K Feb  9 10:32 .bash_history
-rw-r--r--  1 john john  220 Feb  8 16:58 .bash_logout
-rw-r--r--  1 john john 5.3K Feb  9 10:29 .bashrc
-rw-r--r--  1 john john 3.5K Jan  6 07:29 .bashrc.original
drwx------  3 john john 4.0K Feb  8 07:36 .BurpSuite
drwx------  3 john john 4.0K Jan  6 07:28 .cache
drwxr-xr-x  7 john john 4.0K Jan  6 07:28 .config
drwxr-xr-x  2 john john 4.0K Feb  2 08:17 Documents
drwxr-xr-x  3 john john 4.0K Feb  2 09:33 Downloads
-rw-r--r--  1 john john    0 Feb  9 10:32 echo
-rw-r--r--  1 john john   10 Feb  9 10:36 foo.sh
-rwxr-xr-x  1 john john  165 Feb  4 10:20 images.py
-rw-r--r--  1 john john  14K Jan  6 07:29 index.html
drwxr-xr-x  4 john john 4.0K Jan  6 07:28 .java
-rw-------  1 john john   20 Feb  3 11:26 .lesshst
drwx------  4 john john 4.0K Jan  6 07:29 .local
-rw-r--r--  1 john john 221K Jan 30 17:02 mpv-shot0001.jpg
-rw-r--r--  1 john john   15 Feb  9 10:32 out.txt
drwx------  3 john john 4.0K Jan 19 07:29 .pki
-rw-r--r--  1 john john  807 Feb  9 10:29 .profile
-rw-r--r--  1 john john   72 Feb  4 10:08 .selected_editor
-rw-r--r--  1 john john    0 Jan 18 08:25 tmp.txt
-rw-------  1 john john 9.3K Feb  4 10:07 .viminfo
drwxr-xr-x  3 john john 4.0K Feb  4 11:43 .wpscan
-rw-rw-rw-  1 john john  480 Jan 18 07:35 .Xresources
-rw-r--r--  1 john john  12K Jan 18 07:38 xterm.2022.01.18.07.38.02.svg
-rw-r--r--  1 john john  51K Jan 18 07:45 xterm.2022.01.18.07.45.16.svg
-rw-r--r--  1 john john  34K Jan 18 07:46 xterm.2022.01.18.07.46.06.xhtml
-rw-r--r--  1 john john  11K Jan  6 07:28 .zshrc

Yet another very strange piping method. This one is abusing piping and redirection.

┌──(john㉿DESKTOP-PF01IEE)-[~]
└─$ echo "Hello World" >| echo "Me" >|hello.txt

This is the output of this command.

┌──(john㉿DESKTOP-PF01IEE)-[~]
└─$ cat hello.txt 
Hello World Me

Useless maybe, but interesting nonetheless.

https://securitronlinux.com/bejiitaswrath/strange-bash-piping-method-that-actually-does-work-and-fixing-hard-disk-partitions-with-fsck/.

1 thought on “Using redirection on the UNIX command line.”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.