0% found this document useful (0 votes)
4 views

Basic Commands Final

Uploaded by

gowdanish34
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Basic Commands Final

Uploaded by

gowdanish34
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 198

Software to practice Unix commands in

windows
windows software view
In ubuntu software view
UNIX ARCHITECTURE/COMPONENTS
COMMAND STRUCTURE

• Syntax of command

• $COMMAND [OPTIONS] [ARGUMENTS]

• Example
• $ ls –l file.txt
• –l is a option
• file.txt is a argument(usually file name)
• The command with its arguments
• The first word in the command syntax is called as command,
• all subsequent words are actually called as arguments.
• Options are also arguments but given a special name because their
list is predetermined.
• There should be at least one space to separate command and
arguments.
OPTIONS:

• There is a special type of argument, that is mostly used with a ‘-‘


sign.
• Such a arguments are called options.

• Normally precede by minus sign


• Example $ls –l file.txt
• No white spaces between – and l
FILE NAME ARGUMENTS
• Many unix commands use a filename as argument, so the command
can take input from the file. It is even possible to give multiple
filename as argument.
• E.g :
• $ls –l chap1.txt chap2.txt
• The command with its arguments and options is known as command
line.
EXCEPTIONS:

• There is exceptions to the general syntax of commands.

• There are commands (pwd) that don’t accept any arguments.


• . Some commands like
• Ex : pwd
Part 1 : Execution following basic UNIX commands

1. ls ,ls –ld , ls –d,ls-lh,ls-a,ls-R


2. cat, wc,
3. cd ,
4. od ,
5. mkdir, rmdir
6. echo ,
7. date ,
8. mv ,
9. cal ,
10. pwd,
11. touch,
12. sort
13. read,
14. Clear
15. ps
16. whoami,
17. alias ,
18. find ,
19. test
20. expr
21. set
22. shift
23. Type
24. ls-lh
1.ls list directory contents
• command to list your directories and file

• With options it can provide information about the size, type of file,
permissions, dates of file creation, change and access

• syntax
• $ ls [options] [argument]
• Complete list of filenames in the current directory arranged in ASCII
collating sequence with one file names in each line

• ASCII collating sequence : numbers first ,lower case , then upper


case
Example
• list of file name arranged in order like file name start with 1.
Numbers 2. Lower case
• 3. upper case letters
• Directory contain many files
• User interested in knowing a specific files
• To check whether files available in directory
• Use ls command with file name.
• If specific file not available then system clearly says : no such file or
directory
Directory we two files text file ,directory file
2. ls –l
• List files and directories with detailed information:–
1.Permissions: The first column shows the file or directory permissions. In the example
above, "drwxr-xr-x" indicates a directory with read, write, and execute permissions for
the owner, and read and execute permissions for the group and others. "rw-r--r--"
indicates a regular file with read and write permissions for the owner and read-only
permissions for the group and others.
2.Number of links: The second column shows the number of hard links to the file or
directory.
3.Owner: The third column shows the owner of the file or directory.
4.Group: The fourth column shows the group associated with the file or directory.
5.File size: The fifth column shows the size of the file in bytes.
6.Modification time: The sixth column shows the date and time when the file or directory
was last modified.
7.Name: The seventh column shows the name of the file or directory.
• In the example, "total 1" at the top indicates the total disk space used by the listed files.
Memory
• total of 1 blocks memory occupied by files on disk

• Each block consists of 512 bytes


ls –l command displays long list of 7 file
attributes.

first column
ls –l command displays long list of 7 file
attributes.
• File type and permissions:
The first column shows types and permissions associated with each
file.
• ‘-‘ indicates file is a ordinary file.
• ‘d’ indicates file is a directory.
• b or c indicates device file
ls –l command displays long list of 7 file
attributes.
• File type and permissions:
9 characters in first column shows permission.

• A file can have 3 types of permissions,


• i.e. read(r), write(w) and execute.(x)
Permissions
ls –l command displays long list of 7 file
attributes.

• The second column indicates


