Day 3 Task: Basic Linux Commands
Table of contents
- Task: What is the linux command to
- To view what's written in a file.
- To change the access permissions of files.
- To check which commands you have run till now.
- To remove a directory/ Folder.
- To create a fruits.txt file and to view the content.
- Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
- To Show only top three fruits from the file.
- To Show only bottom three fruits from the file.
- To create another file Colors.txt and to view the content.
- To find the difference between fruits.txt and Colors.txt file.
Task: What is the linux command to
To view what's written in a file.
cat filename
: Displays the entire contents of a file.
To change the access permissions of files.
To change the access permissions of files.
Certainly! Changing the access permissions of files in Linux is a common task. Here's a practical guide with clear examples:
Using Octal Notation:
Numeric Representation:
chmod [owner][group][others] filename
Examples:
# Give read, write, and execute permissions to the owner, and read permission to others
chmod 744 filename
In this example:
7
(owner) grants read (4) + write (2) + execute (1).4
(group) grants read.4
(others) grants read.
Symbolic Representation:
chmod [who][operator][permissions] filename
Examples:
chmod u+x filename
In this example:
u
(owner) gets the execute permission added.
Using Symbolic Notation:
Symbolic Representation:
chmod [who][operator][permissions] filename
Examples:
chmod o-w filename
In this example:
o
(others) has write permission removed.
Practical Tips:
Readable Permissions:
r
for read,w
for write,x
for execute.Combine them logically for quick understanding.
Quick Reference:
chmod 755
gives read, write, and execute to the owner, and read/execute to group and others.
Symbolic Clarity:
u
(owner),g
(group),o
(others),+
(add),-
(remove).
Examples:
Give Full Access to Owner, Read-Only to Others:
chmod 644 filename
Add Execute Permission for Everyone:
chmod a+x filename
Restrict Access to Owner Only:
chmod 700 filename
Feel free to adapt the examples to your needs, and always consider security implications when adjusting file permissions.
To check which commands you have run till now.
Usinghistory
command:
history
This will display a list of previously executed commands with line numbers.
To remove a directory/ Folder.
To remove a directory (folder) in Linux, you can use the
rmdir
orrm
command. Here are the basic commands:Using
rmdir
command:- The
rmdir
command is used to remove empty directories.
- The
rmdir directory_name
Note: If the directory is not empty, rmdir
will not remove it.
Using
rm
command:- The
rm
command, when used with the-r
(or-R
) option, can remove directories and their contents recursively.
- The
rm -r directory_name
Warning: Be cautious when using rm -r
as it will remove the directory and its contents without confirmation.
Always be careful when using the rm
command, especially with the -r
option, to avoid unintentional removal of important data. Double-check your command before executing it.
To create a fruits.txt file and to view the content.
Creating fruits.txt
:
touch fruits.txt
Adding Content to fruits.txt
:
You can use a text editor like nano
, vim
, or even echo
to add content to the file. Here's an example using echo
:
echo "Apple" >> fruits.txt
echo "Banana" >> fruits.txt
echo "Orange" >> fruits.txt
Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > devops.txt
This command uses the
-e
option forecho
to enable interpretation of backslash escapes, and\n
is used to represent newlines between each fruit. The output is then redirected (>
) to thedevops.txt
file.After running this command, the
devops.txt
file will contain each fruit on a new line.
To Show only top three fruits from the file.
You can use the head
command to display the top (first) three lines from the file. Here's how you can do it:
head -n 3 devops.txt
This command uses the -n
option to specify the number of lines to display, and in this case, it will show the top three lines from the devops.txt
file.
After running this command, you should see the names of the top three fruits in the file.
To Show only bottom three fruits from the file.
You can use the tail
command to display the bottom (last) three lines from the file. Here's how you can do it:
tail -n 3 devops.txt
This command uses the -n
option to specify the number of lines to display, and in this case, it will show the bottom three lines from the devops.txt
file.
To create another file Colors.txt and to view the content.
echo -e "Red\nGreen\nBlue" > Colors.txt
cat Colors.txt
These commands:
Use
echo
with the-e
option to create a fileColors.txt
with three colors on separate lines.Use
cat
to display the content ofColors.txt
.
After running these commands, you'll have a file named Colors.txt
with the specified colors, and you'll see the content displayed in the terminal using cat
.
Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
To find the difference between fruits.txt and Colors.txt file.
To find the difference between two files, you can use the
diff
command. Here's how you can find the difference betweenfruits.txt
andColors.txt
:This command will display the lines that differ between the two files. If there are no differences, it will produce no outpu