0% found this document useful (0 votes)
3 views46 pages

Unix Unit 1

The document provides an overview of the Unix operating system, detailing its features, architecture, and commands. It compares Unix with Windows, highlighting differences in multitasking, security, and file management. Additionally, it includes a list of basic Unix commands with their usage and examples, emphasizing the flexibility and power of Unix as a multiuser and multitasking OS.

Uploaded by

Prakruthi G urs
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)
3 views46 pages

Unix Unit 1

The document provides an overview of the Unix operating system, detailing its features, architecture, and commands. It compares Unix with Windows, highlighting differences in multitasking, security, and file management. Additionally, it includes a list of basic Unix commands with their usage and examples, emphasizing the flexibility and power of Unix as a multiuser and multitasking OS.

Uploaded by

Prakruthi G urs
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

Unix Programming

Unit 1 (Background and Commands)


Operating system
An operating system (OS) is system software that manages computer hardware, software
resources, and provides common services for computer programs.

In simple terms:

It acts as an intermediary between users and the computer hardware. It helps users
interact with the system, run applications, and perform tasks smoothly.

Unix operating system


 UNIX is a multiuser and multitasking operating system, meaning multiple users can
work on the system at the same time, and it can handle several tasks simultaneously.

 It was developed by Ken Thompson and Dennis Ritchie at AT&T Bell Labs in the late
1960s and early 1970s.

 Portability is one of UNIX's strengths—it runs on almost every type of hardware,


from personal computers to mainframes.

 UNIX has been a foundation and inspiration for many other operating systems,
especially in the Open Source movement (e.g., Linux, FreeBSD).

 Users interact with UNIX through a command-line interface known as the shell,
which interprets and executes user commands.

UNIX vs Windows Operating System


Feature UNIX Windows

Command Line Uses Command Prompt (CMD) or


Uses Shell (e.g., Bash, C Shell, etc.)
Interpreter PowerShell

Fully supports multitasking Also supports multitasking, but


Multitasking
efficiently with more overhead

Mostly open-source (e.g., Linux, Closed-source; owned by


Open Source
FreeBSD) Microsoft
Feature UNIX Windows

Known for high stability and Stable, but can be less so with
Stability
uptime heavy software use

Case-sensitive (e.g., [Link] ≠ Case-insensitive (e.g., [Link] =


Case Sensitivity
[Link]) [Link])

File extensions are optional and File extensions are required


File Extension
flexible (e.g., .exe, .txt)

Typically requires more manual User-friendly recovery tools


Recovery Support
tools (not for all) (System Restore, etc.)

Generally more secure (user Security improved, but more


Security
permissions, open reviews) vulnerable to viruses

Features of UNIX Operating System


 Open source – The source code is freely available for anyone to view, use, and modify.

 Portable – Can run on different types of hardware with minimal changes.

 Multiuser and Multitasking – Supports multiple users and can run many tasks at the
same time.

 Networking – Built-in support for connecting and communicating over networks.

 File organization system – Uses a hierarchical file structure to manage data efficiently.

 Pattern matching – Supports powerful text searching using tools like grep, sed, and
regular expressions.

 Building block approach – Combines small programs to perform complex tasks.

 UNIX Programming Tools/Tool Kit – Provides a rich set of tools for software
development.

 Documentation – Comes with detailed manuals and help files (man pages).

 UNIX Programming Language – Supports languages like C and shell scripting for
programming.

Command Man Pwd


In UNIX, everything is treated as a file—including input and output devices like the
keyboard, mouse, printer, etc. This design makes UNIX powerful and flexible.

Now, let’s explain those commands (with corrections):

1. man command

 Stands for: Manual

 Use: Displays the user manual of any command.

 Example: man ls – shows how the ls command works.

 ✅ Great for learning about commands, options, and usage.

2. pwd command Stands for: Print Working Directory

 Use: Shows the current directory you are in.

 Example: pwd – might return /home/user

 ✅ Helps you know where you are in the file system.

History of UNIX
 Ken Thompson at AT&T Bell Laboratories designed UNIX in the late 1960s, evolving
from a project called Multics.

 It was originally written in Assembly language, but was later re-written in C in 1973,
making it more portable and easier to modify.

 Two major branches of UNIX developed:

o AT&T UNIX (System V)

o BSD UNIX (Berkeley Software Distribution)

 In 1989, AT&T and Sun Microsystems collaborated to create System V Release 4


(SVR4), combining features of both System V and BSD.

Unix Architecture
� 1. Hardware (Innermost layer)

 Physical components like CPU, RAM, hard drives, etc.

� 2. Kernel

 The core of UNIX that directly manages hardware resources and system processes.

 Functions: memory management, process scheduling, device I/O, and system calls.

� 3. Shell

 A command-line interpreter that lets users communicate with the kernel.

 Each user gets a shell to run commands.

� 4. Utilities and Applications (Outer Layer)

This is where users interact with useful commands and software. Here's what the key
commands in the diagram do:

Command Description (1-line)