the number of links associated with each file. This is actually number of filenames
maintained by the system of that file.

• Unix lets a file have as many names as we want it to have, even


though there is a single file on disk.
ls –l command displays long list of 7 file
attributes.

Second column
ls –l command displays long list of 7 file attributes.

• Third column

• Ownership:
• The third column shows owner of the file, owner is the person who
has created the file. Owner can create, modify or remove files in a
directory
ls –l command displays long list of 7 file
attributes.

Third column
ls –l command displays long list of 7 file attributes.

• Fourth column

• Group Ownership:
• When opening a user account, the system administrator assigns the
users to some group.
• The fourth column represents group owner of the file.
ls –l command displays long list of 7 file
attributes.

Fourth
column
ls –l command displays long list of 7 file attributes.

• Fifth column

• File size:
• The fifth column shows the size of the files in bytes. The amount of
data it contains.
• It is the character count of the file and not a measure of disk space that
it occupies.
• The space occupied by a file on the disk is usually larger than this
because files are written to a disk blocks of 1024 bytes.
ls –l command displays long list of 7 file
attributes.

Fifth column
Note : size of file 4byte for 4charcter
sixth column
• Modification time: The sixth column shows the date and time when
the file or directory was last modified.

six column
Seventh column

• Name: The seventh column shows the name of the file or directory.

Seventh
column
Note : syntax of command
• $COMMAND [OPTIONS] [ARGUMENTS]
ls -d
The ls -d command, without additional arguments, is used to list directories
themselves, rather than their contents.
ls –ld

• ls -ld command is used in Unix-like operating systems (such as Linux) to display information about a
directory.

• Here's a breakdown of the command:


•ls: This is the command to list directory contents.
•-l: This option is used to display the information in a detailed or long format.
•-d: This option is used to list information about the directory itself, rather than its contents.

d means directory
ls-lh
• The ls -lh command in Linux/Unix is used to list files and directories in the current
directory with human-readable file sizes
• -l option lists files in long format (which includes file permissions, number of links,
owner, group, file size, and modification date)

• the -h option makes the file sizes human-readable (i.e., showing sizes in KB, MB,
GB instead of just bytes).

Syntax :
ls -lh [directory]
• -l: Long listing format.
• -h: Human-readable sizes.
• [directory]: Optional. Specify the directory you want to list. If omitted, it lists the
contents of the current directory.
Example

•total 104K:This indicates the total size of the files listed, which is 104 kilobytes. This is the sum of all file sizes in
the current directory.
•1.File name: 'unix-Self audit961.docx' (with spaces, enclosed in quotes).
•Size: 101K (kilobytes).
•Last modified: January 20, 2024.
explanation
• total 104K:This indicates the total size of the files listed, which is 104
kilobytes. This is the sum of all file sizes in the current directory.

File name : 'unix-Self audit961.docx


1.File name: 'unix-Self audit961.docx' (with spaces, enclosed in quotes).
Size: 101K (kilobytes).
Last modified: January 20, 2024.
ls -R
• The ls -R command in Linux/Unix is used to list all files and directories recursively,
displaying the contents of all subdirectories as well. The –R flag stands for
"recursive.“

• Syntax :
• ls -R [directory]
• -R: Lists all files and directories recursively, showing the contents of each
subdirectory.
• [directory]: Optional. Specify the directory you want to list. If omitted, it lists the
contents of the current directory and all subdirectories.
Example

recursive list of subdirectory


Explanation
1.The output of the ls command.
• B/ and a/ are directories.
• 2. The ls -R command lists all the directories and their contents
recursively.
• The current directory (.) contains two subdirectories: B/ and a/
• .Inside B/, there are two files: f1.txt and unix-Self audit961.docx.
• Inside a/, there is a subdirectory A/ and a file u.docx.
• Inside A/, there is a file n.docx.
ls –a
• The ls -a command in Linux/Unix is used to list all files and directories in the
current directory, including hidden files.
• By default, files and directories whose names begin with a dot (.) are considered
hidden and are not displayed when using the basic ls command.
• The -a flag reveals these hidden files.

