100 likes | 252 Views
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
E N D
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
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 - .)
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'
grep • Find the lines in a file that contain 'abc' but do not contain 'xyz'. Use only grep and |.
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
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.
awk • Use awk to add line numbers to a file awk 'BEGIN{i=1;}{printf”%s: %s”,i, $0;i=i+1;}' myfile
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.