<<

Some Commands and Utilities

Lecture 11 COP 3363 Spring 2021

March 2, 2021 date

The date is used to display the current date and . Syntax: date The optional “format string” allows us to format the date according to our specifications. Some examples:

> date Sun Feb 7 21:44:42 EST 2021 > date +%Y-%m-%d 2021-02-07 >date +%m/%d/%y 02/07/2021 >date +%j 038 >date +"%H:%M, %B %d-%y" 22:09, February 07-21 The Unix Timestamp

I Unix measure time in the Unix Timestamp - the number of seconds elapsed since midnight on January 01, 1970 UTC.

I programming languages and servers use the as the default time - something akin to the universal truth.

I The date command can be used to generate the Unix timestamp with the +%s option. >date +%s 1612754538 The command

The cal command is used to display the current month in the format on the terminal. Syntax: cal The command also has the following options:

I For multiple months starting with the current month: cal -n number of months

I For displaying the week numbers (1-52): cal -

I For displaying the day numbers (1-365): cal -j

I For displaying the calendar month for a particular year: cal day month year Disk Usage - the command

I The du command is used to check the disk usage (in disk blocks) - the amount of space occupied by files and directories.

I Syntax: du [options] [] I Some of the options:

I du directory: Disk usage of a particular directory I du -h: Prints the disk usage in human readable form (bytes/kilobytes/megabytes) instead of disk blocks I du -s: Prints the summary (a grand total) of the disk space used by the directory I du -a: Prints the disk usage of ALL the files/directories in the given directory. I du -c: Prints the grand total of the disk usage the end after listing each individual file/directory I du [options] --exclude: Can exclude the disk usage of certain specified files/directories The command

I The head command is used to print the first few lines (usually 10 lines) of the given file to standard output.

I Syntax: head [options] [files] I Some options:

I head -n number filename: changes the number of lines printed from the default 10 to the given number I head -c number filename: Prints only the first “number” bytes from the file. I head file1 file2 ...: head can be used for multiple files. Each file’s name is printed as a header before the head output I head -q file2 ...: Same as above, except the header with the file names are suppressed The command

I The tail command is used to print the last few lines (usually 10 lines) of the given file to standard output.

I Syntax: tail [options] [files] I Some options:

I tail -n number filename: changes the number of lines printed from the default 10 to the given number I tail -c number filename: Prints only the last “number” bytes from the file I tail file1 file2 ...: tail can be used for multiple files. Each file’s name is printed as a header before the tail output I tail -q file file2 ...: Same as above, except the header with the file names are suppressed I tail -h filename: Watches for changes to the file. Prints the last few lines every time the file changes. Commonly used to log files in real time. The utility

I The cut command is used to “slice” columnar sections of a file I Syntax: cut [options] filename I Some Options: I cut -c start-end filename: Cuts each line of the file from the “start” position until the “end” position. I cut -b start end filename: Cuts by bytes instead of characters. I cut -f field numbers -d delimiter filename: Separates the lines into fields using the given delimiter and then prints the specified fields. I cut [options] --complement filename: Prints the complement - the parts of the files that would have been left out in the original cut command. I cut [options] --output-delimiter new dl filename: Changes the output delimiter from space to the given delimiter I The original file is not changed unless specified. The cut output is just that - output The utility

I The sort command is used to sort the contents of the given file. I The sorting is done in lexicographic order - where characters are ordered by their ASCII value. In lexicographic order, spaces come first, then numbers(0-9), uppercase characters (A-z) and then lowercase characters (a-z). I Syntax: sort [options] filename I Some Options: I sort -r filename: Sorts in reverse order I sort -n filename: Sorts numerical content as numbers instead of lexicographic order I sort -f filename: Ignores case while sorting I sort -c filename: Checks if a file is already sorted. Prints the lines that are not in order. The file is already sorted if there is no output. I sort -u filename: Sorts the file contents and removes the duplicates. I The original file is not changed unless specified. Bundling Files into an Archive

I Tar is a utility for creating and extracting archives. It is very useful for archiving files on disk, sending a set of files over the network, and for compactly making backups

I General Syntax: tar options filenames I Commonly Used Options:

