lecture 4
lecture 4
Lecture 4:
system accounts and groups
● System Accounts
● Managing users and groups
● Globbing Introduction
● Text Utilities
● Redirection
System Accounts
● There are three types of accounts on a lunix system :
❑ Root account
• This is also called superuser and would have complete and unfettered control of the system.
• A superuser can run any commands without any restriction. This user should be assumed as a
example mail accounts (mail services) and the appache accounts (http service).
• These accounts are usually needed for some specific function on your system, and any
• General users are typically assigned to these accounts and usually have limited access
• lunix supports a concept of Group Account which logically groups a number of accounts.
Page - 4
View all user accounts
Page - 5
System’s username
● whoami: It tells you about the system's username.
● who command gives the information about the users logged on to the system.
● w command tells about the users who are logged in and what are they doing.
● Id command tells about your user id, primary group id, and a list of groups that belongs to you.
Page - 6
Managing Users and Groups
● There are four main user administration files −
• /etc/passwd − Keeps the user account and password information. This file holds the
majority of information about accounts on the lunix system.
• /etc/shadow − Holds the encrypted password of the corresponding account. Not all
the systems support this file.
• /etc/group − This file contains the group information for each account.
• /etc/gshadow − This file contains secure group account information.
• /etc/skel/- contains some hidden files which have profile settings and default values
for applications. Hence, it serves as a default home directory and user profile. While
using useradd -m option, the /etc/skel/ is copied to the newly created directory.
•
Page - 7 Check all the above files using the cat command.
Commands to create and manage accounts and groups
Page - 8
Create a Group
Page - 9
The following table lists out the parameters
Sr.No. Option & Description
1 -g gid The numerical value of the group's ID
2 -o This option permits to add group with non-unique GID
3 -r This flag instructs groupadd to add a system account
4 -f This option causes to just exit with success status, if the specified group
already exists. With -g, if the specified GID already exists, other (unique) GID is
chosen
5 Groupname Actual group name to be created
• following example creates a developers group with default values, which is very much
acceptable for most of the administrators. $ groupadd developers
Page - 10
Page - 11
Modify a Group
Page - 12
Create an Account
● Following is the syntax to create a user's account −
useradd -d homedir -g groupname -s shell -u userid accountname
● The following table lists out the parameters −
Page - 15
Delete an Account
• The userdel command can be used to delete an existing user. This is a very
dangerous command if not used with caution.
• There is only one argument or option available for the command -r, for removing
the account's home directory and mail file.
● For example, to remove account lunix_2, issue the following command −
$ userdel -r lunix_2
● If you want to keep the home directory for backup purposes, omit the -r option.
You can remove the home directory as needed at a later time
Page - 16
Globbing Introduction
Globbing
• Globs, also called “wildcards”, are special characters to the shell designed to match
filenames used for manipulating (listing, copying, moving, etc.) groups of files
• Three types:
Page - 18
“*” Wildcard Examples
Page - 19
“?” Wildcard Examples
• Display all files in the current directory that have exactly one character in file name:
○ $echo ? or $ls ?
• Display all files in the current directory that begin with the letter D and have three more
characters:
○ $echo D??? or $ls D???
Page - 20
“[ ]” Wildcard Examples
• Display all files in the current directory that begin with "a", "b" or "c":
○ $echo [abc]* or $ls [abc]*
○ $echo [a-c]* or $ls [a-c]*
• When using a range ([a-c]), the range is based on the ASCII text table
• Display all files in the current directory that don't begin with "a", "b" or "c":
○ $echo [^a-c]* or $ls [^a-c]*
Page - 21
More complex Examples
• Display all files in the current directory that begin with "a", "b" or "c" and are at least 5
characters long:
○ $echo[abc]????* or $ls [abc]????*
• Display all files in the current directory that begin with don't end with "x", "y" or "z":
○ $echo*[^xyz] or $ls*[^xyz]
Page - 22
Text Utilities
Display file content
• The cat command merges two or more files into a single file:
$cat file1 file2 > file3
• Can also be used to display the contents of a file to the terminal:
$cat file1
• The fmt command does very simple text formatting
• The most common purpose is to format a text file with a maximum line width
• Use -w option to specify the width of each line
Page - 24
pr command
Page - 25
The file command
Page - 26
The split command
• The split command will break large files into smaller files
• Useful for file transfer when large files create problems
• Syntax:
● split [OPTION]...[INPUT [PREFIX]]
• INPUT is a file or stdin
• By default the new files will be named with a prefix of x and a suffix of aa, ab, etc.
• For example, the first file would be called xaa, the second file would be called xab, etc
• The -d option splits files to have a numeric suffix instead of a default alphabetic suffix
• The file names will start with the PREFIX, if specified; if not specified, then "x" is used
Page - 27
The nl command
Page - 28
tr command
• The tr command can be used to translate from one set of characters to another set
Page - 29
The sed command
● The sed (Stream Editor) command is a powerful tool used for text manipulation, search, find-and-replace,
insertion, and deletion in Linux.
● It processes text line by line and can modify files or input streams without opening them in an editor.
sed [OPTIONS] 'COMMAND' file
Page - 30
Redirection
Standard Text Streams and Redirection
● Data is entered into the computer via stdin (usually the keyboard), and the resulting
output goes to stdout (usually the shell). These pathways are called streams. However,
it's possible to alter these input and output locations, causing the computer to get
information from somewhere other than stdin or send the results somewhere other
than stdout. This functionality is referred to as redirection.
Page - 32
Standard Text Streams
• stdin − This is referred to as the standard input and the associated file descriptor
is 0. This is also represented as STDIN. The lunix program will read the default
input from STDIN.
• stdout − This is referred to as the standard output and the associated file
descriptor is 1. This is also represented as STDOUT. The lunix program will
write the default output at STDOUT
• stderr − This is referred to as the standard error and the associated file
descriptor is 2. This is also represented as STDERR. The Unix program will
write all the error messages at STDERR.
Page - 33
File Descriptors
• In simple words, file descriptors are integers (numbers) that act as unique identifiers
for an open file (or other I/O resource) in a Linux system.
• Remember, in lunix, "everything is a file descriptor or a process" or even
"everything can have a file descriptor“.
• It's important and useful to understand how the so-called three standard file
descriptors, or standard streams, work because all processes use these channels for
input and output operation.
Page - 34
Page - 35
Standard Out
• When a command executes without any errors, the output produced is known as
standard out (also called stdout or STDOUT)
• By default stdout is sent to the terminal
• You can redirect stdout from a command into a file
• For example, when you type the date command, the resulting time and date output is
displayed on the screen.
• It is possible, however, to redirect this output from stdout to somewhere else. In this
case, I'm going to redirect the results to a file named specifications.txt. I'll confirm it
worked by using the cat command to view the file contents.
Page - 36
Standard Out
• To redirect stdout of a command to a file, use the “>”:
○ $ls > /tmp/ls.txt
• A single > will overwrite existing file contents
• Use >> to append to the end of a file
• Stdout is assign a numeric value of 1, so it could also be used like this:
○ $ls 1> /tmp/ls.txt
• The >> redirector even works on an empty file. That means that you could
conceivably ignore the regular > redirector to alleviate the potential to overwrite data,
and always rely on the >> redirector instead.
Page - 37
Page - 38
Standard Error
• When command encounters an error, it will produce output that is known as standard error
(also called stderr or STDERR).
• The stderr output is sent to the terminal
• The number associated with the standard error file descriptor is 2.
• To redirect stderr, use the following syntax:
• $ls /junk 2> /tmp/output.err
• A single 2> will override existing file contents
• Use 2>> to append to the end of a file
• To discard output, redirect output to the /dev/null file (trash can)
Page - 39
Standard Error
Page - 40
Standard Input
• Standard in (also called stdin or STDIN) normally comes from the keyboard
• Some commands expect input from STDIN rather than a filename argument
• Sometimes you may want to redirect standard input so the input comes from a file instead of
they keyboard
• User interactions with the system are input through standard input (stdin), which is
channel/stream 0, usually by using a keyboard.
Page - 41
• Then, any command executed through an interactive shell connects to a text
terminal on which the shell is running and sends the output through either
standard output (stdout), which is channel/stream 1, if it is OK, or through
standard error (stderr), which is channel/stream 2 if it is not OK.
• The stdout is usually the terminal displayed by the monitor. There are other
channels and streams (3 and up) that any process can use and don't have a
default input or output.
Page - 42
Redirection with >
Page - 43
Append with >>
Page - 44
tee command
• The tee command duplicates output to be placed in a file and to the terminal
simultaneously:
the diff between tee and redirection:
● $date | tee timer.txt
in tee :
● Fri Nov 7 02:21:24 UTC 2014 You see the output on the screen AND it is saved in timer.txt.
in redirection :
● $cat timer.txt The date is saved in timer.txt, but you don’t see it on the screen.
Page - 45
Prepare for the Next Lecture!
After solving, take screenshots and be ready for discussion.
1. Write a command to remove all white spaces from a file named input.txt.
2. write a command to replace all occurrences of the word "error" with "warning" in a file called
log.txt.
3. How to print only the first and last columns from a space-separated file named data.txt?
4. Write a command to delete all digits (0-9) from a file called numbers.txt.
5. Write a command to delete all lines that contain the word "debug" from logfile.txt.
6. Using redirection, how would you save both the output and error messages of a script called
run.sh into a file named output.log?
Page - 46
orl
Thank you !