sort Sorts lines in text files alphabetically or numerically.


Command Description (1-line)

grep Searches for matching patterns in files or outputs.

sed Edits and transforms text in a stream or file.

tar Archives multiple files into a single .tar file.

ps Displays information about currently running processes.

who Lists users currently logged into the system.

cp Copies files or directories.

ls Lists files and directories in the current location.

cc C language compiler (compiles C programs).

Also includes:

 Text processors, Spreadsheets, Databases, Browsers, and Other Software – user-


level applications.

 Networking tools – UNIX supports connection with other computers.

 UNIX programming tools – for software development.

� 5. Users (Top Layer)

 Multiple users interact with the system simultaneously through the shell.

 Each user session is isolated, and users can run commands or applications.

Commands
 ✅ Command

 � Meaning (Purpose)

 � Example

 � Sample Output

� Basic Linux Commands with Meaning, Example & Output


1. mkdir

� Creates a new directory (folder).

mkdir test_dir

� Output: (No output if successful)


✅ A directory named test_dir is created.

2. rmdir

� Removes an empty directory.

rmdir test_dir

� Output: (No output if successful)


✅ Deletes test_dir if it's empty.

3. rm

� Deletes a file (or directory with -r).

rm [Link]

� Output: (No output if successful)


✅ Deletes [Link].

4. cd

� Changes the current working directory.

cd Documents

� Output: (No output)


✅ Switches to the Documents folder.

5. cd ..

� Moves to the parent directory (one level up).

cd ..
� Output: (No output)
✅ Goes one directory up.

6. pwd

� Prints the current working directory.

pwd

� Output:

/home/user/Documents

7. man

� Displays the manual/help page of a command.

man ls

� Output: Shows manual for ls.


� (Press q to quit the manual.)

8. wc

� Counts lines, words, and characters in a file.

wc [Link]

� Output:

10 50 300 [Link]

✅ Means: 10 lines, 50 words, 300 characters.

9. mv

� Moves or renames files.

mv [Link] [Link]

� Output: (No output)


✅ [Link] is renamed to [Link].
10. head

� Displays the first 10 lines of a file.

head [Link]

� Output:

Line 1

Line 2

...

Line 10

11. tail

� Displays the last 10 lines of a file.

tail [Link]

� Output:

Line 91

Line 92

...

Line 100

12. who

� Shows who is logged in.

who

� Output:

user tty1 2025-04-20 08:30

13. whoami

� Shows the current username.

whoami
� Output:

user

14. date

� Displays the current date and time.

date

� Output:

Sun Apr 20 [Link] AM UTC 2025

15. cal

� Displays the calendar of the current month.

cal

� Output:

April 2025

Su Mo Tu We Th Fr Sa

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30

16. cat

� Displays the content of a file.

cat [Link]

� Output:

This is the content of [Link]

Line 2
Line 3

17. tty

� Prints the terminal file name (your current terminal session).

tty

� Output:

/dev/pts/0

18. echo

� Prints the text or value of variables.

echo "Hello, Linux!"

� Output:

Hello, Linux!

19. type

� Shows how a command is interpreted (builtin, alias, function, etc).

type ls

� Output:

ls is aliased to `ls --color=auto`

Sure! Here's the explanation and example usage of head -n and tail -n in Linux:

✅ head -n

� Displays the first n lines of a file.

� Syntax:

head -n <number> <filename>

� Example:
head -n 5 [Link]

� Output:

Displays the first 5 lines of [Link]:

Line 1

Line 2

Line 3

Line 4

Line 5

✅ tail -n

� Displays the last n lines of a file.

� Syntax:

tail -n <number> <filename>

� Example:

tail -n 5 [Link]

� Output:

Displays the last 5 lines of [Link]:

Line 96

Line 97

Line 98

Line 99

Line 100

LS options
✅ ls Options

1. ls -l

Usage:

ls -l

Description: Long listing format (shows permissions, ownership, size, modified date)

Sample Output:

-rw-r--r-- 1 user user 123 Apr 20 10:00 [Link]

drwxr-xr-x 2 user user 4096 Apr 19 16:30 my_folder

The ls -l Unix command is used to display detailed information about the files and directories
in the current directory. Here's a breakdown:

Syntax:

ls -l

Output Explanation:

When you run this command, it lists files and directories in long format, providing detailed
information for each item. The columns in the output represent:

1. File Type and Permissions:

o Example: -rw-r--r--

o The first character indicates the file type (- for regular files, d for directories, l
for symbolic links, etc.).

o The next 9 characters show permissions for the owner, group, and others
(read r, write w, execute x).

2. Number of Links:

o Example: 1

o Indicates how many hard links point to the file or directory.

3. Owner Name:

o Example: username

o Specifies who owns the file.

4. Group Name:
o Example: groupname

o Shows the group associated with the file.

5. File Size:

o Example: 1024

o Displays the size of the file in bytes.

6. Modification Date and Time:

o Example: Apr 20 16:54

o Shows the last time the file was modified.

