3. Agenda
yum and apt-get to install tar
tar gzip
split and cat for multiple files
sed
useradd
groups
4. Install tar
RPM Based distributions like CentOS use yum
yum install tar gzip zip unzip bzip2
Debian Based distributions like
MX Linux use apt-get
apt-get install tar gzip zip unzip bzip2
5. Using tar
tar cvf FILENAME.tar DIRECTORY/
Replace FILENAME with whatever filename you
want and DIRECTORY with the path to the
directory you want to make into a tarball.
Command Flags Explanation
c: Create a TAR file.
v: Output verbosely (you'll be told exactly what is
happening in detail).
f: Specify a filename for the resulting TAR file.
6. Using tar
Put a Directory into a TAR file and Compress it with GZIP
Execute the following to create a single .tar.gz file
containing all of the contents of the specified directory:
tar cvfz FILENAME.tar.gz DIRECTORY/
Replace FILENAME with whatever filename you want
and DIRECTORY with the path to the directory you want
to make into a compressed tarball.
Tarred files compressed with GZIP sometimes use
the .tgz file extension.
7. Using tar
Command Flags Explanation
c: Create a TAR file.
v: Output verbosely (you'll be told exactly what is
happening in detail).
f: Specify a filename for the resulting TAR file.
z: Compress the TAR file with GZIP
8. Using tar
More for practice
https://www.hostdime.com/kb/hd/command-line/how-to-ta
r-untar-and-zip-files
9. Split and cat
We have discussed split and cat for making pieces of
files and combining them.
More for practice
https://ostechnix.com/split-combine-files-command-line-li
nux/
This can be used if the tar file is larger than the medium.
For example a DVD
10. Split and cat
tar cvfz /home /tmp/home_stuff.tgz
cd /tmp
split –bytes 4G home_stuff.tgz home_parts.
This shall create files as home_parts.aa .. ab ac and so
on
11. Split and cat
Split can use size letters
K,M,G,T,P,E,Z,Y
1K = Kilobyte
1G = 1 Gigabyte
P = petabyte = 1000 Terabytes
E = exabyte = 1 million Terabytes
Z = zetabyte = 1 billion Terabytes
Y = yotabyte = 1000 zetabytes = trillion terabytes
12. sed
$cat > geekfile.txt
unix is great os. unix is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
13. sed
$sed 's/unix/linux/' geekfile.txt
Output :
linux is great os. unix is opensource. unix is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.unix is a multiuser os.Learn unix .unix is a powerful.
14. sed
$sed 's/unix/linux/g' geekfile.txt
Output :
linux is great os. linux is opensource. linux is free os.
learn operating system.
linux linux which one you choose.
linux is easy to learn.linux is a multiuser os.Learn linux .linux is a powerful.
15. sed
Replacing the nth occurrence of a pattern in a line : Use
the /1, /2 etc flags to replace the first, second occurrence
of a pattern in a line. The below command replaces the
second occurrence of the word “unix” with “linux” in a
line.
16. sed
$sed 's/unix/linux/2' geekfile.txt
Output:
unix is great os. linux is opensource. unix is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.linux is a multiuser os.Learn unix .unix is a powerful.
17. sed
Replacing all the occurrence of the pattern in a line : The
substitute flag /g (global replacement) specifies the sed
command to replace all the occurrences of the string in
the line.
18. sed
Replacing from nth occurrence to all occurrences in a line
: Use the combination of /1, /2 etc and /g to replace all
the patterns from the nth occurrence of a pattern in a
line. The following sed command replaces the third,
fourth, fifth… “unix” word with “linux” word in a line.
19. sed
$sed 's/unix/linux/3g' geekfile.txt
Output:
unix is great os. unix is opensource. linux is free os.
learn operating system.
unix linux which one you choose.
unix is easy to learn.unix is a multiuser os.Learn linux .linux is a powerful.
20. sed
sed requires a lot of time and practice.
So we shall not be spending too much class time.
I have given an example of what it can look like and
longer tutorial which shall take things slowly but more
easily
21. sed
Parenthesize first character of each word :
This sed example prints the first character of every word
in parenthesis.
$ echo "Welcome To The Geek Stuff" | sed 's/(b[A-Z])/(1)/g'
Output:
(W)elcome (T)o (T)he (G)eek (S)tuff
22. sed
sed -e '/^[[:space:]]*$/ d' # remove empty line
-e 's/^[[:space:]]*//' # remove white space at the beginning
-e 's/[[:space:]]*$//' # remove white space at the ending (EOL)
-e 's/[[:space:]][[:space:]]*/n/g' # convert blanks between words to newline
-e '$a' # add a newline if missing at EOF
-e ..... # replace other patterns.
23. sed
This is the link to the tutorial. You can practice and ask questions later or on
slack. This shall require some time to explain each example or question and
may not be a good idea to bore everyone with sed examples.
https://www.tutorialspoint.com/sed/index.htm
You can use as much time as you want via slack for asking detailed questions
with the applications you want to practice on.
25. useradd
Adding a comment/GECOS value
-c : option assigns the value of GECOS field. For example:
Here the new user Steve is assigned the GECOS value as Steve Smith which is
the full name of the user.
26. useradd
Set Home Directory
-d : option creates a home directory with a name and path of your choice. By
default the home directory is created in /home directory with the same name as
the user’s name. But with –d option you can create a home directory of your
choice. For example:
27. useradd
Set UID of choice
-u : option assigns a user ID of your choice. For example:
The user David is given the UID 5555.
28. useradd
Assign a GID of choice
-g : option assigns the user to an existing group rather than creating a new
group for it. For example:
using -g option with useradd command using -g option with useradd
The user David is assigned to the group with GID 1007 which is an existing
group. Steve is already a member of 1007.
29. groups
A group in Linux is a collection of accounts that
can be given special permissions on the system.
For example, you can give one group the Read
permission on a file and another group the Read/
Write permissions on the same file.
This way, the users in the first group can only read
the file while the users in the second group can
read and modify it.
30. groups
Every user in Linux must have a primary group
assigned.
In most Linux distributions, the primary group is a
group with the same name as the user.
When a user creates files or launch programs,
those files and running programs are associated
with that group.
31. groups
In the next example we will create a file while
logged in as the user bob:
In the picture above you can see that the file
we’ve just created is associated with the group
named bob (the fourth field from the left
represents the group the file is associated with).
32. groups
The /etc/group file is a configuration file that
stores group information.
This file is readable by all users.
33. groups
To create groups in Linux, you can use a tool
called addgroup. addgroup is a friendlier front-end
to the low level tool groupadd.
The syntax of this command is simple: you just
type addgroup, followed by the name of the group.
You can also provide a specific GID (Group ID) for
the group with the –gid NUMBER parameter. If
you omit this parameter, addgroup will use the
next available GID.
35. groups
Groups are created with no users.
To add a user to the group, use the adduser
command with two parameters: the username and
group. For instance, to add the user jowilliams to
the group test_group, we would use the following
command:
36. groups
To delete a group in Linux, you can use a tool
called delgroup. delgroup is a friendlier front-end
to the groupdel program.
The delgroup command takes a single parameter:
the group name.
Note that you can’t delete the primary group of an
existing user: you must delete the user first or
change its primary group.
38. groups
You can also delete a group by editing the
/etc/group file, but it is recommended to use the
delgroup command.
delgroup checks whether the group is any user’s
primary group and if it is, refuses to remove the
group.
39. Modify groups
To modify an existing group in Linux, the
groupmod command is used.
Using this command you can change the GID of a
group, set the group password and change the
name of a group.
41. Modify groups
Here is a simple example of this command’s
usage. To change the name of the group from
test_gr to test_group, we can use the following
command:
42. Modify groups
Add users to groups
Interestingly enough, you can’t use the groupmod
command to add a user to a group.
Instead, the usermod command with the -G option
is used.
When adding a user to a new group, be sure to
also list all of the user’s current groups.
Omitting any of the user’s current groups will
remove the user from those groups!
43. Modify groups
To add bob to the group test_group, we can use
the following command:
We can list the groups a user is in by using the
groups command:
44. Modify groups
If you don’t wont to list all the user’s current
groups when adding a user to a new group, you
can use the -a option.
For example, if we want to add jwilliams to the
group cdrom and keep the current group
membership, we can use the following command: