combinepdf.pdf
Document Details
Uploaded by RefinedBowenite
Tags
Full Transcript
GraphicalEditors 33 What You Will Learn ● Various graphical editors. 34 Graphical Editors ● ● ● ● ● emacs gedit gvim kedit kate Emacs has a graphical mode too. The default text editor for Gnome. The graphical version of vim. The default text editor for the KDE. The KDE Advanced Text Editor...
GraphicalEditors 33 What You Will Learn ● Various graphical editors. 34 Graphical Editors ● ● ● ● ● emacs gedit gvim kedit kate Emacs has a graphical mode too. The default text editor for Gnome. The graphical version of vim. The default text editor for the KDE. The KDE Advanced Text Editor. 35 Graphical Editors ● ● ● AbiWord Microsoft Word alternative. LibreOffice Full office suite. Kate Source code editor. 36 Summary ● ● Various graphical editors exist for Linux. Graphical modes exist for emacs and vi. 37 Example of sort wc grep fgrep egrep sort searchfile.txt wc searchfile.txt wc -l searchfile.txt grep john searchfile.txt grep -i john searchfile.txt grep -v john searchfile.txt : Invert search You should try this on your own: Try Egrep and Fgrep : A nice example is in article below: https://linuxhandbook.com/grep-egrep-fgrep/#:~:text=To%20summ arize%3A,it%20performs%20a%20faster%20search . egrep “^(0|1)+ [a-zA-Z]+$” searchfile.txt 37 Polling question What does this do? wc -l searchfile.txt A) B) C) D) Prints the line count Prints file info Prints byte size None of these 39 echo - prints out something What is the difference between all these? echo “me” pwd echo `pwd` echo “pwd” `pwd` echo $HOME 40 Variables ▪ Variables are symbolic names that represent values stored in memory ▪ Three different types of variables • Global Variables: Environment and configuration variables, capitalized, such as HOME, PATH, SHELL, USERNAME, and PWD. When you login, there will be a large number of global System variables that are already defined. These can be freely referenced and used in your shell scripts. • Local Variables : like the i variable in for loop Within a shell script, you can create as many new variables as needed. Any variable created in this manner remains in existence only within that shell. • Special Variables Reserved for OS, shell programming, etc. such as positional parameters $0, $1 … A few global (environment) variables SHELL DISPLAY Current shell Used by X-Windows system to identify the display HOME Fully qualified name of your login directory PATH MANPATH Search path for commands Search path for <man> pages PS1 & PS2 Primary and Secondary prompt strings USER TERM PWD Your login name terminal type Current working directory 50 Defining Local Variables ▪ As in any other programming language, variables can be defined. ▪ Unlike other programming languages, variables in Shell Scripts are not typed. ▪ Examples : a=1234 # a is NOT an integer, a string instead b=$a+1 # will not perform arithmetic but be the string ‘1234+1’ b=$(($a+1)) will perform arithmetic so b is 1235 now. Note : +,-,/,*,**, % operators are available. b=abcde # b is string b=‘abcde’ # same as above but much safer. b=abc def # will not work unless ‘quoted’ b=‘abc def’ # i.e. this will work. IMPORTANT NOTE: DO NOT LEAVE SPACES AROUND THE = 51 global vs. local variables ● Global variables ○ ○ ○ ● Local variables ○ ○ ● Pollute the global namespace Sooner or later you step on your own foot , or someone else will Bugs of this kind can be very hard to track down Ideally can be flexibly scoped Don’t have the same issues as globals for being declared/defined over Conclusion ○ ○ ○ Everything is global, unless you make it local You can only use the ‘local’ keyword in functions Therefore, use functions as much as possible 52 Referencing Variables Variable contents are accessed using ‘$’: $ echo $HOME $ echo $SHELL To see a list of your environment variables: $ printenv or: $ printenv | more 53 echo - try all these What is the difference between all these? echo “me” pwd echo `pwd` echo “pwd” `pwd` echo $HOME 46 Polling question echo `pwd` and echo “pwd” print the same thing A) B) True False 47 Referencing variables --curly bracket ▪ Having defined a variable, its contents can be referenced by the $ symbol. E.g. ${variable} or simply $variable. When ambiguity exists $variable will not work. Use ${ } the rigorous form to be on the safe side. ▪ Example: a=’abc’ b=${a}def # this would not have worked without the { } as #it would try to access a variable named adef 56 Export is used for setting variables export DATETIME=`date "+%Y%m%d_%H%M%S"` echo "Using $DATETIME for outdir suffix" OUTDIR=out.$DATETIME Export Functions with -f : $name() { echo "CS131";} $export -f name $name CS131 57 Exit codes ● ● ● ● When a C program is done, it calls the function exit with an argument of 0. The function exit terminates a program. By convention, an exit code of 0 means OK, and an exit code between 1 and 255 means that an error occurred. On shell do this to check if a completed program succeeded: echo $? 58 CS 131.01 Processing Big Data: Tools and Techniques 3. Home Directory and More San José State University Slides are adapted from slides created by Dr. Andreopoulos 1 Optional readings 5th Internet edition available online:https://linuxcommand.org/tlcl.php/ Ch. 9, 11-12 Ch. 1-4 Ch. 1-5 2 Bash shortcut tip of the day Looking for a command in your history? Ctrl+R used Search for previous commands 3 Bash shortcut tip of the day Want to copy/yank multiple lines in vi? <Esc> puts you in command mode <num>yyyank num lines from current cursor position Go to where you want to paste the lines, press ‘p’ (below) or ‘P’ (above) 4 Bash shortcut tip of the day In vi you can move to the end of the doc with Shift+G 5 Output Redirection Operators >> appends to a file if it exists already, while > overwrites it 6 UNIX/Linux was built for customizing to a user’s needs ▪ Unix is designed so that users can extend the functionality • To build new tools easily and efficiently • To customize the shell and user interface. • To string together a series of Unix commands to create new functionality. • To create custom commands that do exactly what we want. 6 Configuring bash ■ ■ Almost any home installation of Linux defaults to the bash shell. Bash is one of many GNU.org (http://www.gnu.org) projects. 8 Polling question Configuring Bash to your personal needs is a good reason to use UNIX/Linux A) B) True False 9 bash Configuration Files ■ bash has two different login files under your homedir. ■ .bashrc gets read when you open a local shell on a machine ■ .bash_profile only gets read if and only if you login from a remote machine. Note that .bash_profile itself reads in your .bashrc file as well. ■ If you want aliases to be executed regardless, then you should put them in the .bashrc file. 10 Open your .bashrc for editing From home directory: vi .bashrc Add this line in the file: alias lll='ls -latr' Then source the bashrc file using : source .bashrc Now you can type lll so you don’t have to remember all those ls parameters 11 Polling question What does alias do in the previous example? A) B) C) D) Replaces a simpler word for the command given Runs the command Sets an env variable All of these 12 # File: .bashrc # Get the aliases and functions # Get whatever is in your # .bash_aliases file if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi 13 Supplementary Tips on ‘ (single quote), ` (backtick), and “ (double quote) ● Single quotes ( ' ) preserves the literal value of each character within the quotes. eg: echo 'Hello, $USER' # Outputs: Hello, $USER ● Double quotes ( " ) preserves the literal value of all characters within the quotes, with the exception of $, `, \ eg : echo “Hello, $USER” ● Backticks( ` ) is used for command substitution. It is executed by the shell before the main command eg: → file_count=`wc -l spotify-2023.csv` → echo "The number of files in the directory is: $file_count" Output:The number of files in the directory is: 954 spotify-2023.csv 14 Next: User Accounts 15 How Linux User Accounts Work ● Username ● Password ○ By default, all user home directories are created and maintained under the directory /home ○ However, the root user’s home directory is /root 16 User Accounts storage • This has been the default configuration used by Linux systems for many years. ○ /etc/passwd This file contains the user account information for your system. ○ /etc/shadow This file contains passwords for your user accounts. ○ /etc/group This file contains your system’s groups 17 Polling question Which file contains the passwords encrypted? A) B) C) D) /etc/passwd /etc/shadow /etc/group All of these 17 How Linux User Accounts Work ● Linux stores a list of all users in various files. You can try running this command in the Terminal to to view the groups and users in your system: ○ sudo nano /etc/group ○ sudo nano /etc/passwd What’s this sudo? 19 The Superuser ● By default, one account has elevated privileges to issue any command, access any file, and perform every function ● Superuser, a.k.a. root ● User and group number 0 20 The Superuser: sudo ● Linux systems have two user accounts—your own user account, and the root account, which is the super user that can access everything on the system, make system changes, and administer other users. ● In Ubuntu you can not login directly as the root, instead you use the sudo or su command to switch to root-level access when need privileged access to something (a file or a command). you 21 The Superuser ● ● Must limit use of root ○ Inexperienced users can cause serious harm ○ Use of root for non-privileged tasks is unnecessary ○ Security and privacy violations – root can look at anyone’s files Ensure a strong password 22 Superuser Privileges ● What usually works best is short periods of superuser privilege, only when necessary ● Obtain privileges, complete task, relinquish privileges ● Most common ways are sudo and su 23 sudo ● Allows you to issue a single command as root user or another user sudo [options] [-u user] command ● ● ● ● If no user specified, root is assumed New shell opened with user’s privileges Specified command executed Shell exited 24 Polling question Sudo can let you run a command as any user Eg: Can a user ‘ronaldo’ run a command as ‘messi’ using : ronaldo@sjsu: sudo -u messi <some_command> A) B) True False 25 Polling question Sudo can let you run as any user A) B) True False But if you are in /etc/sudoers file or in the wheel group or sudo group only 26 su ● Short for substitute or switch user su [options] [username] ○ If username is omitted, root is assumed ● After issuing command, prompted for that user’s password ● A new shell opened with the privileges of that user ● Once done issuing commands, must type exit 27 Configure sudoers ● Can configure if/how a user can run commands as another user with sudo ● Permissions stored in /etc/sudoers ● Use sudo visudo to edit this file (run as root) ● Permissions granted to users or groups, to certain commands or all, and with or without password being required 28 Summary root is the superuser. wheel (or sudo) is the group's name with superuser privileges. If I am in wheel (or sudo) group -> I have root/superuser privileges. On RedHat to do things as superuser you can either, login as root; or from another user account that has root/superuser privileges (as designated by "wheel" group membership or inclusion in the sudoers file) you can run su to get a root shell, or run sudo to run a single command with elevated privileges. In other distributions like Ubuntu it works similar - on Ubuntu you need to type su in order to become a root 29 Difference between Sudo and Su sudo and su serve different purposes: sudo is used for executing specific commands with elevated privileges, providing better security and control su is used to switch to another user's shell session, granting full access to that user's environment. Polling question How do you find the material so far? A) B) C) D) Easy - comfortable with topic Normal - don’t know the topic but learning Hard - don’t know the topic and hard to learn Very hard 31 Polling question How time-consuming are the course assignments/worksheets for you? A) Little time B) Normal time C) Much time D) Too much 32 Managing file/directory ownership ● Anytime a user creates a new file or directory, his or her user account is assigned as that file or directory’s “owner.” ● For example, suppose the user alice logs in to her Linux system and creates a file named linux_intro.txt using vi in home directory. Because she created this file, alice is automatically assigned ownership of linux_intro.txt. 33 How ownership works ● You can specify a different user and/or group as the owner of a given file or directory. ● To change the user/group that owns a file, you must be logged in as root or the owner. Commands used : ✔ Using chown ✔ Using chgrp ✔ You can also view file ownership from the command line using the ls –l command 34 Using chown ● The chown utility can be used to change the user or group that owns a file or directory. chown user file or directory. Example: If you wanted to change the file’s owner to the ken1 user: chown ken1 /tmp/myfile.txt Note: You can use the –R option with chown to change ownership on many files at once recursively. 35 Using chgrp ● In addition to chown, you can also use chgrp to change the group that owns a file or directory. chgrp group file (or directory) ● Example: chgrp student /tmp/newfile.txt 35 Permissions in File Systems • Every user of the system has a login name. • When a file is created, the UID and GID of the creator are remembered. • Every file also has associated with it a set of permissions in the form of a string of bits. Owner r w x/s Sum them up to get a number representing permissions for a user 4r 2w 1x s Group r w x/s Others rwx read write execute List contents Create and remove Exec file and chdir setUID/GID (see “man chmod”, will see chmod next time) 36 37 Polling question Chown can change ownership of a file OR directory A) B) True False 34 Creating and Managing User Accounts ● Using useradd ● Using passwd ● Using usermod ● Using userdel 40 Using useradd Syntax: Example: useradd options useradd username ken Note: you probably need to use sudo useradd … ken account is created using the default parameters contained in the following configuration files: /etc/default/useradd 41 Using userdel Syntax: Example: userdel options username userdel ken By default, userdel will not remove the user’s home directory from the file system. If you want to remove the home directory when you delete the user, you need to use the –r option in the command line. For example, userdel –r ken will remove the account and delete her home directory. Note: you probably need to use sudo userdel ... 42 Groups ● Ubuntu Linux uses groups to help you manage users, set permissions on those users, and even monitor how much time they are spending in front of the computer. 43 Why Linux uses groups? For security reasons ● Groups are the foundation of Linux security and access. ○ ● Groups logically tie users together for a common security, privilege and access purpose. Files and devices may be granted access based on a users ID or group ID. ○ Only admins have elevated privileges and general users can only access files that they are meant to 44 Managing groups ● Groups are defined in the /etc/group file (this is similar to /etc/passwd for users). ● Each record is composed of the following four fields: Group:Password:GID:Users ● Group - Specifies the name of the group. ● Password - Specifies the group password if it exists. (actually in /etc/gshadow) ● GID - Specifies the group ID (GID) number of the group. ● Users - Lists the members of the group. 45 Using groupadd Syntax: groupadd Options: options groupname –g Specifies a GID for the new group. –p Specifies a password for the group. –r Specifies that the group being created is a system group. 46 Using groupdel Syntax: groupdel example: groupdel group_name student 47 To add a user to a particular group Syntax: usermod -a -G examplegroup exampleusername On Debian (or Ubuntu, which is compatible) the sudo group has full admin privileges. We can grant a user the same sudo privileges by adding them to the group like this example: usermod -aG sudo testusr ← This is like modifying sudoers file with “sudo visudo” On CentOS (or RedHat, which is compatible) this is usually the wheel group instead of the sudo group: example: usermod -aG wheel testusr 48 Next: Scheduling repeated jobs with cron 49 Scheduling repeated jobs with cron cron - A time based job scheduling service. crontab - A program to create, read, update, and delete your job schedules. ● We will use cron to schedule and automate tasks. 50 Using the crontab command crontab file Install a new crontab from file. crontab -l List your cron jobs. crontab -e Edit your cron jobs. crontab -r Remove all of your cron jobs. 51 Crontab file entries * * * * * command ||||| | | | | +-- Day of the Week (0-6) | | | +---- Month of the Year (1-12) | | +------ Day of the Month (1-31) | +-------- Hour (0-23) +---------- Minute (0-59) See also : cat /etc/crontab 52 Crontab file entries # Run every Monday at 07:00. 0 7 * * 1 /root/backupdb > /tmp/db.log 2>&1 ||||| | | | | +-- Day of the Week (0-6) | | | +---- Month of the Year (1-12) | | +------ Day of the Month (1-31) | +-------- Hour (0-23) +---------- Minute (0-59) 53 Crontab file entries # Run at 02:00 every day and # send output to a log file. 0 2 ?* * /root/backupdb > /tmp/db.log 2>&1 * 54 Crontab file entries # Run at 02:00 every day and # send output to a log file. 0 2 * * * /root/backupdb > /tmp/db.log 2>&1 55 Polling question What does this crontab do? * * * * * /root/backupdb > /tmp/db.log 2>&1 A) Runs the script every minute B) Runs the script every hour C) Runs the script every second D) Never runs the script 56