• Syntax: ls -a [directory]
• -a: Shows all files, including hidden ones (files starting with .)
• [directory]: Optional. Specify the directory to list. If omitted, the command will
list the contents of the current directory.
Files and folder in unix1
Example :

• ls -a shows both visible and hidden files in the current directory.


• The hidden file here is .n.txt, which wasn't shown in the previous ls command
• .. and .. are system-generated references to the current and parent directories,
respectively.
2.Cat command
• 1. display the contents of files.

• 2 Redirecting Output to a New File


1. display the contents of files.
2.Cat command : Redirecting Output to a New
File

Listing of file

Display content of file

1. create new file (p4.txt) 2. store information from


p1.txt to p4.txt

U can check the content of new file and also new file created in
directory
2.1 : to overwrite the contents of an existing file
The cat command is often used to concatenate and display the content
of files. If you want to overwrite the contents of an existing file using
the cat command, you can use the following

syntax:
2. 2 : create a file and adding contents
Example of 2.2 :

Press Enter
Cltrl + d
2.3 : append the text ( by using >> )
Example : Appending existing file

Display content of old file

Add new contents to existing file


Press ctrl +d

display contents of existing file


3. WC = word count

the basic syntax of the wc command is as follows:

Here are some commonly used options:


•-l: Display the number of lines in the file.
•-w: Display the number of words in the file.
•-c: Display the number of bytes in the file.
•-m: Display the number of characters in the file.
Example :

•-l: Display the number of lines in the file.

•-w : Display the number of words in the file.

-c : Display the number of bytes in the file.


4.date command

• used to display or set the current date and time on Unix-like operating
systems
Various options of date command
+%FORMAT: Display the date and time using a specific format.
The FORMAT argument specifies the format of the output.

For example
%Y represents the year,
%m represents the month,
%d represents the day,
%H represents the hour,
%M represents the minute, and
%S represents the second
Example
• %Y represents the year
5.calendar

• cal : Shows current month calendar on the terminal with the


current date highlighted.
6.clear
• The clear command is a Unix and Unix-like operating system
command used to clear the terminal screen or console. When you run
the clear command, it removes all previous commands and output
from the terminal, giving you a clean and empty screen.
Example : clear
7. The pwd command stands for "print working directory.
The pwd command stands for "print working directory." It is a command used in Unix and Unix-like
operating systems to display the
current working directory.
When you run the pwd command, it prints the full pathname of the current working directory to the
terminal.

Example :
8. echo command
• Display text or output to the screen. It's often used for printing
messages, variables, or other information.
9.Whoami command

The whoami command is used to display the username of the current user in a Unix

Example :
9.Cd command : change the current working directory

The cd command is used to change the current working directory in a command-


line interface, such as the Unix/Linux shell or Windows Command Prompt.

Example :
Move to Parent Directory
• This command is used to move up one level in the directory structure
cd use to move to sub directory

Syntax
Listing of files Listing of all files

We will move to sub directory

Listing of files
10. mkdir
• mkdir (make directory) command in Unix/Linux and other command-
line interfaces is used to create new directories.

• Syntax:
• mkdir [options] directory_name
• directory_name: The name of the directory (or directories) you want to
create. You can specify an absolute or relative path.
• Options:-p: Creates parent directories as needed.
10.
The mkdir command is used to create a new directory (folder) in a file system.
The name "mkdir" stands for "make directory.“

Listing of files

Creating directory

We can check whether new folder created


11. rmdir
• The rmdir command is used to remove (delete) empty directories in a file
system.

• Syntax :

• rmdir my_directory
• This command removes the directory my_directory, but only if it is empty.
Example :
The rmdir command is used to remove (delete) empty directories in a file system.

Listing of files

Remove directory

Listing of files
12.mv command
The mv command in Unix-like operating systems is used to move or rename files and directories.
Here are some examples of how to use the mv command:
Listing of files

Copy file to directory

Change directory to abc

Checking of file existing directory


