0% found this document useful (0 votes)
2 views49 pages

03_LinuxCommandsAndMore

This document provides an overview of basic Linux commands, file and directory management, and permission settings. It covers command structure, environment variables, file operations, and the differences between hard and soft links. Additionally, it explains how to change permissions and ownership of files and directories using various commands.

Uploaded by

iamsandesh1905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views49 pages

03_LinuxCommandsAndMore

This document provides an overview of basic Linux commands, file and directory management, and permission settings. It covers command structure, environment variables, file operations, and the differences between hard and soft links. Additionally, it explains how to change permissions and ownership of files and directories using various commands.

Uploaded by

iamsandesh1905
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

DevOps

01 Linux Basics

Linux Commands &


More - Part 1
Agenda for this session
• Linux Commands – Basic Commands
• Working with Files and Folders, Changing
Permissions & Ownership
• Types of Links – Soft Links and Hard Links

2
Linux
Commands
Linux Command - Structure
$ command --option argument

Command: Command/program that does one thing


Options: Change the way a command does that one thing
Short form: Single-dash and one letter (Example: ls –a )
Long form: Double-dash and a word (Example: ls --all )
Argument: Provides the input/output that the command interacts with.

Example: $cal –j 3 2000


4
Commands – Small programs that do one
thing well

“Many UNIX programs do quite trivial things in isolation, but, combined


with other programs, become general and useful tools.”

(From the book ‘The Unix Programming Environment’, Kernighan and


Pike)

5
Unix/Linux Philosophy

6
Simple commands

7
printenv displays environment variables
Environment Variables - Examples

SHELL=/bin/bash
NAME=LAPTOP-FV4AF8PS
PWD=/home/raja
LOGNAME=raja
HOME=/home/raja
LANG=C.UTF-8

8
alias

alias command is used to provide an alias name to an existing command.

clear clears the screen.


alias cls=‘clear’ provides an alias ‘cls’ to clear command.

Now, try

cls