7. File Name:

o Example: [Link]

o Lists the name of the file or directory.

Example:

Imagine your current directory contains three files. Running ls -l might show:

-rw-r--r-- 1 username groupname 1024 Apr 20 16:54 [Link]

-rwxr-xr-x 1 username groupname 2048 Apr 20 16:53 [Link]

drwxr-xr-x 2 username groupname 512 Apr 20 16:52 dir1

 [Link]: Regular file with read and write permissions for the owner.

 [Link]: Executable file with permissions for all users.

 dir1: Directory with read and execute permissions.

Would you like a detailed example of file permissions or how to use other flags with ls? Let
me know!

2. ls -a

Usage:

ls -a

Description: Lists all files, including hidden ones (those starting with a .)

Sample Output:
. .. .bashrc .profile [Link] my_folder

3. ls -d

Usage:

ls -d */

Description: Lists only directories in the current folder

Sample Output:

my_folder/ test_dir/

⚠ Note: ls -d by itself just lists the current directory name: .

4. ls -r

Usage:

ls -r

Description: Lists files in reverse order

Sample Output:

[Link]

hello.c

[Link]

5. ls -f

Usage:

ls -f

Description: Lists files without sorting, shows directories with /

Sample Output:

./ ../ file1 dir1/ .bashrc

The ls -F command in Unix/Linux is used to list files and directories, with special symbols
appended to indicate the file type. These symbols help you quickly identify what kind of file
each item is. Here's what they mean:
ls -F Symbols Explained:

1. /: Indicates a directory.

o Example: Documents/

2. *: Indicates an executable file.

o Example: [Link]* (a file that can be executed like a program).

3. @: Indicates a symbolic link (soft link).

o Example: shortcut@ (a link pointing to another file or directory).

4. Other Symbols:

o =: Indicates a socket file.

o |: Indicates a FIFO (named pipe).

o >: Indicates a door (used in certain file systems like Solaris).

Example Output:

If you run ls -F, you might see:

file1*

dir1/

symlink@

socket=

pipe|

door>

6. ls -u

Usage:

ls -u

Description: Sorts files by last access time

Sample Output: (varies by access)

7. ls -t
Usage:

ls -t

Description: Sorts files by modification time, most recent first

Sample Output:

[Link]

[Link]

old_notes.txt

8. ls -i

Usage:

ls -i

Description: Shows inode numbers of files

Sample Output:

123456 [Link]

123457 my_folder

9. ls -ut

Usage:

ls -ut

Description: Sorts by last access time, most recent first

Sample Output:

accessed_recently.txt

[Link]

[Link]

� Combine with Multiple Flags

10. ls -x
Usage:

ls -x

Description: Lists files horizontally across screen (multi-column)

Sample Output:

file1 file2 file3 dir1 dir2

� who with options

11. who -u

Usage:

who -u

Description: Shows logged-in users along with idle time and process ID

Sample Output:

user pts/0 2025-04-20 09:30 . 12345 (:0)

� date with format

12. date +%m

Usage:

date +%m

Description: Shows current month (01-12)

Sample Output:

04

13. date +%h

Usage:

date +%h

Description: Shows abbreviated month name


Sample Output:

Apr

� Concatenate using -

If you're talking about using - to concatenate strings, we use echo or similar.

14. Concatenate using - in echo

echo "user"-"data"

Output:

user-data

Or using variables:

first="user"

second="data"

echo "$first-$second"

Output:

user-data

The Unix File system : Types of files

1. Ordinary (Regular) File

 Most common file type

 Can be text or binary:

o Text files: contain readable characters (e.g., .c, .java, .sh)

o Binary files: contain unreadable characters, like compiled programs, images,


audio, video

 Examples:

o Text: source code, shell scripts

o Binary: executables, object files, media files


2. Directory File

 Stores information about other files and directories

 Contains entries with:

o Filename

o Inode number (unique ID for the file)

 When files are added or deleted, the directory updates automatically

3. Device File

 Represents hardware devices (like USBs, printers, etc.)

 Found in the /dev directory

 Treated like files, so standard file commands (e.g., cat, cp) can work

 Kernel uses the file’s attributes to handle device operations (not the file content)

MKNOD
✅ What is mknod?

mknod is a command used to create special files like device files in the /dev directory.

� Syntax:

mknod [filename] [type] [major] [minor]

 [filename] – Name of the device file (e.g., /dev/mydevice)

 [type] – c for character device, b for block device

 [major] – Major number (identifies driver)

 [minor] – Minor number (identifies specific device)

� Simple One-Line Explanation:

mknod creates a device file with specified type, major, and minor numbers.
� Example:

sudo mknod /dev/mydevice c 1 5

� Sample Output (via ls -l):

crw-r--r-- 1 root root 1, 5 Apr 20 13:00 /dev/mydevice

Parent Child relationship

This diagram represents the Unix File System Tree, and it's a great illustration of the parent–
child relationship in the context of directories (not processes).

