1 / 9

Linux Tools

Linux Tools. Tar. tar – (Tape Archive). This is a tool for archiving files and directory hierarchies. tar output can be sent to stdout using the – file name tar can read input from stdin. tar cf myarchive.tar target_directory # archives recursively the target

finnea
Download Presentation

Linux Tools

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linux Tools

  2. Tar • tar – (Tape Archive). This is a tool for archiving files and directory hierarchies. • tar output can be sent to stdout using the – file name • tar can read input from stdin tar cf myarchive.tar target_directory # archives recursively the target tar xf myarchive.tar # expands the archive into the current dir tar tf myarchive.tar # lists the archive contents tar cfz ... # tars and gzips tar cf – MyDir # tars the MyDir directory and sends all output to stdout tar cf – MyDir # tars the MyDir directory and sends all output to stdout tar cf – MyDir # tars the MyDir directory and sends all output to stdout tar cf – MyDir # tars the MyDir directory and sends all output to stdout tar xf – # extracts input from stdin and expands data in current directory

  3. Tar • What does the following do? • cd SrcDir: Changes directory to a source directory • ;: allows sequential execution of a second command on the same command line • |: allows concurrent execution of a second command on the same command line; stdout of command on the left of | is redirected to stdin of command on the right of |. • (): combines two or more commands into a single command for purposes of coupling stdin and stdout of both commands. Given (a;b), programs a and b will share the same stdin and stdio streams cd SrcDir; tar cf - | (cd DestDir; tar xf - .)

  4. grep • grep searches lines of text files from matches to a search string; matching lines are output to stdout. • grep also excludes lines grep abc myfile # finds all lines in myfile that contain 'abc' grep 'ab*c' myfile # finds all lines in myfile that contain 'ab' followed later by a 'c' grep -v xyz myfile # shows all lines except those containing 'xyz'

  5. grep • Find the lines in a file that contain 'abc' but do not contain 'xyz'. Use only grep and |.

  6. sed • sed allows text strings in text files to be replaced by other text strings • sed, by default, prints all input lines with or without substitutions • sed -n prints out no lines • sed -n s/old/new/p prints only changed lines. sed -n s/aba/b/p myfile # replaces 'aba' with 'b' and prints out only lines affected

  7. awk • awk tokenizes a line of text and allows you to use the tokens to build new strings. Writes to stdout. Default input is stdin. • awk does what ever is inside {} to every line. It does BEGIN{} once at the beginning and END{} once at the end. awk 'BEGIN{FS=":"}{printf "%s is a user\n",$1;}' /etc/passwd # awk tokenizes the lines of /etc/passwd using “:” as a field separator; # it then prints out the first token found as part of a sentence.

  8. awk • Use awk to add line numbers to a file awk 'BEGIN{i=1;}{printf”%s: %s”,i, $0;i=i+1;}' myfile

  9. Homework: • Write an awk command that will add line numbers to an existing file without changing the file name. Use multiple commands on the same line to accomplish this. • Write a command that will use grep and sed to relace all instances of abc with xyz except in those lines that contain rst. Output should go to stdout.

More Related