12.1 : mv
Example : rename to old file (f2 rename as f1)
12.2
Listing of file

Move source directory destination


location

File is already moved

Now we xyz location

Check for files


Note : Pipeline command
• Output of one command passed and become of input of other
command
Here's a breakdown of how pipelines work:

1.Command1: Produces some output.


2.| (Pipe): Sends the output of Command1 as input to Command2.
3.Command2: Processes the input from Command1 and produces its
own output.
4.| (Pipe): Sends the output of Command2 as input to Command3.
5.Command3: Processes the input from Command2 and produces the
final output.
Become input
Output perform wc command
Contents of file passed
Example
Note
13.od
• The od (octal dump) command in Unix/Linux is used to display the
contents of a file in different formats like octal, hexadecimal, decimal,
or ASCII.
• It's useful for inspecting binary files or seeing the raw data in files.
• Key Points about od:
• Basic Usage:
• Displays the file content in octal (base 8) format by default.
• Syntax : od filename
14.touch
Example :
15.sort command
The sort command is a Linux/Unix command-line utility used for
sorting lines of text files.
It can be used to sort data alphabetically, numerically, or based on other
criteria

Syntax :
• 1.option : -n
• Numeric sort
Example
2. Sort a file in reverse order:

Syntax
2. Sort a file in reverse order:

Example

Content of file

Sorted reversed order


-nr option
• nr options used to -nr option:
• To sort a file with numeric data in reverse order
16.ps
17. alias
• Key Points about alias:
• Basic Usage: Create an alias by using the following format:

• Syntax :

• alias shortcut_name='command_to_run'
17.
18: bash command in windows

Mention the printable path


Note : in ubuntu
Note : We created file by using vi editor
Components of shell script

1.The first line of a script, often starts with #!/bin/bash, indicating the path to the shell
interpreter.

2.Comments: Lines starting with # are comments and are ignored by the shell. You
can use comments to explain what the script does.
Press i : enter into append mode
To save press ESC and shift : (colon) wq
To run the command
• To make script executable we use chmod
• $ chmod +x p1.txt

• Run command
• sh filename.txt or sh filename
1.File contents

To make script executable we use chmod

We see the output


19.read
Contents of file

Output
20.find command
• It allows users to search for files based on various criteria such as file
name, size, modification time, and more.
1. Find files by name:
• $ find ./ -name "p1.txt"

• we consider use only starting directory ( ./ means current directory)


• Search _criteria : we consider only name of file (-name indicates consider only
name and also specify name of file )
Example : search for text files and directory
21.type command
The type command in Unix-like operating systems is used to display information
about a command type, such as whether it is an external command, built-in
command

Syntax
type command : 1. Check if a command is an
external command:

If we get path it indicates external


command
type command : 1. Check if a command is an
internal command:

If we get shell in in-built it indicates


internal command
Note : combining two commands using ;
In Unix-like operating systems, you can combine multiple commands on a single line using the
semicolon (;). The semicolon acts as a command separator.
Each command is executed sequentially, regardless of whether the previous command succeeds or
fails.

Here's an example:

$command1 ; command2
Output of Command 1

Output of Command 2

Output of Command 1 and command 2


Note : echo $?
The echo $? command in Unix-like operating systems is used to display the exit
status of the last executed command.

• In Unix, the exit status is a numeric value that represents the result of a command.
• A value of 0 : usually indicates success
• while non-zero values : typically indicate an error or failure.
Display contents , command successful

A value of 0 : usually indicates success

while non-zero values : typically indicate an error or failure.


• Assign the value to variables we will see the result

1. Assign value 5 to x and stored in $

2. Display the result , using $


22.test
1.Initialize
2.Test equality between two variables

x =5 and y=7
x not equal y ,so
result is false
-lt : less than equal

x =5 and y=7
x less than y so result True
We will apply test command only for integer

We get false result


f : File exists and a regular file

0 : indicates yes file exists and its regular file


w : File exists and is writable
• we can check only permissions related to user

W : Test whether file is writable or not

