Lab RedirectionTransformSearch
Lab RedirectionTransformSearch
To redirect standard output to a file, you must use the > character like this:
[me@linuxbox me]$ ls l > ls_output
The ls command is executed and the resuls are written in a file ls_output and no results appear on the display. Each
time the command above is repeated, ls_output is overwritten.
If you want the new results to be appended to the file, use >>:
[me@linuxbox me]$ ls l >> ls_output
To redirect standard input from a file instead of the keyboard use the character <:
[me@linuxbox me]$ sort < name.txt
With pipes, the standard output of one command is fed into the standard input of another. By using this | less trick,
you can make any command have scrolling output.
[me@linuxbox me]$ ls -l | less
Display total number of files in the current working directory and all of its subdirectories:
[me@linuxbox me]$ find . type f -print | wc -l
Other example is for printing the content of file from command line. Linux provides a program called lpr that accepts
standard input and sends it to the printer.
[me@linuxbox me]$ cat file_to_print.txt | sort | uniq | pr | lpr
First, cat sends the list into sort which sorts it and feeds it into uniq which removes any duplicates. Next pr and lpr
are used to paginate and print the list.
Try the following and compare their results:
[me@linuxbox me]$ paste family name
[me@linuxbox me]$ paste d- family name
1
You can use cut command to extract portion of text from a file by selecting columns:
[me@linuxbox me]$ cut c2 name
[me@linuxbox me]$ cut c2-4 name
[me@linuxbox me]$ cut c-8 name
The folloing tr command is used to convert the lower case to upper case:
[me@linuxbox me]$ tr abcdefghijklmnopqrstuwxyz ABCDEFGHIJKLMNOPQRSTUWXYZ
[me@linuxbox me]$ tr [:lower:] [::upper]
[me@linuxbox me]$ tr a-z A-Z
Or, to remove all characters except digits, you can use the following:
[me@linuxbox me]$ echo The PIN code is 5487 | tr cd [:digit:]
5487
To print number of newline, words and bytes in files use the wc command. Compare this examples:
[me@linuxbox me]$ wc smartphone
15 49 254 smartphone
2
[me@linuxbox me]$ find . type f empty
[me@linuxbox me]$ find . type d -empty