0% found this document useful (0 votes)
38 views

Lab RedirectionTransformSearch

This document discusses various Linux commands for redirecting input/output streams, filtering and transforming text, and searching for files. It provides examples of using redirection characters like > and >> to output command results to files, < to use a file as stdin, and | to pipe output between commands. Commands covered include ls, sort, uniq, paste, join, cut, tr, grep, find, and wc. The document demonstrates how to select columns, convert case, remove characters, and count lines/words/bytes in files.

Uploaded by

Radu Daescu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lab RedirectionTransformSearch

This document discusses various Linux commands for redirecting input/output streams, filtering and transforming text, and searching for files. It provides examples of using redirection characters like > and >> to output command results to files, < to use a file as stdin, and | to pipe output between commands. Commands covered include ls, sort, uniq, paste, join, cut, tr, grep, find, and wc. The document demonstrates how to select columns, convert case, remove characters, and count lines/words/bytes in files.

Uploaded by

Radu Daescu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

4.

Redirection, transform text and search file

Redirect stdout to a file

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

Use file as stdin

To redirect standard input from a file instead of the keyboard use the character <:
[me@linuxbox me]$ sort < name.txt

How can we save in a file the output of the above?

Pipes and filters

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 the 10 newest file in the current directory:


[me@linuxbox me]$ ls lt | head

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

[me@linuxbox me]$ paste d, -s family


[me@linuxbox me]$ paste d, -s family name
[me@linuxbox me]$ paste -d,: family name smartphone

[me@linuxbox me]$ join state city


[me@linuxbox me]$ join i state city

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

[me@linuxbox me]$ cut d -f1 smartphone


[me@linuxbox me]$ grep /bin/bash /etc/passwd | cut d: f1,6 /etc/passwd

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

To remove all the digits from the string:


[me@linuxbox me]$ echo The PIN code is 5487 | tr d [:digit:]
The PIN code is

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

You can use -s option to squeeze the repetition of characters:


[me@linuxbox me]$ echo The PIN code is 5487 | tr s [:space:]
The PIN code is 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

[me@linuxbox me]$ grep c Samsung smartphone


[me@linuxbox me]$ cat smartphone | grep Samsung | wc -l

Finding files and directorys

Basic find commands for finding files with names:


[me@linuxbox me]$ find . name system.txt
[me@linuxbox me]$ find . iname system.txt

[me@linuxbox me]$ find . type d name class


[me@linuxbox me]$ find . type f name index.html

[me@linuxbox me]$ find . type f name *.html

[me@linuxbox me]$ find . type f perm 777


[me@linuxbox me]$ find . type f ! perm 777

[me@linuxbox me]$ find . type f name *.txt exec rm f {} \;


[me@linuxbox me]$ find . type f name .*

2
[me@linuxbox me]$ find . type f empty
[me@linuxbox me]$ find . type d -empty

[me@linuxbox me]$ find . user student


[me@linuxbox me]$ find . group linux

[me@linuxbox me]$ find . size +50 size -100

You might also like