LNX255 – Week 04 Notes © M. Heidenreich, Seneca Polytechnic Lesson Overview ● Ambiguous File Names ● Quoting ● File and Directory Permissions ● Process Control © M. Heidenreich, Seneca Polytechnic Ambiguous File Names ● Ambiguous File Names allow you to use meta-characters (also called wildcards) to refer to a group of files and/or directories – ● ● ● The process of matching meta-characters to actual file names is called filename expansion or shell globbing Also useful shell feature when you don’t know file name(s) exactly Hidden file marker (period at the beginning of file name) cannot be matched by meta-characters Filename expansion is often confused with regular expressions, which look similar but have a different purpose © M. Heidenreich, Seneca Polytechnic Meta-Characters ● When any meta-character is present in a command line the shell expands it usually to match a group or range of files ● Can save you on typing time ● Characters most often used for file matching are: ? * [ ] – ? matches any single character – * – [ ] (square brackets) enclose a group of characters to match (asterisk) matches any number (including zero) of characters © M. Heidenreich, Seneca Polytechnic ? Meta-Character ● Matches any single character ● Example use: ls hello? – Above can match for example: ● ● – hello1 hello_ But it will not match for example: ● ● hello myhello1 © M. Heidenreich, Seneca Polytechnic [ ] Meta-Characters ● ● You can specify a range of characters within the square brackets for letters and digits Example commands: – ls hello[1-3] – rm record.[a-z] – cp [1-3a]-monday ~/Desktop – mv example[1-3f-j].txt /media/usb © M. Heidenreich, Seneca Polytechnic [ ] Meta-Characters Exclusion ● ● ● Sometimes you might want to exclude certain characters in the file names Place the ! character first inside the square brackets to exclude the other characters listed Example command: ls hello[!0-9]* – ● Above command will match any file name that its name starts with hello as long as there is no digit on the 6th position The square brackets still matches only one character, just “not” © M. Heidenreich, Seneca Polytechnic [ ] Meta-Characters ● Matches one character out of a class ● Example use: rm file[1abc] ● Above command will delete the following files (if found in the present working directory) – file1 – filea – fileb – filec © M. Heidenreich, Seneca Polytechnic * Meta-Character ● Matches any characters, zero or more times ● Example use: cp hello* /tmp – Above command will copy files with names beginning with hello (including hello) into /tmp – Files not copies would include: Hello or myhello1 or .hello © M. Heidenreich, Seneca Polytechnic Quoting ● Remember that most non-alphanumeric characters are special to the shell and subject to globbing ● ● Special characters are replaced or otherwise interpreted and this may affect the way the actual command is run Unless you intend to use special characters, for example as wildcards in filename expansion they need to be quoted to turn off the globbing ● ● Use double quotes to enclose longer strings, for example: echo “What is your name?” - without the quotes the extra spaces would be ignored and name? may be replaced with file names if matched Use backslash (\) to quote single characters, directly following the backslash, for example: touch weird\;file\;name - this would allow you to have special characters in a file name (not a good idea anyway) © M. Heidenreich, Seneca Polytechnic Quotes ● ● Sometimes it may be necessary to quote a quote – use two different quotes to accomplish that, for example: echo I would like to say: \”Hello Friend\” Linux shell supports a few other quoting characters, including: ● ● Single quotes (‘) - similar in function as double quotes (“) but they also quote the $ character which double quotes ignore Single back quotes (`) - deprecated way to run commands within command, no longer used in practice © M. Heidenreich, Seneca Polytechnic Quoting Examples ● Quoting may be necessary for command parameters to avoid undesired or harmful effects, here are some examples: ● ● ● find /tmp -name “*.txt” – the find command uses wildcards internally and without the quotes the shell may interfere by replacing the *.txt with matching file names resulting in find producing en error grep \~ records.csv – here the search pattern is the tilde (~) character and without the quote it would be substituted by the shell with the path to home directory, causing grep to return incorrect results rm \* – an attempt to remove a file with a very unwise name of *; without the quote the rm would remove all non-hidden files in the PWD © M. Heidenreich, Seneca Polytechnic File Access Control ● ● File permissions define who has access to files and directories and under what terms Files and directories on all UNIX-like systems can be accessed by three entities: User – the owner of the file (you own files which you create) ● Group – each user belongs to at least one user group ● Others – everyone else (who does not have an account on the system) ● Permissions do no apply to system administrators who have always full access to the entire system ● ● All new files get default permissions, dependent on local settings © M. Heidenreich, Seneca Polytechnic Regular File Permissions ● File permissions define what happens with the file contents ● There are three types of access permissions for regular files: ● ● ● ● Read – file contents can be read/viewed and file can be copied Write – file contents can be changed or erased Execute – file can be executed (run), set only for programs File permissions should be as restrictive as possible Set only only permissions needed to protect them from accidental change ● Do not give permissions to group or others without a reason ● © M. Heidenreich, Seneca Polytechnic Directory Permissions Directory permissions define what happens with directory contents ● There are three types of access permissions for directories: ● Read Directory contents (files inside) can be viewed/listed (for example using the ls or tree command) ● Write Directory contents can be changed - you can create files inside and/or rename or delete them ● Pass-through Directory can be entered by using the cd command Directory can be used in a pathname to reach something inside ● ● Access permissions for directories are not recursive All sub-directories control access to their content separately ● Pass-through when disallowed for parent, cuts off access to everything inside ● © M. Heidenreich, Seneca Polytechnic Checking Permissions ● Use the ls -l command ● Example output: ls -l a1.check User (pi) Group (gpio) -rwxrw-r-- 1 pi gpio 2281 May 11 23:09 a1.check Permission for others - read Permission for group (gpio) – read and write Permission for user (pi) – read, write and execute © M. Heidenreich, Seneca Polytechnic Changing Permissions ● Use the chmod command (change file mode) ● The chmod command accepts permissions in two ways: ● Symbolic, using letters ● ● ● ● Numeric, using octal numbers ● ● ● ● Letters r, w and/or x are used for permissions Letters u, g, o and/or a are used for affected entities (use, group, others, all) Arithmetic operators +, – and/or = are used to set the permissions Permissions are set for all entities (user, group and others) at the same time Each entity permission is a number between 0 and 7 A specific entity permission is a sum where r=4, w=2 and x=1 Method used at any particular time is a matter of personal preference © M. Heidenreich, Seneca Polytechnic Symbolic Permission Changing ● Access can be changed for one or multiple entities the time ● Usage examples: ● ● ● – add write chmod u+x file permission for user chmod u=rwx,g-rw file – grant user full permissions and remove read and write permission for group chmod u-w,go= file – remove write permission for user and remove all permissions for group and others ● chmod a=r file – allow only read permission for all © M. Heidenreich, Seneca Polytechnic Numeric Permission Changing ● Each access type is represented by a number: r=4 ● w=2 ● x=1 ● ● A permission is a mathematical sum of desired modes, for example: r+w=6 ● w+x=3 ● ● Permissions are always set for all entities (user, group and others), for example: ● chmod 751 file Sets rwx for user (4 (r) +2 (w) +1 (x) = 7), rx for group (4 (r) + 1 (x) = 5) and x (1) for others © M. Heidenreich, Seneca Polytechnic Process Management ● A running program is called a process ● An operating sytem may run hundreds ot thousands processes at a time ● ● ● Some processes belong to the operating system and others are user programs running ● Processes are being multi-tasked by the OS to share the hardware and other resources ● Some processes start other processes ● Processes can be suspended, resumed, killed etc. as needed From the user perspective processes are running in the background or in the foreground Processes are managed using signals, for example SIGINT (pressing Ctrl+C) or SIGTERM which may use a special command (kill) ● There are many more signals available but most users only need to know about a few © M. Heidenreich, Seneca Polytechnic Process Management ● Processes are arranged in a hierarchy ● ● ● ● This structure has a root process, as well as parent and child processes Each process has a process ID (PID) and with the exception of the root process a parent process ID (PPID) Each process has a priority, which affects how much attention and resources it gets form the OS Processed can be killed ● This may be simply normal program exit or an external event ● Killing a process terminates all child processes of that process © M. Heidenreich, Seneca Polytechnic Process Identification ● The main tool to view processes is the ps command ● ps = Process Status ● Provides a snapshot at the moment of its execution ● ● This is a very old command with inconsistent options – the manual (man ps) is a good resource Another useful tool is the top command ● Provides a live feed of processes and resource usage – CPU, memory etc. © M. Heidenreich, Seneca Polytechnic How to Kill a Process ● Use the kill or the pkill command to terminate processes which you own (which you have started), for example: pkill lab1.sh ● ● ● ● The process gets a soft signal (SIGTERM) asking it it terminate itself kill command requires a numerical PID to identify the process, which takes time to determine, but is more accurate pkill uses process name to identify the process but it will kill all processes with the same name at the same time, which may not be desired Some processes may require a stronger signal than SIGTERM ● Programs can be written to ignore the SIGTERM signal altogether ● The SIGKILL (or -9) signal cannot be escaped – use it if necessary only ● Using SIGKILL may lead to data loss as the program terminates immediately © M. Heidenreich, Seneca Polytechnic Case for Process Management ● ● ● In a single Terminal a user runs one process in the foreground while any other processes in that Terminal run in the background The user is typically interacting with the foreground process while the background processes either complete their task silently (do not produce output to the Terminal) or are waiting (sleeping) to resume their work in the foreground when the user chooses that Users may or may not choose to have any background processes on their Terminal ● Multiple Terminals may be a simpler alternative, each use case is different © M. Heidenreich, Seneca Polytechnic Case for Process Management ● ● Example: User works on a shell script in a text editor and wants to execute the script to test it. Only one terminal is available. In this example case placing the editor in the background temporarily instead of exiting the editor makes sense to save time. Compare the two different routes: One Process At The Time Concurrent Processes 1) Save the work 2) Exit the editor 3) Run the shell script 4) Retype the editor command 5) Move cursor to the previous place 6) Resume work 1) Save the work 2) Put the editor in background by pressing Ctrl+Z 3) Run the shell script 4) Put the editor in the foreground using fg command 5) Resume work © M. Heidenreich, Seneca Polytechnic