[Day 3] 4. Some more commands
1. Head and Tail Commands:
-
: Displays the first part of files.
headhead -5 filename.txt- This command prints the first 5 lines of .
filename.txt - You can also use negative numbers with , but this feature might vary based on the implementation.
head
- This command prints the first 5 lines of
-
: Displays the last part of files.
tail-
Example:
tail +5 filename.txt -
This command starts printing from the 5th line onward to the end of
.filename.txt -
Unlike
,headdoes not accept negative numbers.tail
-
2. Echo Command:
- : Used to display messages or output text to the console.
echo- Example:
echo "Hello Ojas"- This command prints on the console.
Hello Ojas
- This command prints
- Example:
3. Translate Command (tr):
- : Used to translate or delete characters.
tr- Examples:
-
Translate space to
:#echo "text with spaces" | tr ' ' '#'- This replaces spaces with .
#
- This replaces spaces with
-
Squeeze multiple spaces into one:
echo "text with multiple spaces" | tr -s ' '- This command condenses consecutive spaces into a single space.
-
Change case:
echo "Hello World" | tr 'A-Z' 'a-z'- This converts uppercase letters to lowercase.
-
- Examples:
4. Cut Command:
- : Used to remove sections from each line of files.
cut-
Example:
cut -d ',' -f 1 filename.csv-
This command cuts and displays the first field (column) from a CSV file, using a comma as the delimiter.
-
You can also specify ranges:
cut -d ',' -f 1-3 filename.csv -
This would cut and display fields 1 through 3.
-
-
Meta-characters/Wildcards in Shell:
- : Represents all characters.
* - : Represents a single occurrence of any character.
? - : Used to separate multiple commands on a single line.
;
Changing Permissions with chmod:
-
: Changes the file permissions (also called access or mode).
chmod- Examples:
-
Add write permission:
chmod +w filename.txt -
Add execute permission for all:
chmod +x filename.txt
-
- Examples:
-
Setting permissions using user types:
- : User
u - : Group
g - : Others
o - Example:
chmod u+x,g-w filename.txt
-
Octal Notation: Permissions can also be changed using octal numbers.
- For example, if the original permissions are , the octal number would be
rw- rw- r--.664 - You can set this using:
chmod 664 filename.txt
- For example, if the original permissions are
Why Are Permissions Required?
Permissions ensure that only authorized users can read, write, or execute files, providing security and access control.
Changing File Ownership with chown:
-
: Changes the ownership of a file.
chown- Example:
chown username filename.txt
- Example:
-
Change group ownership:
- Example:
chown :groupname filename.txt
- Example:
-
Change both owner and group:
- Example:
chown username:groupname filename.txt
- Example:
-
To change ownership of a directory and all its sub-directories, use
(recursive):-Rchown -R username directoryname/
Find Command:
- : Searches for files in a directory hierarchy.
find-
Search by name:
find -name test.txt -
Search by exact path:
find /home/sunbeam -name test.txt -
Search by file type:
-
Simple files:
find -type f -
Directories:
find -type d
-
-
Search by size:
find . -size +1M- This searches for files larger than 1 MB in the current directory.
-
Read Next
[Day 4] 1. Few more Commands
## **Types of Commands in Linux** #### **1. Internal Commands** - **Definition**: Commands implemented within the shell program itself. - **Exe...
[Day 4] 3. Types of Operating System
### **Resident Monitors** !3. Resident Monitors.png - **Input Method:** Code was written using a typewriter-like device on punch cards. - **Pro...