� Explanation:

 The root directory / is at the top — it's the parent of all other directories.

 Each directory under / is its child. For example:

o /bin, /home, /usr, /lib, etc. are children of /.


 This hierarchy continues:

o /bin has children: cat, date, who

o /home has children: kumar, sharma

o /home/kumar has children: [Link], progs

Internal and External commands


✅ Internal vs External Commands in Unix

In Unix, commands are classified into internal and external based on where they are
executed from.

� 1. Internal Commands

These are built into the shell itself.

 They are executed directly by the shell without calling a separate program.

 Faster, because no new process is created.

� Examples:

 cd – change directory

 pwd – print working directory

 echo – print text

 exit – exit the shell

 set – set shell options/variables

� Try:

type cd

# Output: cd is a shell builtin

� 2. External Commands

These are separate programs stored in system directories like /bin, /usr/bin, etc.

 Shell spawns a new process to run them.

 Located on the filesystem.


� Examples:

 ls – list files

 cp – copy files

 mv – move files

 cat – display file contents

 grep – pattern search

� Try:

type ls

# Output: ls is /bin/ls

Environment Variables
Environmental variables are settings that store information about the system, such as file
paths, configurations, or user preferences. They help software programs and scripts
interact with the operating system by providing essential data like the location of
executable files or system settings. In simple terms, they act like labels that hold key
information used by the system and applications.

� Key Points:

1. Definition:
Environment variables are key-value pairs used to configure the system and programs.

2. Syntax:

3. VARIABLE_NAME=value

4. export VARIABLE_NAME

5. Accessing a Variable:

6. echo $VARIABLE_NAME

7. Setting a Temporary Variable:

8. export MY_VAR="Hello"

9. Common Environment Variables:


Variable Description

PATH Directories to search for commands

HOME User's home directory

USER Current logged-in username

SHELL Default shell (e.g., /bin/bash)

PWD Current working directory

EDITOR Default text editor (e.g., vim, nano)

� Example:

echo $USER # Shows the username

echo $HOME # Shows home directory

export TEST=123 # Sets a new environment variable

� Note:

 These variables can be made permanent by adding them to config files like ~/.bashrc
or ~/.profile.

Pattern Matching and Wildcards

Pattern Matching in Unix & Wildcards


A wildcard is a special character used in computing to represent one or more characters in a
search or command. In Unix, wildcards help match file names and simplify tasks like
searching, copying, or deleting multiple files at once. Common examples include *, ?, and [].

� Wildcards (Glob Patterns):

Wildcard Meaning Example Matches

* Matches zero or more characters *.txt All files ending in .txt

? Matches exactly one character file?.txt [Link], [Link]

[] Matches any one character in set file[12].txt [Link], [Link]


Wildcard Meaning Example Matches

[Link], but not


[! ] Matches any character NOT in set file[!a].txt
[Link]

� Examples:

ls *.sh # List all files ending with .sh

ls a?.txt # List [Link], [Link], [Link] (any single character)

ls file[1-3].txt # Matches [Link], [Link], [Link]

ls [!0-9]* # Files not starting with a number

� Pattern Matching vs Regular Expressions:

 Pattern matching (globbing): Used in shell (like ls, rm)

 Regular expressions: Used in tools like grep, sed, awk (more powerful)

✅ Summary:

 Wildcards like *, ?, and [] help match file patterns.

 Useful for scripting and managing files efficiently.

Escaping and quoting , Absolute and relative path

1. Escaping and Quoting in Unix

Escaping

Escaping is used to treat special characters as literals instead of their special meaning. You
can escape a character by placing a backslash (\) before it.

Examples:

 Escaping spaces in a filename:

 touch my\ [Link]

Here, the backslash escapes the space, so my [Link] is treated as a single file name.

 Escaping other special characters:


 echo I\'m learning Unix

