Introduction to Linux Organizing Files Computational Science and Engineering North Carolina A&T State University Instructor: Dr. K. M. Flurchick Email: kmflurch@ncat.edu
Arranging, Organizing, Packing 2
Command: sort Sort the lines contained in a file or a group of files, alphabetically, and write the solution to standard output. Very useful when combined with other commands (see pipes and redirections). Format: sort <filenames> Options: -r or --reverse -n or --numeric-sort -k or --key <pos1> [,<pos2>] 3
Command: tar tape archive This name is a misnomer and dates back to when it was used for tapes. Now tar is commonly used to collect together sets of files and directory trees into a single file, which is usually called a tar file and by convention ends with the extension.tar. This archive, or tar file, (or tar ball) is a file that contains other files plus information about them, such as their filename, owner, timestamps, and access permissions. This allows collections of files to be moved around, shared, or transported easily by simply manipulating the single tar file. 4
Options for tar Format: tar <operation> [options] <file(s)> Typical operations: (note, no dash(-) required) c - create x extract t list Options: -v - verbose -z - filter through gzip -f <thisfile> use file thisfile 5
Example: tar Create an archive by tarring up everything in the myproject mydata and mysrc directories: tar cvf myproject.tar myproject mydata mysrc Note: it is usually much better to use relative directory paths so that there are no issues when untarring the archive. List contents of a tar archive: tar tvf myproject.tar Extract all the data from the archive: tar xvf myproject.tar To unzip and untar in one step (note: by convention these files usually have the extension.tar.gz or more commonly,.tgz): tar xzf ncardata.tgz 6
Command: zip and unzip zip and unzip are common utilities for compressing and decompressing files. Format: zip <filename.zip> <list of files to add> filename.zip is the archive file name. <list of files to add> is the list of all the files you want to add to the zip. unzip <filename.zip> To restore use unzip <filename.zip> To list the files in the archive use -t option 7
Command: compress and gzip compress and gzip (gnu zip) are utilities for compressing and decompressing files (which may be or may not be archive files). Format: compress <filename> filename is replaced with compressed filename.z To restore use uncompress <filename.z> gzip <filename> filename is replaced with compressed filename.gz To restore use gunzip or gzip -d 8
Exercise 4 1. Use tar to create the archive files.tar of the scratch directory. 2. Generate a long listing of the directory. (Note the size of the archive file). 3. Compress the file files.tar. 4. Generate a long listing of the directory. (Note the size of the compressed archive file). 9
Pipes and Redirection 10
Redirection Output from programs are: usually written to the screen, referred to as standard output (stdout). Input for programs are: usually comes from the keyboard. If no file arguments are given, referred to as standard input (stdin). Error messages from processes go to another output channel: usually written to the screen, referred to as standard error (stderr). 11
Redirection and Pipes You begin to experience the power of Linux when you combine simple commands together to perform complex tasks. Most (all?) Linux commands can be piped together. Use a - as the value for an argument to mean read this from standard input. 12
Redirection and Pipes > >> < redirects stdout, appends stdout, redirects stdin, Redirecting stderr varies by the shell, use & in tcsh/csh use 2> in bash/ksh/sh pipes (connects) stdout of one command to stdin of another command. 13
Jobs and Processes 14
Command: ps process status This is a snapshot of current processes, and in particular, displays the process id (pid). Options vary somewhat with Linux flavors and you can customize the output, if desired. Examples: show all processes owned by userid. ps u <userid> show every process in long format ps -elf Find all processes of a particular kind labeled by <string>. ps elf grep <string> 15
Command: kill,pkill,ctrl-c Terminate a process. You can only terminate processes you own, unless, of course, you are root. ctrl-c To terminate a process running that hasn t ended. kill <pid> kill processes with the TERM signal. kill -9 <pid> kill processes with the KILL signal -9 (stronger form). pkill <name> looks up process based on name or other attribute (not in all versions of Linux). 16
Command: bg, fg, ctrl-z, & To run a job in the background, just end the command with an &. This allows the job to run and the shell will return control to the user (recall Linux is innately multiprocess). mylongrunningjob & If a job is already running and you want to interrupt it and put it in the background use ^z (ctrl-z) and bg. To return a job to the foreground, use fg. 17
Command: jobs jobs shows all processes running in the background. Use fg %n to move job n to the foreground, if required. fg by itself will foreground the default job. kill %n will kill job n Example: % jobs [1] + Running OracleCalendar/bin/ocal [2] - Running emacs [3] Running mulberry 18
Environment Variables Environment variables are used (sometimes required) by many applications. Environment variables can be modified by you. Environment variables customize the runtime behavior for applications (including the shell). You can save environment variables in a special file called a resource file. 19
1. In your current shell, type env. Try to interpret the results. Try env sort Exercise 5 2.Use setenv to set an environment variable TEST to the value /usr/share. Confirm this using env. 3.Type bash to change your shell. 4.Use alias to create a simple shortcut, alias more to the letter m. 20
Tips and Tricks 21
Command: history tcsh shell Issued as a command, this displays the history of commands used in the particular shell, as a list. In the tcsh shell, set the variables: history number of events saved within a session, savehist number of events saved between sessions.!string will repeat command beginning with string. Use the ^p, ^n keys to page through history or use the up/down arrow keys. You can interactively edit the command line to modify the previous command as needed. Use the left/right arrows, crtl-e end of line, ctrl-a to get to the beginning of the line. 22
Command: history - bash shell Issued as a command, this shows the history of commands, used in the particular shell, as a list. In the bash shell, set the variables: HISTSIZE number of events saved within a session, Histfilesize number of events saved between sessions. The rest works much like it does in the tcsh, except it does not have the searching using the meta keys. Caveat: I m not a bash user so maybe there are redeeming features to it. 23
File Completion To avoid typing mistakes, use file completion. The <tab> character will complete the entry, up to the next non-unique point. tcsh: ^d shows you all possible completions. set autolist which will show you possible completions automatically. You can put this variable in your.cshrc resource file. bash: A second <tab> shows all possible completions. 24
Command: alias This will allow you to create shortcuts for commonly used commands. alias, by itself, will display all the currently (i.e. in the shell you are in) defined aliases. You can pass arguments to alias. Examples: alias ls ls CFA alias lm ls lt \!* more alias h history 25
Good (lots) Linux: The Good, The Bad, and The Ugly Powerful, efficient, secure, stable, widespread, free, developerfriendly, fast, extensible, customizable. Bad (depends on the user) UI needs work, poor plug-n-play support, help may be hard to find (if no one you know uses it). Ugly (rarely) Sometimes arcane syntax. 26
man pages Linux Commands Summary Working with files and directories pwd, cd, ls, mkdir, rmdir, cp, mv, rm, cat, more, less, grep Wildcards *,?, [] Permissions chmod, chgrp Finding stuff find, which, whereis, locate Packing and Unpacking stuff tar, gzip, gunzip, compress, uncompress 27
Linux Commands Summary Cont'd Pipes and redirection >, >>, <, Jobs and processes ps, kill, pkill, ctrl-c (^c), bg, ctrl-z (^z), &, fg, jobs Quick and Easy: shortcuts and tricks of the trade file completion, history, alias 28
Linux is an oral tradition. For More Information man pages, man pages, man pages info command We used some material from: http://www.doc.ic.ac.uk/~wjk/unixintro/index.html 29
Resources Google: http://www.google.com/linux GNU: http://www.gnu.org/ Linux documentation project: http://tldp.org/ O'Reilly: http://www.ora.com/ Red Hat's documentation: http://www.redhat.com/docs/ UNIX: http://www.bell-labs.com/history/unix/moreinfo.html Your distribution's man pages and assorted documentation. 30