I -c insert files into a tar file I -f use the name of the tar file that is specified I -v output the name of each file as it is inserted into or extracted from a tar file I -x extract the files from a tar file I -t list contents of an archive I -z gzip / gunzip if necessary Creating an Archive with tar

I The typical command used to create an archive from a set of files is illustrated below.

I Note that each specified filename can also be a directory.

I Tar will insert all files in that directory and any subdirectories. The f flag is used to create an archive file with a specific name (usually named with an extension of .tar). I Examples:

I tar -cvf proj.tar proj If proj is a directory this will insert the directory and all files and subdirectories (recursively) with the name of the archive being proj.tar Note that if proj.tar already existed it will simply be overwritten; previous information will be lost. I tar -cvf prog.tar *.c *.h All files ending in .c or .h will be archived in prog.tar Extracting files from a tar archive

I The typical tar command used to extract the files from a tar archive is illustrated below.

I The extracted files have the same name, permissions, and directory structure as the original files.

I If they are opened by another user (archive sent by email) the user id becomes that of the user opening the tar archive. I Examples:

I tar -xvf proj.tar Extract all files from proj.tar into the current directory. Note that proj.tar remains in the current directory I tar -xvzf proj.tar.gz Extract files but also unzip files in the process The gzip/gunzip command

I this is one of the compression utilities that reduces the size of a file to take up space on your drive. It should be used with some care.

I The compressed file has an extension of .gz I Examples:

I gzip bigfile compresses file, flag -v can be used for verbose mode to give information. Note: original file is gone! I gzip -d bigfile.gz Restores a .gz file I gunzip bigfile.gz Same as above

I When you experiment with this use copies of files rather than the originals until you are comfortable with how gzip works! The command

I diff compares two text files ( can also be used on directories) and prints the lines for the files differ.

I Syntax: diff [options] file1 file2 I Some options:

I -b Treats groups of spaces as one I -i Ignores case I -r Includes directories in comparison I -w Ignores all spaces and tabs

I Example: diff -w testprog1.c testprog2.c The command

I Compares two files byte by byte and tells you where they differ.

I Generally used for binary and executable files as opposed to diff which is used for text files.

I Syntax: cmp options file1 file2

I Example: cmp myfile1.o myfile2.o The command

I grep is a very useful utility that searches files for a particular pattern.

I The pattern can be a word, a string enclosed in single quotes, or a .

I Syntax: grep options pattern files I grep has many options; a few are noted below

I -i Ignore case I -n Display line numbers I -l Display only the names of the files and not the actual lines I -P pattern is a Perl regular expression, not a Unix regular expression

I The way regular expressions work is somewhat complicated and is outside the scope of this course. If you need more information, please contact the instructor. Some grep Examples

I grep int *.c find all occurrences of the pattern int in all files with a .c extension

I grep ‘main()’ testprog1.c enclosing the pattern in quotes is useful when using special characters

I grep ‘m.*n’ myfile The . matches a single character, the .* matches any number of characters; this finds anything starting with an m and ending with an n Some grep patterns

I Bracketed Expressions

I [1357] matches 1 or 3 or 5 or 7 I [∧1357] with a beginning ∧ matches not (1,3,5,7) I Range Expressions

I [b-g] matches b, c, d, e, f, g I Named classes of expressions

I [:digit:], [:alnum:] I Special symbols

I ? The preceding item is optional and matched at most once. I * The preceding item will be matched zero or more times. I + The preceding item will be matched one or more times. I . This matches any single character More grep expressions

I Matching at the beginning and end

I ∧ matches the beginning of the line, thus ∧#include would macth any lines with a #include at the beginning of the line. I $ matches the end of line I \< matches the beginning of a word I \> matches the end of a word I | - The or operator

I grep | dog Unix manpages

I The man command is used to display the manual for any of the Unix utilities/commands - also called a manpage

I The manual includes a description of the command, the options, the status(es), return value(s), errors, files, actions, examples, etc.

I Syntax: man command I The man command also comes with a few options:

I To show a certain section of the manual: man section number command I To show only the synopsis of the command: man -f command I To show all the manuals that contain a reference to a command: man -k command