This escapes the single quote ('), so it is treated as a literal character.

 Escaping a dollar sign to print it instead of using it for variables:

 echo \$USER

This will print $USER rather than expanding the value of the USER variable.

Quoting

Quoting prevents the shell from interpreting certain characters. There are two types of
quoting in Unix:

 Single Quotes ('): Everything inside single quotes is treated literally. Variables or
special characters are not expanded.

Example:

echo 'This is a $variable'

Output:

This is a $variable

 Double Quotes ("): Inside double quotes, variables and special characters can be
expanded, but some characters (like backslashes) are still treated literally.

Example:

name="Alice"

echo "Hello, $name"

Output:

Hello, Alice

2. Absolute and Relative Paths

Paths in Unix can be either absolute or relative.

Absolute Path

An absolute path starts from the root directory (/) and provides the full location of a file or
directory.

Example:

cd /home/user/Documents
Here, /home/user/Documents is an absolute path starting from the root (/) to the
Documents directory.

Relative Path

A relative path is relative to the current working directory. It does not start from the root
directory but instead uses the current directory as a reference.

Example:

cd Documents

This command will take you to the Documents directory inside your current directory.

You can also use .. (two dots) to refer to the parent directory:

cd ..

This will move you one directory level up.

3. Special Directories in Unix

Unix has a few special directories that are used to navigate through the file system:

 . (Dot): Refers to the current directory.

Example:

ls .

This command lists the contents of the current directory.

 .. (Double Dot): Refers to the parent directory (one level up).

Example:
cd ..

This will move you up one level in the directory hierarchy.

 ~ (Tilde): Refers to the home directory of the current user. This is a shortcut to access
the user's home directory.

Example:

cd ~

This will take you to your home directory, like /home/user on Linux systems.

You can also use ~username to refer to another user's home directory.

Example:

cd ~alice

This will take you to the home directory of the user alice.

Filters
Filters in Unix/Linux

In Unix, a filter is a program or command that reads input from standard input (stdin),
performs some processing on it, and writes the result to standard output (stdout). Filters are
commonly used in pipelines to process text streams.

Types of Filters

1. Text Processing Filters:


These filters are designed to manipulate or process text files and streams. Some
common ones are:

o pr: Format text for printing, adding headers, page numbers, etc.

o cut: Extract specific columns, bytes, or characters from text.

o grep: Search for patterns in text and filter matching lines.

o sed: Stream editor for performing basic text transformations.

o awk: A powerful text-processing tool, often used for extracting and


processing data in columns.

o sort: Sort lines of text.

o uniq: Remove duplicate lines in a text stream.

o tr: Translate or replace characters.


2. File Handling Filters:
Filters that manage file content, metadata, and structure.

o cat: Concatenate and display files.

o head: Display the beginning of a file.

o tail: Display the end of a file.

o wc: Count the lines, words, and characters in a file.

3. Input/Output Filters:
These filters are used to redirect, pipe, or handle input/output streams.

o tee: Reads from standard input and writes to both standard output and a file.

o xargs: Builds and executes command lines from input.

4. System Filters:
These filters are related to system information or processes.

o ps: Report on processes.

o df: Report file system disk space usage.

o du: Estimate file space usage.

o top: Display real-time system information.

1. pr Command (Print)

The pr command is used to format text files for printing. It is commonly used to paginate
long text files and add headers and footers.

Syntax:

pr [options] [file...]

Common Options:

 -l: Specify the number of lines per page.

 -h: Print a custom header.

 -t: Suppress printing of headers and footers.

 -n: Number the lines of the file.

 -w: Set the page width.

Examples:
1. Paginate a File:
Paginate a file to display 40 lines per page.

2. pr -l 40 [Link]

3. Add a Header:
You can add a custom header to each page printed.

4. pr -h "My Custom Header" [Link]

5. Number Lines:
Print a file with numbered lines.

6. pr -n [Link]

7. Suppress Header and Footer:


Use the -t option to suppress headers and footers.

8. pr -t [Link]

9. Set Page Width:


Set the width of the page to 80 characters.

10. pr -w 80 [Link]

2. cut Command

The cut command is used to extract sections from each line of input (such as files or output
from other commands) based on a delimiter (character or byte position).

Syntax:

cut [options] [file...]

Common Options:

 -f: Select specific fields based on the delimiter.

 -d: Specify the delimiter (default is a tab).

 -b: Select specific bytes (rather than fields).

 -c: Select specific characters (instead of bytes or fields).

Examples:

1. Extract Fields (Delimited by Space):


Suppose you have a text file [Link] where fields are separated by spaces:

2. cat [Link]

3. name age city


4. Alice 30 NewYork

5. Bob 25 Boston

To extract the second field (age):

cut -d ' ' -f 2 [Link]

Output:

age

30

25

In this case, -d ' ' specifies that the delimiter is a space, and -f 2 extracts the second field.

6. Extract Columns by Byte Position:


Suppose you have a file [Link] with data in a fixed-width format:

7. cat [Link]

8. Alice12345

9. Bob67890

To extract the first 5 characters (bytes):

cut -b 1-5 [Link]

Output:

Alice

Bob

10. Extract Fields (Delimited by Tab):


For a file with tab-separated values (TSV), such as:

11. cat [Link]

12. name\tage\tcity

13. Alice\t30\tNewYork

14. Bob\t25\tBoston

You can extract the name (first field) and age (second field):

cut -f 1,2 [Link]

Output:

name age
Alice 30

Bob 25

15. Extract Columns by Character Position:


If you need to extract specific characters (rather than fields), you can use the -c
option. For example:

16. cat [Link]

17. Alice12345

18. Bob67890

To extract the first 5 characters:

cut -c 1-5 [Link]

Output:

Alice

Bob

Summary of pr and cut

 pr:

o Formats text files for printing.

o Can paginate, add headers/footers, and number lines.

o Useful for preparing text for printing or viewing with page breaks.

 cut:

o Extracts portions of text based on delimiters, byte positions, or character


positions.

o Used for filtering out specific data from text files (e.g., extracting certain
columns or fields).

Delimiter , sort , uniq and comparing


1. Delimiter in Unix/Linux

A delimiter is a character or sequence of characters that marks the boundary between


different pieces of data. In the context of text files, a delimiter separates fields or columns of
data. Common delimiters include spaces, commas, tabs, and semicolons.

For example, in a CSV (Comma-Separated Values) file, the comma (,) is typically used as the
delimiter to separate different fields. Here's an example of a CSV file:
name,age,city

Alice,30,New York

Bob,25,Boston

Charlie,35,San Francisco

In this example, the comma is the delimiter that separates the name, age, and city fields.

2. paste Command

The paste command is used to merge or combine lines from multiple files into a single file or
stream. It is especially useful when working with data that is structured in columns and you
want to combine them side by side.

Syntax:

paste [options] file1 file2 ...

Common Options for paste:

 -d: Specify the delimiter to use between columns (default is a tab).

 -s: Merge lines of files serially, i.e., paste all lines of the first file together, then all
lines of the second file, and so on.

Examples:

1. Basic Usage:
Combine two files [Link] and [Link] by pasting their corresponding lines side by
side.

2. paste [Link] [Link]

Example Input:
[Link]:

Alice

Bob

Charlie

[Link]:

30

25

35
Output:

Alice 30

Bob 25

Charlie 35

3. Using a Custom Delimiter:


You can specify a custom delimiter using the -d option. For example, using a comma
as a delimiter:

4. paste -d ',' [Link] [Link]

Output:

Alice,30

Bob,25

Charlie,35

5. Merge Lines Serially:


Use the -s option to merge lines from multiple files serially:

6. paste -s [Link] [Link]

Output:

Alice Bob Charlie

30 25 35

3. sort Command

The sort command is used to sort lines of text in a file or stream. It can sort alphabetically,
numerically, or in reverse order.

Syntax:

sort [options] [file...]

Common Options for sort:

 -n: Sort numerically (default is lexicographically).

 -r: Sort in reverse order.

 -k: Specify a key (field) to sort by.

 -t: Specify the delimiter for separating fields.


 -u: Remove duplicates (unique lines).

 -b: Ignore leading spaces when sorting.

 -o: Write the output to a file.

Examples:

1. Basic Sorting (Alphabetically):


Sort a file alphabetically:

2. sort [Link]

Example Input ([Link]):

Charlie

Alice

Bob

Output:

Alice

Bob

Charlie

3. Sort Numerically:
Sort the numbers in a file numerically:

4. sort -n [Link]

Example Input:

20

10

30

Output:

10

20

30

5. Reverse Sorting:
Sort the lines in reverse order:

6. sort -r [Link]
Output:

Charlie

Bob

Alice

7. Sort by Specific Field:


Sort by a specific field (e.g., the second field):

8. sort -k 2 [Link]

Example Input:

Alice 25

Bob 30

Charlie 20

Output:

Charlie 20

Alice 25

Bob 30

9. Sort by Delimited Field:


Specify a delimiter (e.g., comma) and sort by the second field:

10. sort -t ',' -k 2 [Link]

11. Remove Duplicates:


Sort and remove duplicate lines:

12. sort -u [Link]

4. uniq Command

The uniq command is used to filter out duplicate lines from a sorted file. It only works with
sorted data, so you typically use it after sort.

Syntax:

uniq [options] [file...]

Common Options for uniq:

 -c: Prefix each line with the number of occurrences.


 -d: Only show lines that are repeated.

 -u: Only show lines that are unique (no duplicates).

 -i: Ignore case when comparing lines.

Examples:

1. Remove Duplicate Lines:


If the input is already sorted, use uniq to remove duplicate lines:

2. uniq [Link]

Example Input ([Link]):

Alice

Bob

Bob

Charlie

Output:

Alice

Bob

Charlie

3. Count Occurrences of Each Line:


Use the -c option to prefix each line with the count of occurrences:

4. uniq -c [Link]

Output:

1 Alice

2 Bob

1 Charlie

5. Show Only Duplicates:


Use the -d option to show only the lines that are repeated:

6. uniq -d [Link]

Output:

Bob
7. Show Only Unique Lines:
Use the -u option to show only the unique lines (without duplicates):

8. uniq -u [Link]

Output:

Alice

Charlie

5. comm Command

The comm command compares two sorted files line by line. It displays three columns of
output:

1. Lines that are unique to the first file.

2. Lines that are unique to the second file.

3. Lines that are common to both files.

Syntax:

comm [options] file1 file2

Common Options for comm:

 -1: Suppress the first column (lines unique to file1).

 -2: Suppress the second column (lines unique to file2).

 -3: Suppress the third column (common lines).

Examples:

1. Basic Comparison:
Compare two files ([Link] and [Link]):

2. comm [Link] [Link]

Example Input ([Link]):

Alice

Bob

Charlie

Example Input ([Link]):

Bob
Charlie

Dave

Output:

Alice

Bob

Charlie

Dave

3. Suppress Lines Unique to File 1:


Use -1 to suppress the first column (lines unique to file1):

4. comm -1 [Link] [Link]

Output:

Bob

Charlie

Dave

5. Suppress Lines Unique to File 2:


Use -2 to suppress the second column (lines unique to file2):

6. comm -2 [Link] [Link]

Output:

Alice

Dave

7. Suppress Common Lines:


Use -3 to suppress the third column (common lines):

8. comm -3 [Link] [Link]

Output:

Alice

Dave

Summary of Key Commands


 paste: Combines lines of text from multiple files, either side-by-side or serially, with a
custom delimiter.

 sort: Sorts lines of text based on various criteria like alphabetical order, numerical
order, reverse order, etc.

 uniq: Filters out duplicate lines from sorted input, with options for showing only
unique or repeated lines and counting occurrences.

 comm: Compares two sorted files and shows the differences (unique lines from each
file and common lines).

Combing files and Creating file using concat


Combining Commands with Semicolon (;) in Unix/Linux

In Unix/Linux, the semicolon (;) is used to combine multiple commands and execute them
sequentially. This means that the commands will run one after another, regardless of
whether the previous command succeeded or failed. The semicolon is a way to write
multiple commands on a single line.

Concept of Semicolon (;)

The semicolon (;) is a command separator. It allows you to run several commands in
sequence, one after the other.

 Syntax:

 command1 ; command2 ; command3

 Explanation:

o command1, command2, and command3 are executed one after the other in
the same shell session.

o Note: Each command runs independently of the others. Even if one command
fails (returns a non-zero exit status), the next command will still run.

Examples of Using Semicolon (;) in Unix/Linux

1. Basic Sequential Commands

echo "First Command"; echo "Second Command"; echo "Third Command"

 Explanation: This will execute the three echo commands one by one:

1. "First Command" will be printed.

2. "Second Command" will be printed.


3. "Third Command" will be printed.

2. Creating and Listing Files

touch [Link] [Link] ; ls -l

 Explanation:

1. touch [Link] [Link]: Creates two files, [Link] and [Link].

2. ls -l: Lists the files in the current directory with detailed information (including
file permissions, owner, size, etc.).

3. These commands will run one after the other.

3. Changing Directory and Listing Files

cd /tmp ; ls

 Explanation:

1. cd /tmp: Changes the directory to /tmp.

2. ls: Lists the contents of the /tmp directory.

3. Both commands are executed sequentially.

4. Create a Directory and Display Its Contents

mkdir new_folder ; cd new_folder ; touch [Link] [Link] ; ls

 Explanation:

1. mkdir new_folder: Creates a directory named new_folder.

2. cd new_folder: Changes the directory to new_folder.

3. touch [Link] [Link]: Creates two files, [Link] and [Link], inside the
new_folder.

4. ls: Lists the files in new_folder (which will show [Link] and [Link]).

5. Check if a File Exists and Then Display It

[ -f [Link] ] ; echo "File exists or does not exist"

 Explanation:

1. [ -f [Link] ]: Checks if the file [Link] exists.

2. echo "File exists or does not exist": Prints a message.

3. Even if the file doesn't exist, the echo command will still execute.

6. Running Multiple Commands with Condition


You can combine multiple commands that have different purposes using semicolons, such as
modifying a file and then viewing its contents:

echo "Hello, World!" > [Link] ; cat [Link]

 Explanation:

1. echo "Hello, World!" > [Link]: Writes "Hello, World!" to [Link].

2. cat [Link]: Displays the contents of [Link] (which will be "Hello, World!").

7. Cleaning Up Files and Directories

rm -f [Link] ; rmdir new_folder

 Explanation:

1. rm -f [Link]: Removes the file [Link] (using the -f flag to force deletion
without a prompt).

2. rmdir new_folder: Removes the empty directory new_folder.

3. Both commands will execute sequentially.

Creating a File by Concatenating Multiple Files Using Semicolon (;)

You can concatenate multiple files into a new file using the cat command, along with
redirection to create a file.

Example: Concatenate Files into a New File

cat [Link] [Link] > [Link] ; ls -l [Link]

 Explanation:

1. cat [Link] [Link]: Concatenates the contents of [Link] and [Link].

2. > [Link]: Redirects the concatenated output to a new file [Link].

3. ls -l [Link]: Lists the details of [Link].

Assuming [Link] contains:

Hello from file1.

And [Link] contains:

Hello from file2.

After running the above command, [Link] will contain:

Hello from file1.


Hello from file2.

Alternative Example: Concatenate More Than Two Files

cat [Link] [Link] [Link] > merged_output.txt ; cat merged_output.txt

 Explanation:

o This command concatenates the contents of [Link], [Link], and [Link]


into merged_output.txt and then displays the contents of merged_output.txt.

Conclusion

 Semicolon (;) allows you to chain multiple commands together, executing them one
after another, regardless of whether the previous command succeeded or failed.

 Practical Uses include running a sequence of commands to manipulate files, create


directories, or execute any number of tasks in a single line.

 Combining Commands can help automate tasks in a script or command line session
by creating concise workflows that reduce manual intervention.

Foreground and Background Jobs


Jobs in a Unix-like environment can run in two modes: foreground or background.
Foreground Jobs
 Definition: A job running under the direct control and supervision of the user.
 Interaction: It interacts with the user directly, using standard input (keyboard) and
standard output (screen).
 Blocking: While a foreground job is running, you cannot start other jobs or use the
command line until it finishes or is terminated.
 Starting: Simply type the command and press Enter.
 Terminating (Killing): Use Ctrl+C to terminate a running foreground job. After
termination, press Enter to get the command prompt back.
Background Jobs
 Purpose: Used for jobs that take a long time to complete, allowing the user to
continue using the terminal for other tasks.
 Resource Sharing: Background jobs still share the keyboard and monitor with
foreground tasks, though they don't block the command prompt.
 Starting: Add an ampersand (&) at the end of the command.
o Example: [Link] &
o The system will respond with a job number (e.g., [1]) and a Process ID (PID).
 Suspending, Restarting, and Terminating Background Jobs:
o Suspending: Use stop %job_number (e.g., stop %1). This sends a SIGSTOP
signal.
o Restarting (to background): Use bg %job_number (e.g., bg %1). This resumes
a suspended job in the background.
o Terminating (Killing): Use kill %job_number (e.g., kill %1).
Moving Between Foreground and Background
To switch a job between foreground and background, it must first be suspended.
 Moving a Foreground Job to Background:
1. Suspend the foreground job (usually with Ctrl+Z).
2. Use the bg command to move it to the background. (No job number needed
if it was the last suspended job).
 Moving a Background Job to Foreground:
o Use the fg command.
o Syntax: fg %job_number (e.g., fg %1)
o If only one background job exists, fg without a number will bring it to the
foreground.
Unix File Permissions

Unix file permissions control who can do what with a file. There are three categories of users:

 Owner: The user who owns the file.

 Group: Users who are members of the group associated with the file.

 Other (World): All other users on the system.

For each of these categories, three types of permissions can be granted:

 Read (r): Allows viewing the file's contents.

 Write (w): Allows modifying or removing the file's contents.

 Execute (x): Allows running the file as a program (if it's an executable) or entering a
directory.

Permission Indicators (from ls -l command)

When you run ls -l, the output shows permissions like this: -rwxr-xr--.

 The first character indicates the file type (- for a regular file, d for a directory).

 The next nine characters are grouped into three sets of three:

o Characters 2-4 (e.g., rwx): Owner permissions.

o Characters 5-7 (e.g., r-x): Group permissions.

o Characters 8-10 (e.g., r--): Other (world) permissions.

Example: -rwxr-xr--

 Owner: rwx (read, write, execute)

 Group: r-x (read, execute, no write)

 Other: r-- (read, no write, no execute)

Changing Permissions (chmod)

The chmod command is used to change file or directory permissions. There are two main
ways to use it:

1. Absolute (Numeric) Mode

Permissions are represented by numbers:

 Read (r) = 4

 Write (w) = 2

 Execute (x) = 1
To set permissions, you sum the values for each category (owner, group, other).

Number Permission Ref

0 No permission ---

1 Execute permission --x

2 Write permission -w-

3 Execute and write -wx

4 Read permission r--

5 Read and execute r-x

6 Read and write rw-

7 All permissions rwx

Syntax: chmod [owner_num][group_num][other_num] filename

Examples:

 chmod 755 testfile:

o Owner: 7 (rwx)

o Group: 5 (r-x)

o Other: 5 (r-x)

o Result: -rwxr-xr-x

 chmod 743 testfile:

o Owner: 7 (rwx)

o Group: 4 (r--)

o Other: 3 (-wx)

o Result: -rwxr--wx

2. Symbolic Mode

This method uses symbols to add (+), remove (-), or set (=) permissions for specific user
categories.

 Users:

o u: owner
o g: group

o o: others

o a: all (u, g, o)

 Permissions: r (read), w (write), x (execute)

Syntax: chmod [who][operator][permissions] filename

Examples:

 chmod o+wx testfile: Adds write and execute permissions for others.

 chmod u-x testfile: Removes execute permission from the owner.

 chmod g=r-x testfile: Sets group permissions to read and execute (removes write).

 chmod o+wx,u-x,g=r-x testfile: Combines multiple changes in one command.

Changing Owners and Groups

 chown (change owner):

o Changes the owner of a file.

o Syntax: chown user filelist

o Example: chown amrood myfile

 chgrp (change group):

o Changes the group ownership of a file.

o Syntax: chgrp group filelist

o Example: chgrp users myfile

Pipes (|)

A pipe (|) is used to connect the standard output of one command to the standard input of
another command. It allows for sequential processing of data between commands.

Concept: Think of it as connecting a monitor (output) to a keyboard (input) for two different
commands.

Example:

 who | lpr: The output of the who command (list of logged-in users) is sent as input to
the lpr command (print). This effectively prints the list of logged-in users.

You might also like