0 : indicates yes file is writable


Example of String comparison
• Consider three string variables s1 , s2 ,s3 and assign the values and by
using operators compare the this string variables.
• 1. assign values to the string variables s1 ,s2,s3 (null string )
• By using test command and operators compare two string variables s1
and s2

// two string are not equal


z : null string
• whether String s3 is null string or not

0 : indicates Yes: string s3 is null string


23. expr
The expr command in Unix is used for evaluating expressions. It can perform
various operations, such as arithmetic, string manipulation, and logical comparisons.

The basic syntax of the expr command is:

here, expression is the expression that you want to evaluate.


The expr command returns the value of the expression.
Example : Arithmetic Expressions: addition

Output: 8
Arithmetic Expressions : Division

Output: 5
24.Set
The set command can be used to set or modify positional parameters.

Syntax

set -- arg1 arg2 arg3


Here arg1, arg2, and arg3 called positional parameters
arg1 is first positional parameter (represents as $1)
arg2 is second positional parameter (represents as $2)
arg3 is third positional parameter (represents as $3)
Example
set or modify positional parameters.

Display : first positional parameter

Display : second positional parameter

Display : third positional parameter


Note : ` special character used in set
command
Set command used in command substitution
` = special symbol

$* indicates display all positional parameters

Display first positional parameter

Display second positional parameter


Positional parameter used in echo statement
24.Shift command
• used to shift the positional parameters to the left

• The basic syntax of the shift command is as follows:

Shift [ n ]

• Here, n is an optional parameter that specifies the number of positions to shift. If n is not
provided, it defaults to 1, meaning the positional parameters are shifted left by one position.
Step 1: Set value to positional parameters

$1 $2 $3 $4 $5 $6 $7
11 12 13 14 15 16 17

Step 2 :Apply shift 1

12 13 14 15 16 17
$1 $2 $3 $4 $5 $6 $7
Step 1 : set positional parameters using set command

$1 $2 $6 $7
$3 $4 $5

We can display positional parameter using echo statement like $1 $2 $3 for first second and third positional parameters

Output
Step 2 : shift this positional parameters to left
• After setting the positional parameters we will shift those parameters by using shift commands
• Example
• 1. Set the positional parameters

2. Display positional parameters

3. Shift one place to left side means


second positional parameter become
first positional parameter
After shifting second positional parameter become first positional parameter so on
grep
grep: searching for a pattern

Syntax :
$grep [options] pattern filename(s)
Example .Basic search
1
To search for the term “college " in a file named p2

1.1 display contents of file p2


1.2 To search for the term “college " in a file
named p2

So, the command searches the file p2 for any occurrences of the
word "college" and outputs the lines that contain it. Since "college"
is the only matching line in this case, it is printed as the result.
Unix
tr command
awk
• Used for text processing and extracting specific fields in a file.

• Syntax:

• Awk 'pattern {action}’ file


Example :
• $ cat p1
• Output : cse ise csbs ec

• Apply awk command to file p1

• $ awk '{print $2}' p1


• Output : ise
Explanation
1. $ cat p1
• This command displays the contents of the file named `p1`.
• The output: cse ise csbs ec
• indicates that the file contains a single line with four words separated by spaces.
2. $ awk '{print $2}' p1
• This command uses `awk`, a powerful text-processing tool, to process the file `p1`
and output specific data.
awk: Invokes the `awk` command-line tool.
• `{print $2}` : `$2 Represents the second field (column) in the current line of the
file. Fields in a line are separated by whitespace (by default).
• print: Outputs the value of the specified field.
Execution Flow:
• awk` reads the line in the file `p1`: `cse ise csbs ec`.
It splits the line into fields based on whitespace:
• $1 = cse
• $2 = ise
• $3 = csbs
• $4 = ec
• The command `{print $2}` instructs `awk` to output the second field
(`ise`). This is the second word from the file.
paste
• Joins lines of files horizontally (side by side).

