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

unix short notes

The document provides an overview of Unix file attributes, including ownership, permissions, and the structure of the Unix file system. It explains commands for managing files, such as changing permissions with chmod, using pipelines for command output, and searching files with grep. Additionally, it covers file types, navigation commands, and the use of regular expressions in Unix commands.

Uploaded by

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

unix short notes

The document provides an overview of Unix file attributes, including ownership, permissions, and the structure of the Unix file system. It explains commands for managing files, such as changing permissions with chmod, using pipelines for command output, and searching files with grep. Additionally, it covers file types, navigation commands, and the use of regular expressions in Unix commands.

Uploaded by

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

SELF STUDY SHORT NOTES

 Unix file attributes define the characteristics of a file, such as its permissions,
ownership, and access control. The primary file attributes are:
 Owner: The user who owns the file.
 Group: The group associated with the file.
 Permissions: Defines who can read, write, or execute the file (owner, group, others).
 Changing file permissions can impact security by allowing or restricting access to
files. For example:

 Granting read/write permissions to unauthorized users may compromise sensitive


data.
 Restricting permissions to only specific users helps protect the file from unauthorized
access.

 A Unix file is a collection of data or information stored in a file system that can be
accessed or manipulated by Unix-based operating systems. Every file in Unix is
treated as a sequence of bytes and can represent various entities such as documents,
programs, or system configurations.

 The Unix file system (UFS) is the method by which data is stored and organized on a
Unix operating system. It uses a hierarchical structure that consists of files and
directories. The root directory (/) is at the top of the hierarchy, and everything else
stems from it.

 Absolute Path: Refers to the complete path starting from the root directory. It begins
with / (e.g., /home/user/file.txt).
 Relative Path: Refers to the path relative to the current working directory. It does not