9
Working with files and folders (directories)
- Simple commands
pwd - displays the present working directory (also known as ‘print
current directory’
ls - list files (similar to ‘DIR’ command in Windows but has several
options)
cd - change directory (to move from one directory to another)

10
Special characters interpreted by the shell
~ - home directory
. current directory
.. parent directory
* wildcard matching any file name
? wildcard matching any character

11
Changing directory and listing files/directories
cd /usr/local/lib Change directory to /usr/local/lib
cd ~ Change to home directory (could just type ‘cd’)
pwd Print working (current) directory
cd .. Change directory to the “parent” directory
cd / Change directory to the “root”
cd ../my Move one level above (to parent directory) and
then move to ‘my’ directory
ls –d uni* List only the directories starting with “uni”

12
ls command – more examples
ls -a List all files, including hidden files
ls -ld * List details about a directory and not its contents
ls -F Put an indicator character at the end of each name
ls –l Simple long listing
ls –la long listing of files including hidden files
ls –lR Recursive long listing
ls –lS Sort files by file size
ls –lt Sort files by modification time (very useful!)

13
Additional Linux commands
cp [file1] [file2] copy file
mkdir [name] make directory
rmdir [name] remove (empty) directory
mv [file] [destination] move/rename file
rm [file] remove (-r for recursive)
file [file] identify file type
less [file] page through file

14
Try these commands too…
head -n N [file] display first N lines
tail -n N [file] display last N lines
ln –s [file] [new] create symbolic link
cat [file] [file2…] display file(s)
tac [file] [file2…] display file in reverse order
touch [file] update modification time if file exists,
else create a new file
od [file] display file contents, esp. binary

15
Additional examples
cd # The same as cd ~
mkdir test
cd test
echo ‘Hello everyone’ > myfile.txt
echo ‘Goodbye all’ >> myfile.txt
less myfile.txt
mkdir subdir1/subdir2 # Fails. Why?
mkdir -p subdir1/subdir2 # Succeeds
mv myfile.txt subdir1/subdir2
cd ..
rmdir test # Fails. Why?
rm –rf test # Succeeds

16
The ls command
ls -l gives the long listing of files

drwxr-xr-x 6 ananth trguser 1024 Aug 8 13.30 personal


-rw------- 1 ananth trguser 1024 Aug 8 13.30 contacts

17
First 10 characters (in the output of ls -l)

drwxr-xr-x
Type of File

- normal file owner group others


d directory Permissions Permissions Permissions
l symbolic link of the file of the group for everyone
owner else
p named pipe
b block device
c character device
s socket

18
The 2nd column
ls -l gives the long listing of files

drwxr-xr-x 6 ananth trguser 1024 Aug 8 13.30 personal


-rw------- 1 ananth trguser 1024 Aug 8 13.30 contacts

The second column is the number of links to the file i.e., (more or less) the number of names there are
for the file. Generally an ordinary file will only have one link, but a directory will have more, because
you can refer to it as “personal‘’, “personal/.'' where the dot means ``current directory'', and if it has a
subdirectory named ``subdir'', ``personal/subdir/..'' (the ``..'' means ``parent directory'').

19
The 3rd and 4th columns
ls -l gives the long listing of files

drwxr-xr-x 6 ananth trguser 1024 Aug 8 13.30 personal


-rw------- 1 ananth trguser 1024 Aug 8 13.30 contacts

Owner Group

The third and fourth columns are the user who owns the file and
the Unix group of users to which the file belongs.
20
All columns
ls -l gives the long listing of files

drwxr-xr-x 6 ananth trguser 1024 Aug 8 13.30 personal


-rw------- 1 ananth trguser 1024 Aug 8 13.30 contacts

Type Permissions
of files (Owner, Owner Group Size in Modification Name of the file
Group, bytes date & time or directory
Others)

21
history command

history displays command history


Using the up ↑ and down ↓ arrows choose a previously used command
To redo your last command, try !!
To go further back in the command history try !, then the number as
shown by history (e.g., !5). Or, !ls, for example, to match the most recent
‘ls’ command.
Try left ← and right → arrows on the command line

22
Help on commands
Try these commands
date --help
date --help |more
man date
info date
help
man bash

23
Try ‘man’ with ‘less’
$man less
This helps you scroll through the text displayed on the screen by using
shortcut keys. For example,
Space or f for page forward, b for page backward, < to go to first line of
file, > to go to last line of file, / to search forward (n to repeat), ? to
search backward (N to repeat), h to display help, q to quit help

$man ls | less

24
Changing
Directory /
File
Permissions &
Ownerships
Three types of file permissions
1. Read - allows a user to open and read the content of the file
2. Write - allows a user to edit content or rename or remove the file
3. Execute - determines if the user can execute the file

26
Three types of directory permissions
1. Read - allows a user to view the directory's contents
2. Write - allows a user to create new files, rename files or delete files
in the directory
3. Execute - determines if the user can enter (cd) into the directory or
run a program or script.

27
Summary – File and Directory Permissions
Permissions File Directory
Read Read file contents Read directory contents
Write Change file contents, rename file, remove Create new files, rename or delete files in the
file directory
Execute Execute the file Enter the directory and/or run a program or script
in that directory

28
File access levels
1. Owner Permissions
2. Group Permissions
3. Others Permission (everyone other than the owner and group of
the owner)

Use the following command to know file or directory permissions.

ls -l

29
ls -l gives the long listing of files

drwxr-xr-x 6 ananth trguser 1024 Aug 8 13.30 personal


-rw------- 1 ananth trguser 1024 Aug 8 13.30 contacts

Type Permissions
of files (Owner, Owner Group Size in Modification Name of the file
Group, bytes date & time or directory
Others)

30
Permission - representation
Binary Octal Permission
000 0 ---
001 1 --x
010 2 -w-
777 means rwxrwxrwx
011 3 -wx
100 4 r-- 654 means rw-r-xr--
101 5 r-x
744 means rwxr--r--
110 6 rw-
111 7 rwx

31
Changing permissions (chmod command)
$ chmod 644 contacts

When you execute this command, the permissions of contacts changes


to rw- r-- r--

32
chmod command – other options
chmod g–w,o–r file1.txt

This will remove ‘write’ permission from group, and ‘read’ permission from ‘others’
on file.txt.

chmod u + x,g + x file1.txt

This will add ‘execute’ permission to user(owner), and add ‘execute’ permission to
group on file.txt

Note: These is no blank space in the second argument of this command. After chmod there is a blank space. After
that, it is a single string. There is a blank space before preceding file1.txt.

33
chmod command - other options
chmod u=rwx,g=r,o= file1.txt

This sets rwxr----- on file1.txt (user or owner can rwx. Group can read,
others do not have any permission)

Note: These is no blank space in the second argument of this command. After chmod there is a blank space. After
that, it is a single string. There is a blank space before preceding file1.txt.

34
chmod command – other options
These are valid too.

chmod u = rwx,g–w,o–r file1.txt

chmod u = rwx,g–w + x,o–r file1.txt

35
Changing file ownership
chown command is used to change file ownership

chown userB file1.txt

This command changes the owner of file1.txt to userB

36
Types of Links
(Soft links,
&Hard links)
inode
Linux users create directories and files. Linux stores information about a file
in ‘inode’.

‘inode’ is a structured data (or data structure) that keeps track of all
information about a directory or file.

When a user wants to access a file called ‘samplefile’, Linux searches a table
called ‘inode’ table to find the ‘inode’ of ‘samplefile’.

The ‘inode’ information of ‘samplefile’ provides the exact location of the file
on the hard disk, in addition to other details such as owner, size, etc.,
Read: https://www.slashroot.in/inode-and-its-structure-linux 38
inode of a file
Mode Has information about 1) permissions, 2) type (file, or directory or a symbolic link,...)
File Owner Access details like owner of the file, group of the file etc.
Size in Bytes
Time Stamps
(creation date & time,
modification date & time
Direct Blocks Points to first 12 data blocks. Up to 48K file size. (12 x 4K = 48 K)
Indirect Blocks Points to 1024 data blocks. Up to 4MB file size. (1024 x 48K = 4 MB)
Double Indirect Blocks Points to 1024 data blocks. Up to 4GB file size. (1024 x 4M = 4 GB)
Triple Indirect Blocks Points to 1024 data blocks. Up to 4TB file size. (1024 x 4 GB = 4 TB)

This is based on the default block size of 4K.

39
inode structure of a directory
Inode number is the same
for both because they
point to the same folder. .
(DOT) means ‘current
directory’

Sub-directories inode
and files numbers

40
Use ‘ls -ia’ command to know inode numbers
ls –ia

41
Hard Links
• Every file on the Linux filesystem starts with a single hard link.
The link is between the filename and the actual data stored on the file
system.
• Use ln command to create a hard link to a file
ln <original file path> <new file path>
ln <original file name> <new file name>

ln /home/user1/fileA /home/user1/doc/fileB
ln fileA fileB
Read: https://www.redhat.com/sysadmin/linking-linux-explained
https://www.redhat.com/sysadmin/inodes-linux-filesystem

42
Hard Links
$ ln link_test /tmp/link_new

$ ls -l link_test /tmp/link_new
-rw-rw-r-- 2 userA groupA 23 Jul 31 14:27 link_test
-rw-rw-r-- 2 userA groupA 23 Jul 31 14:27 /tmp/link_new

ls -li link_test /tmp/link_new


2730074 -rw-rw-r-- 2 userA groupA 23 Jul 31 14:27 link_test
2730074 -rw-rw-r-- 2 tcarrigan tcarrigan 23 Jul 31 14:27 /tmp/link_new

The permissions, link count, ownership, timestamps, and file content are the exact same.
43
Hard links – number of links & inode
• When changes are made to
one file, the other file reflects
Files link_test /tmp/link_new
those changes.
• Hard links cannot be created
across multiple file systems.
• If the original file (link_test) is
deleted, the data still exists
under the hard link
inode File System A (/tmp/link_new).
2730074 • The data is deleted from the
file system when all links to
the data are removed.

44
Soft Links
• Also known as symbolic links
• Command: ln –s <file1> <file2>
• Similar to shortcuts in Windows
• If the original file is deleted, the soft link is broken (also known as
‘dangling soft link’)
• Two different inode numbers
• Permissions and time stamps can be different

45
Soft links – number of links & inode
$ ln -s link_test /tmp/link_soft • When changes are made to either
of the files, both files reflect those
Files link_test /tmp/link_soft changes.
• Soft links cannot be created across
multiple file systems.
• If the original file (link_test) is
deleted, the data does not exist
under the soft link (/tmp/link_soft)
but the soft link remains. Later if a
new file ‘link_test’ is created, the
inode inode File System A soft link will point to it.
2730074 2730186 • The data is deleted from the file
system when the main file is
removed.

46
Differences between soft and hard links
Factor Soft Link Hard Link
inode Soft link’s inode number is different from the Uses the same inode number of
inode number of the original file or directory the original file
Linking of directories Soft links can be used to link directories Cannot link directories
Effect on deleting the The soft link will not work. It is similar to This does not delete the inode. The
original file ‘shortcut’ in Windows. hard link will continue to work and
show the file because it points to
the same inode.
Memory More Less
consumption
Speed or Slower than hard links Faster than soft links
Performance

47
Summary
• Linux Commands – Basic Commands
• Working with Files and Folders, Changing Permissions & Ownership
• Types of Links – Soft Links and Hard Links

48
Thank You

49

You might also like