• Syntax :
• paste [options] file1 file2
• file1 and file2: Input files whose lines will be combined horizontally.
• Options:-d: Specify a custom delimiter instead of the default tab.
• -s: Merge all lines from each file serially.
Example 1 :
The paste command combines the lines of two files
$ cat p3 horizontally. By default, the fields are separated by a tab
character.
1
2
3
Apply paste command
$ paste p2 p3
Output : A 1 Execution:Combines corresponding lines from p2 and p3 side
B 2 by side.
Uses a tab character as the default separator.
C 3
Example 2 : -d option
$ cat p3 The -d option in the paste command specifies a custom
delimiter. In this case, the delimiter is a hyphen (-).
1
2
3
Apply paste command – d
$ paste -d "-" p2 p3
• Output : A-1
• B-2 Execution:Combines lines from p2 and p3.Replaces the default
tab character with a hyphen.
• C-3
Example 2 : -d option
The -s option merges all lines from each file serially instead of
$ cat p3 line by line.
1
2
3
Apply paste command – d
$ paste -s p2 p3 Execution:Reads all lines from each file.Combines all lines from
p2 into one line and does the same for p3.Uses the default tab
• Output : A B C
character to separate entries.
• 1 2 3
top Command
• top command displays real-time system information, such as
processes, CPU usage, memory usage, and more. It's a powerful tool
for monitoring system performance.

• Syntax:
• top [options]
Key Features
• Provides a dynamic, real-time view of system performance.
• Displays processes and their resource consumption.
• Updates automatically at regular intervals (default: every 3 seconds).
• Fields in the top Output:
• Tasks: Total number of processes and their states (running, sleeping, stopped, or zombie).
• CPU Usage: Percentage of CPU time used by user, system, idle, etc.
• Memory Usage: Total and used physical memory (RAM).
• Swap Usage: Total and used swap memory.
• Process List: PID: Process ID.USER: User running the process.
• %CPU: Percentage of CPU time consumed.
• %MEM: Percentage of memory used.
• COMMAND: The command or program running.
env
• Shows the current environment variables or runs a command with a
modified environment.

• Syntax
• env [options] [command]
Example :
• Display all environment variables:
Jobs

• Displays the list of running or stopped jobs in the current shell.

• Syntax :
• jobs [options]

• Example:
List all jobs:
chmod Command in Linux

• The chmod command is used to change the permissions of a file or


directory, allowing or restricting access to users.

• Syntax:
• chmod [permissions] file

• permissions: Specifies the access rights using either symbolic or


numeric notation.
• file: The name of the file or directory whose permissions are being
changed
Permission Categories:
• Owner (u): The user who owns the file.
• Group (g): A group of users with shared access.
• Others (o): Everyone else.
Types of Permissions:
• Read (r): View the file's contents or list directory contents.
• Write (w): Modify the file or directory.Execute
• (x): Run a file as a program or enter a directory.
Numeric Format:

• Permissions are represented by numbers:


• 4: Read (r)
• 2: Write (w)
• 1: Execute (x)
These values are summed to set permissions:
• 7: Read, write, and execute (rwx)
• 6: Read and write (rw-)
• 5: Read and execute (r-x)
• 4: Read only (r--)
Example 1:

• 1. Grant read, write, and execute permissions to the owner:

• chmod 700 file.txt

• Explanation:
• 7: Full permissions (read, write, execute) for the owner.
• 0: No permissions for the group.
• 0: No permissions for others.
Example 2.
• Grant read and execute permissions to everyone:

• Command : chmod 755 file.txt

• Explanation:
7: Full permissions for the owner.
5: Read and execute for the group.
5: Read and execute for others.
• Symbolic Format:
• You can use symbols to modify specific permissions:
• + ( Add a permission).
• - (Remove a permission.)
• = (Set exact permissions.)
• Example:
• Grant execute permission to the owner:
Example of Symbolic Format:

• chmod u+x file // add execution permission for user

• chmod o-w file.txt // Remove write permission from others:


Example :
• Set read-only permissions for everyone:

• chmod a=r file.txt

You might also like