begin with / (e.g., user/file.txt if you're in the /home directory).

 The default home directory for a user in Unix is typically /home/username, where
username is the name of the user. This is where the user’s personal files and
configurations are stored.

 The chown command is used to change the owner and/or group of a file or directory.
It is written as:
 chown owner:group filename

Example: chown john:admins file.txt changes the owner of file.txt to john


and its group to admins.

 Standard Input (stdin): Refers to the input stream (usually from the keyboard).
 Standard Output (stdout): Refers to the output stream (usually to the screen).
 Standard Error (stderr): Refers to the error stream (usually to the screen, separate
from standard output).
 A pipeline (|) connects the output of one command to the input of another command.
Example: ls | grep 'file' will list files and pass the output to grep to search for
the string 'file'.
 Redirection (>, <) sends output to a file or takes input from a file. Example: ls >
files.txt saves the output of ls to files.txt.

In Unix, hidden files are files or directories that start with a dot (.), making them invisible by
default when listing files.
To view hidden files, use the ls -a or ls -al command, where:

 -a: shows all files, including hidden ones.


 -l: lists files with detailed information (permissions, size, etc.).

The chmod command is used to change the permissions of files or directories in Unix. It can
be used to define who can read, write, or execute a file. The command works using symbolic
or numeric modes to specify the permissions for the file owner, group, and others.

Example:

chmod 755 filename

This gives full permissions (read, write, execute) to the owner, and read and execute
permissions to the group and others.

The ls command is used to list the contents of a directory.


Two common options for ls:

 -l: Lists files in long format, showing permissions, owner, file size, and modification
time.
 -R: Lists directories recursively, showing all contents inside subdirectories.

A wildcard is a symbol used in Unix commands to represent one or more characters.


Wildcards are used to match filenames or patterns.

 *: Matches any sequence of characters (zero or more).


 ?: Matches exactly one character.

Example:

ls *.txt

This will list all files with a .txt extension.

The tee command reads from standard input and writes the input to both standard output and
one or more files simultaneously. It is commonly used in pipelines to save the output of a
command while also displaying it on the terminal.

Example:

echo "Hello" | tee file.txt


 . dot refers to the current directory.
 .. double dot refers to the parent directory of the current directory.

These are used to navigate the directory structure. For example:

 cd . will keep you in the current directory.


 cd .. will move you one level up in the directory hierarchy.

In Unix, files are categorized into the following types:

 Regular Files: These contain user data or programs. It can be text, binary files, or
executable files.
 Directory Files: These are files that store references to other files.
 Special Files: These are used to access hardware devices (like printers, hard drives),
represented in the /dev directory. Types include block files and character files.
 Symbolic (Soft) Link: These files contain a reference to another file, acting as a
shortcut.

This will print "Hello" to the terminal and also write it to file.txt.

The /dev/null file is a special file in Unix that discards all data written to it, effectively
acting as a "black hole" for unwanted output. It is often used in redirection to suppress output.

Example:

command > /dev/null 2>&1

This suppresses both standard output and error output from a command.

 grep: A command-line utility that searches for a specified pattern in a file using
regular expressions.
 egrep: Extended grep, which supports extended regular expressions (ERE) without
the need for escaping special characters.

Example:

 grep "pattern" file.txt: Search for "pattern" in the file.


 egrep "patt(er|ern)" file.txt: Uses extended regex to match either "patt" or
"ern".

Redirection in Unix allows you to change the flow of input or output to and from commands.

 Input redirection: Redirects input from a file instead of the keyboard using <.
Example:
 command < input.txt

This takes input for the command from input.txt instead of the standard input
(keyboard).

 Output redirection: Redirects the output of a command to a file using >.


Example:
 command > output.txt

This sends the command output to output.txt instead of the terminal.

 grep: Searches for basic patterns using regular expressions (BRE).


Example:

 grep "pattern" file.txt


 egrep: Searches for extended regular expressions (ERE), with more advanced pattern
matching features.
Example:
 egrep "patt(er|ern)" file.txt
 fgrep: Searches for fixed strings, without using any regular expressions.
Example:
 fgrep "pattern" file.txt

To remove duplicates and sort data in a file, use the sort and uniq commands.
Example:

sort file.txt | uniq

This will:

1. Sort the file alphabetically.


2. Remove any duplicate lines from the file.

A typical command pipeline to process log files could be:

grep "ERROR" logfile.txt | sort | tee sorted_errors.txt

Explanation:

1. grep "ERROR" logfile.txt: Filters log file for lines containing the word
"ERROR".
2. sort: Sorts the filtered error messages.
3. tee sorted_errors.txt: Saves the sorted errors to sorted_errors.txt while
displaying them on the terminal.

This command pipeline helps in logging and monitoring error occurrences while maintaining
a sorted list of them.

 The sort command arranges lines of text in a specified order (default is ascending).
Example: sort file.txt will sort the lines of file.txt alphabetically.

 uniq: Removes duplicate lines from a file (often used after sort). Example: sort
file.txt | uniq removes duplicate lines in file.txt.
 tr: Translates or deletes characters in a file. Example: echo "hello" | tr 'a-z'
'A-Z' will convert 'hello' to 'HELLO'.
 Regular Expressions (regex) are patterns used to match character combinations in
strings. Example: grep "^A" file.txt will find lines in file.txt starting with 'A'.

 Navigation in vi:
o h: Move left
o j: Move down
o k: Move up
o l: Move right
 You can also use /pattern to search for a word.

 chmod changes the permissions of files or directories.


 Syntax: chmod [permissions] [file]
 Example: chmod 755 file.txt assigns rwx (read, write, execute) to the owner and
rx (read, execute) to others.
o Numeric permissions: 7 = rwx, 6 = rw-, 5 = r-x, 4 = r--.
o Example: chmod u+x file.txt grants execute permission to the user
(owner).

 Use the tee command to redirect output to both a file and the screen. Example: ls |
tee output.txt will display the output of ls on the screen and save it in
output.txt.

 vi Editor: It is a powerful, command-based text editor in Unix, requiring less memory


and is available on most Unix systems. It is ideal for editing configurations and
programming scripts.
 Other Editors: Editors like nano and emacs are also used but have more overhead.
 Why vi is preferred: It’s lightweight, always available in Unix environments, and
offers powerful editing features for both beginners and advanced users.

 Example command sequence:


 grep '^A' file.txt | sort > sorted_names.txt
 grep '^A': Filters lines starting with 'A'.
 sort: Sorts the filtered lines.
 > sorted_names.txt: Saves the result in sorted_names.txt.

 Example command:
 grep 'error' /var/log/syslog | sort | uniq > error_log.txt
 grep 'error' /var/log/syslog: Extracts lines containing 'error' from the log.
 sort: Sorts the error messages.
 uniq: Removes duplicate error messages.
 > error_log.txt: Saves the final output to a file.

You might also like