Some More Unix Commands and Utilities

Total Page:16

File Type:pdf, Size:1020Kb

Some More Unix Commands and Utilities Some More Unix Commands and Utilities Lecture 11 COP 3363 Spring 2021 March 2, 2021 date The date command is used to display the current date and time. 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 Most programming languages and servers use the Unix time 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 cal command The cal command is used to display the current month in the calendar 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 -w 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 du 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] [directory] 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 at the end after listing each individual file/directory I du [options] --exclude: Can exclude the disk usage of certain specified files/directories The head 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 file file2 ...: Same as above, except the header with the file names are suppressed The tail 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 watch log files in real time. The cut 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 sort 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 less 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 diff command I diff compares two text files ( can also be used on directories) and prints the lines for which 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 cmp 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 grep 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 regular expression. 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.
Recommended publications
  • UNIX Cheat Sheet – Sarah Medland Help on Any Unix Command List a Directory Change to Directory Make a New Directory Remove A
    THE 2013 INTERNATIONAL WORKSHOP ON STATISTICAL METHODOLOGY FOR HUMAN GENOMIC STUDIES UNIX cheat sheet – Sarah Medland Help on any Unix command man {command} Type man ls to read the manual for the ls command. which {command} Find out where a program is installed whatis {command} Give short description of command. List a directory ls {path} ls -l {path} Long listing, with date, size and permisions. ls -R {path} Recursive listing, with all subdirs. Change to directory cd {dirname} There must be a space between. cd ~ Go back to home directory, useful if you're lost. cd .. Go back one directory. Make a new directory mkdir {dirname} Remove a directory/file rmdir {dirname} Only works if {dirname} is empty. rm {filespec} ? and * wildcards work like DOS should. "?" is any character; "*" is any string of characters. Print working directory pwd Show where you are as full path. Copy a file or directory cp {file1} {file2} cp -r {dir1} {dir2} Recursive, copy directory and all subdirs. cat {newfile} >> {oldfile} Append newfile to end of oldfile. Move (or rename) a file mv {oldfile} {newfile} Moving a file and renaming it are the same thing. View a text file more {filename} View file one screen at a time. less {filename} Like more , with extra features. cat {filename} View file, but it scrolls. page {filename} Very handy with ncftp . nano {filename} Use text editor. head {filename} show first 10 lines tail {filename} show last 10 lines Compare two files diff {file1} {file2} Show the differences. sdiff {file1} {file2} Show files side by side. Other text commands grep '{pattern}' {file} Find regular expression in file.
    [Show full text]
  • Types and Programming Languages by Benjamin C
    < Free Open Study > . .Types and Programming Languages by Benjamin C. Pierce ISBN:0262162091 The MIT Press © 2002 (623 pages) This thorough type-systems reference examines theory, pragmatics, implementation, and more Table of Contents Types and Programming Languages Preface Chapter 1 - Introduction Chapter 2 - Mathematical Preliminaries Part I - Untyped Systems Chapter 3 - Untyped Arithmetic Expressions Chapter 4 - An ML Implementation of Arithmetic Expressions Chapter 5 - The Untyped Lambda-Calculus Chapter 6 - Nameless Representation of Terms Chapter 7 - An ML Implementation of the Lambda-Calculus Part II - Simple Types Chapter 8 - Typed Arithmetic Expressions Chapter 9 - Simply Typed Lambda-Calculus Chapter 10 - An ML Implementation of Simple Types Chapter 11 - Simple Extensions Chapter 12 - Normalization Chapter 13 - References Chapter 14 - Exceptions Part III - Subtyping Chapter 15 - Subtyping Chapter 16 - Metatheory of Subtyping Chapter 17 - An ML Implementation of Subtyping Chapter 18 - Case Study: Imperative Objects Chapter 19 - Case Study: Featherweight Java Part IV - Recursive Types Chapter 20 - Recursive Types Chapter 21 - Metatheory of Recursive Types Part V - Polymorphism Chapter 22 - Type Reconstruction Chapter 23 - Universal Types Chapter 24 - Existential Types Chapter 25 - An ML Implementation of System F Chapter 26 - Bounded Quantification Chapter 27 - Case Study: Imperative Objects, Redux Chapter 28 - Metatheory of Bounded Quantification Part VI - Higher-Order Systems Chapter 29 - Type Operators and Kinding Chapter 30 - Higher-Order Polymorphism Chapter 31 - Higher-Order Subtyping Chapter 32 - Case Study: Purely Functional Objects Part VII - Appendices Appendix A - Solutions to Selected Exercises Appendix B - Notational Conventions References Index List of Figures < Free Open Study > < Free Open Study > Back Cover A type system is a syntactic method for automatically checking the absence of certain erroneous behaviors by classifying program phrases according to the kinds of values they compute.
    [Show full text]
  • Medi-Cal Dental EDI How-To Guide
    ' I edi Cal Dental _ Electronic Data Interchange HOW-TO GUIDE EDI EDI edi EDI Support Group Phone: (916) 853-7373 Email: [email protected] Revised November 2019 , ] 1 ,edi .. Cal Dental Medi-Cal Dental Program EDI How-To Guide 11/2019 Welcome to Medical Dental Program’s Electronic Data Interchange Program! This How-To Guide is designed to answer questions providers may have about submitting claims electronically. The Medi-Cal Dental Program's Electronic Data Interchange (EDI) program is an efficient alternative to sending paper claims. It will provide more efficient tracking of the Medi-Cal Dental Program claims with faster responses to requests for authorization and payment. Before submitting claims electronically, providers must be enrolled as an EDI provider to avoid rejection of claims. To enroll, providers must complete the Medi-Cal Dental Telecommunications Provider and Biller Application/Agreement (For electronic claim submission), the Provider Service Office Electronic Data Interchange Option Selection Form and Electronic Remittance Advice (ERA) Enrollment Form and return them to the address indicated on those forms. Providers should advise their software vendor that they would like to submit Medi-Cal Dental Program claims electronically, and if they are not yet enrolled in the EDI program, an Enrollment Packet should be requested from the EDI Support department. Enrollment forms are also available on the Medi-Cal Dental Program Web site (www.denti- cal.ca.gov) under EDI, located on the Providers tab. Providers may also submit digitized images of documentation to the Medi-Cal Dental Program. If providers choose to submit conventional radiographs and attachments through the mail, an order for EDI labels and envelopes will need to be placed using the Forms Reorder Request included in the Enrollment Packet and at the end of this How-To Guide.
    [Show full text]
  • A High-Level Programming Language for Multimedia Streaming
    Liquidsoap: a High-Level Programming Language for Multimedia Streaming David Baelde1, Romain Beauxis2, and Samuel Mimram3 1 University of Minnesota, USA 2 Department of Mathematics, Tulane University, USA 3 CEA LIST – LMeASI, France Abstract. Generating multimedia streams, such as in a netradio, is a task which is complex and difficult to adapt to every users’ needs. We introduce a novel approach in order to achieve it, based on a dedi- cated high-level functional programming language, called Liquidsoap, for generating, manipulating and broadcasting multimedia streams. Unlike traditional approaches, which are based on configuration files or static graphical interfaces, it also allows the user to build complex and highly customized systems. This language is based on a model for streams and contains operators and constructions, which make it adapted to the gen- eration of streams. The interpreter of the language also ensures many properties concerning the good execution of the stream generation. The widespread adoption of broadband internet in the last decades has changed a lot our way of producing and consuming information. Classical devices from the analog era, such as television or radio broadcasting devices have been rapidly adapted to the digital world in order to benefit from the new technologies available. While analog devices were mostly based on hardware implementations, their digital counterparts often consist in software implementations, which po- tentially offers much more flexibility and modularity in their design. However, there is still much progress to be done to unleash this potential in many ar- eas where software implementations remain pretty much as hard-wired as their digital counterparts.
    [Show full text]
  • A Brief Introduction to Unix-2019-AMS
    Brief Intro to Linux/Unix Brief Intro to Unix (contd) A Brief Introduction to o Brief History of Unix o Compilers, Email, Text processing o Basics of a Unix session o Image Processing Linux/Unix – AMS 2019 o The Unix File System Pete Pokrandt o Working with Files and Directories o The vi editor UW-Madison AOS Systems Administrator o Your Environment [email protected] o Common Commands Twitter @PTH1 History of Unix History of Unix History of Unix o Created in 1969 by Kenneth Thompson and Dennis o Today – two main variants, but blended o It’s been around for a long time Ritchie at AT&T o Revised in-house until first public release 1977 o System V (Sun Solaris, SGI, Dec OSF1, AIX, o It was written by computer programmers for o 1977 – UC-Berkeley – Berkeley Software Distribution (BSD) linux) computer programmers o 1983 – Sun Workstations produced a Unix Workstation o BSD (Old SunOS, linux, Mac OSX/MacOS) o Case sensitive, mostly lowercase o AT&T unix -> System V abbreviations 1 Basics of a Unix Login Session Basics of a Unix Login Session Basics of a Unix Login Session o The Shell – the command line interface, o Features provided by the shell o Logging in to a unix session where you enter commands, etc n Create an environment that meets your needs n login: username n Some common shells n Write shell scripts (batch files) n password: tImpAw$ n Define command aliases (this Is my password At work $) Bourne Shell (sh) OR n Manipulate command history IHateHaving2changeMypasswordevery3weeks!!! C Shell (csh) n Automatically complete the command
    [Show full text]
  • Managing Files with Sterling Connect:Direct File Agent 1.4.0.1
    Managing Files with Sterling Connect:Direct File Agent 1.4.0.1 IBM Contents Managing Files with Sterling Connect:Direct File Agent.......................................... 1 Sterling Connect:Direct File Agent Overview.............................................................................................. 1 How to Run Sterling Connect:Direct File Agent.......................................................................................... 2 Sterling Connect:Direct File Agent Logging.................................................................................................3 Sterling Connect:Direct File Agent Configuration Planning........................................................................ 3 Sterling Connect:Direct File Agent Worksheet ...........................................................................................4 Considerations for a Large Number of Watch Directories.......................................................................... 6 Modifying MaxFileSize............................................................................................................................ 6 Modifying MaxBackupIndex...................................................................................................................6 Considerations for a Large Number of Files in a Watch Directory..............................................................7 Sterling Connect:Direct File Agent Configuration Scenarios...................................................................... 7 Scenario:Detecting
    [Show full text]
  • “Linux at the Command Line” Don Johnson of BU IS&T  We’Ll Start with a Sign in Sheet
    “Linux at the Command Line” Don Johnson of BU IS&T We’ll start with a sign in sheet. We’ll end with a class evaluation. We’ll cover as much as we can in the time allowed; if we don’t cover everything, you’ll pick it up as you continue working with Linux. This is a hands-on, lab class; ask questions at any time. Commands for you to type are in BOLD The Most Common O/S Used By BU Researchers When Working on a Server or Computer Cluster Linux is a Unix clone begun in 1991 and written from scratch by Linus Torvalds with assistance from a loosely-knit team of hackers across the Net. 64% of the world’s servers run some variant of Unix or Linux. The Android phone and the Kindle run Linux. a set of small Linux is an O/S core programs written by written by Linus Richard Stallman and Torvalds and others others. They are the AND GNU utilities. http://www.gnu.org/ Network: ssh, scp Shells: BASH, TCSH, clear, history, chsh, echo, set, setenv, xargs System Information: w, whoami, man, info, which, free, echo, date, cal, df, free Command Information: man, info Symbols: |, >, >>, <, ;, ~, ., .. Filters: grep, egrep, more, less, head, tail Hotkeys: <ctrl><c>, <ctrl><d> File System: ls, mkdir, cd, pwd, mv, touch, file, find, diff, cmp, du, chmod, find File Editors: gedit, nedit You need a “xterm” emulation – software that emulates an “X” terminal and that connects using the “SSH” Secure Shell protocol. ◦ Windows Use StarNet “X-Win32:” http://www.bu.edu/tech/support/desktop/ distribution/xwindows/xwin32/ ◦ Mac OS X “Terminal” is already installed Why? Darwin, the system on which Apple's Mac OS X is built, is a derivative of 4.4BSD-Lite2 and FreeBSD.
    [Show full text]
  • Unix/Linux Command Reference
    Unix/Linux Command Reference .com File Commands System Info ls – directory listing date – show the current date and time ls -al – formatted listing with hidden files cal – show this month's calendar cd dir - change directory to dir uptime – show current uptime cd – change to home w – display who is online pwd – show current directory whoami – who you are logged in as mkdir dir – create a directory dir finger user – display information about user rm file – delete file uname -a – show kernel information rm -r dir – delete directory dir cat /proc/cpuinfo – cpu information rm -f file – force remove file cat /proc/meminfo – memory information rm -rf dir – force remove directory dir * man command – show the manual for command cp file1 file2 – copy file1 to file2 df – show disk usage cp -r dir1 dir2 – copy dir1 to dir2; create dir2 if it du – show directory space usage doesn't exist free – show memory and swap usage mv file1 file2 – rename or move file1 to file2 whereis app – show possible locations of app if file2 is an existing directory, moves file1 into which app – show which app will be run by default directory file2 ln -s file link – create symbolic link link to file Compression touch file – create or update file tar cf file.tar files – create a tar named cat > file – places standard input into file file.tar containing files more file – output the contents of file tar xf file.tar – extract the files from file.tar head file – output the first 10 lines of file tar czf file.tar.gz files – create a tar with tail file – output the last 10 lines
    [Show full text]
  • Text Editing in UNIX: an Introduction to Vi and Editing
    Text Editing in UNIX A short introduction to vi, pico, and gedit Copyright 20062009 Stewart Weiss About UNIX editors There are two types of text editors in UNIX: those that run in terminal windows, called text mode editors, and those that are graphical, with menus and mouse pointers. The latter require a windowing system, usually X Windows, to run. If you are remotely logged into UNIX, say through SSH, then you should use a text mode editor. It is possible to use a graphical editor, but it will be much slower to use. I will explain more about that later. 2 CSci 132 Practical UNIX with Perl Text mode editors The three text mode editors of choice in UNIX are vi, emacs, and pico (really nano, to be explained later.) vi is the original editor; it is very fast, easy to use, and available on virtually every UNIX system. The vi commands are the same as those of the sed filter as well as several other common UNIX tools. emacs is a very powerful editor, but it takes more effort to learn how to use it. pico is the easiest editor to learn, and the least powerful. pico was part of the Pine email client; nano is a clone of pico. 3 CSci 132 Practical UNIX with Perl What these slides contain These slides concentrate on vi because it is very fast and always available. Although the set of commands is very cryptic, by learning a small subset of the commands, you can edit text very quickly. What follows is an outline of the basic concepts that define vi.
    [Show full text]
  • What Is UNIX? the Directory Structure Basic Commands Find
    What is UNIX? UNIX is an operating system like Windows on our computers. By operating system, we mean the suite of programs which make the computer work. It is a stable, multi-user, multi-tasking system for servers, desktops and laptops. The Directory Structure All the files are grouped together in the directory structure. The file-system is arranged in a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally called root (written as a slash / ) Basic commands When you first login, your current working directory is your home directory. In UNIX (.) means the current directory and (..) means the parent of the current directory. find command The find command is used to locate files on a Unix or Linux system. find will search any set of directories you specify for files that match the supplied search criteria. The syntax looks like this: find where-to-look criteria what-to-do All arguments to find are optional, and there are defaults for all parts. where-to-look defaults to . (that is, the current working directory), criteria defaults to none (that is, select all files), and what-to-do (known as the find action) defaults to ‑print (that is, display the names of found files to standard output). Examples: find . –name *.txt (finds all the files ending with txt in current directory and subdirectories) find . -mtime 1 (find all the files modified exact 1 day) find . -mtime -1 (find all the files modified less than 1 day) find . -mtime +1 (find all the files modified more than 1 day) find .
    [Show full text]
  • LINUX INTERNALS LABORATORY III. Understand Process
    LINUX INTERNALS LABORATORY VI Semester: IT Course Code Category Hours / Week Credits Maximum Marks L T P C CIA SEE Total AIT105 Core - - 3 2 30 70 100 Contact Classes: Nil Tutorial Classes: Nil Practical Classes: 36 Total Classes: 36 OBJECTIVES: The course should enable the students to: I. Familiar with the Linux command-line environment. II. Understand system administration processes by providing a hands-on experience. III. Understand Process management and inter-process communications techniques. LIST OF EXPERIMENTS Week-1 BASIC COMMANDS I Study and Practice on various commands like man, passwd, tty, script, clear, date, cal, cp, mv, ln, rm, unlink, mkdir, rmdir, du, df, mount, umount, find, unmask, ulimit, ps, who, w. Week-2 BASIC COMMANDS II Study and Practice on various commands like cat, tail, head , sort, nl, uniq, grep, egrep,fgrep, cut, paste, join, tee, pg, comm, cmp, diff, tr, awk, tar, cpio. Week-3 SHELL PROGRAMMING I a) Write a Shell Program to print all .txt files and .c files. b) Write a Shell program to move a set of files to a specified directory. c) Write a Shell program to display all the users who are currently logged in after a specified time. d) Write a Shell Program to wish the user based on the login time. Week-4 SHELL PROGRAMMING II a) Write a Shell program to pass a message to a group of members, individual member and all. b) Write a Shell program to count the number of words in a file. c) Write a Shell program to calculate the factorial of a given number.
    [Show full text]
  • Unix (And Linux)
    AWK....................................................................................................................................4 BC .....................................................................................................................................11 CHGRP .............................................................................................................................16 CHMOD.............................................................................................................................19 CHOWN ............................................................................................................................26 CP .....................................................................................................................................29 CRON................................................................................................................................34 CSH...................................................................................................................................36 CUT...................................................................................................................................71 DATE ................................................................................................................................75 DF .....................................................................................................................................79 DIFF ..................................................................................................................................84
    [Show full text]