Basic Linux Commands Day 3 - #90Days of DevOps challenge

Basic Linux Commands Day 3 - #90Days of DevOps challenge

·

6 min read

#Devops #TrainwithShubham #90DaysOfDevOps #Linux

Table of Contains

Basic Linux Commands

Task: What is the linux command to

  1. To view what's written in a file.

  2. To change the access permissions of files.

  3. To check which commands you have run till now.

  4. To remove a directory/ Folder.

  5. To create a fruits.txt file and to view the content.

  6. Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

  7. To Show only top three fruits from the file.

  8. To Show only bottom three fruits from the file.

  9. To create another file Colors.txt and to view the content.

  10. Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.

  11. To find the difference between fruits.txt and Colors.txt file.

1) To view what's written in a file.

You can also use the cat command to display the contents of one or more files on your screen. Combining the cat command with the pg command allows you to read the contents of a file one full screen at a time. You can also display the contents of files by using input and output redirection.

The pg, more, and page commands allow you to view the contents of a file and control the speed at which your files are displayed.

You can also use the cat command to display the contents of one or more files on your screen. Combining the cat command with the pg command allows you to read the contents of a file one full screen at a time.

You can also display the contents of files by using input and output redirection.

Using the pg command

Use the pg command to read the files named in the File parameter and writes them to standard output one screen at a time.

Using the more or page commands

Use the more or page command to display continuous text one screen at a time.

cat command

Use the cat command to read each File parameter in sequence and writes it to standard output.

sas@ubuntu:~/Desktop/Day3$ echo "Hello" >file.txt
csas@ubuntu:~/Desktop/Day3$ cat file.txt 
Hello

a) Cat

This is the simplest and perhaps the most popular command to view a file in Linux.

b) nl

The nl command is almost like the cat command. The only difference is that it prepends line numbers while displaying the text in the terminal.

c) Less

Less command views the file one page at a time. The best thing is that you exit less (by pressing q), there are no lines displayed on the screen. Your terminal remains clean and pristine.

d) Head

Head command is another way of viewing text file but with a slight difference. The head command displays the first 10 lines of a text file by default.

e). Tail

Tail command in Linux is similar and yet opposite to the head command. While head command displays file from the beginning, the tail command displays file from the end.

By default, tail command displays the last 10 lines of a file.

2)To change directory permissions in Linux, use the following:

[if !supportLists]1. [endif]chmod +rwx filename to add permissions.

[if !supportLists]2. [endif]chmod -rwx directoryname to remove permissions.

[if !supportLists]3. [endif]chmod +x filename to allow executable permissions.

[if !supportLists]4. [endif]chmod -wx filename to take out write and executable permissions.

csas@ubuntuc:~/Desktop/Day3$ ls -l file.txt 
-rw-rw-r-- 1 csas csas 40 Mar 20 22:47 file.txt
csas@ubuntu:~/Desktop/Day3$ chmod 777 file.txt 
csas@ubuntu:~/Desktop/Day3$ ls -l file.txt 
-rwxrwxrwx 1 csas csas 40 Mar 20 22:47 file.txt

Linux file ownership

In Linux, there are three types of owners: user, group, and others .

Linux User

A user is the default owner and creator of the file. So this user is called owner as well.

Linux Group

A user-group is a collection of users. Users that belonging to a group will have the same Linux group permissions to access a file/ folder.

You can use groups to assign permissions in a bulk instead of assigning them individually. A user can belong to more than one group as well.

Other

Any users that are not part of the user or group classes belong to this class.

Linux File Permissions

File permissions fall in three categories: read, write, and execute.

Read permission

For regular files, read permissions allow users to open and read the file only. Users can't modify the file.

Similarly for directories, read permissions allow the listing of directory content without any modification in the directory.

Write permission

When files have write permissions, the user can modify (edit, delete) the file and save it.

For folders, write permissions enable a user to modify its contents (create, delete, and rename the files inside it), and modify the contents of files that the user has write permissions to.

Execute permission

For files, execute permissions allows the user to run an executable script. For directories, the user can access them, and access details about files in the directory.

Below is the symbolic representation of permissions to user, group, and others.

Symbolic representation of permissions

Note that we can find permissions of files and folders using long listing (ls -l) on a Linux terminal.

3) To check which commands you have run till now.

csas@ubuntu:~/Desktop/Day3$ history 
    1  lsblk
    2  ls /
    3  su
    4  su root
    5  man sudo_root

Now that we know the basics of ownerships and permissions, let's see how we can modify permissions using the chmod command.

Syntax of chmod

4. To remove a directory/ Folder.

csas@ubuntu:~/Desktop/Day3$ mkdir text
csas@ubuntu:~/Desktop/Day3$ ls
file.txt  text
csas@ubuntu:~/Desktop/Day3$ rm -rf text/
csas@ubuntu:~/Desktop/Day3$ ls
file.txt

The rm command in Linux is used to remove files or directories from the file system.

e.g, rm [OPTIONS] FILE [FILE...]

Here, OPTIONS are the various options that you can use with the rm command, and FILE is the name of the file or directory that you want to remove.

Some common options that you can use with the rm command are:

  • -f: Force removal of files without prompting for confirmation.

  • -r: Recursively remove directories and their contents.

The rmdir command in Linux is used to remove empty directories from the file system.

rm -rfIt is used to forcefully and recursively remove files and directories from the file system, including non-empty directories.

5) To create a fruits.txt file and to view the content.

  • vim fruits.txt

  • cat fruits.txt

        csas@ubuntu:~/Desktop/Day3$ vim fruits.txt
        csas@ubuntu:~/Desktop/Day3$ cat fruits.txt 
        Apple
        Banana
        Mango
        Grapes
    

    6) Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

vim devops.txt

7) To Show only top three fruits from the file.

csas@ubuntu:~/Desktop/Day3$ head -3 devops.txt 
 Apple
 Mango
 Banana

8) To Show only bottom three fruits from the file.

csas@ubuntu:~/Desktop/Day3$ tail -3 devops.txt 
 Kiwi
 Orange
 Guava.

9) To create another file Colors.txt and to view the content.

csas@ubuntu:~/Desktop/Day3$ echo "Yelow , Red , Blue" > Colors.txt
csas@ubuntu:~/Desktop/Day3$ cat Colors.txt 
Yelow , Red , Blue

10) Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.

csas@ubuntu:~/Desktop/Day3$ vim Colors.txt

11) To find the difference between fruits.txt and Colors.txt file.

csas@ubuntu:~/Desktop/Day3$ diff Colors.txt  fruits.txt 
1,8c1,4
<  Red,
<  Pink 
<  White
<  Black
<  Blue
<  Orange
<  Purple
<  Grey.
---
> Apple
> Banana
> Mango
> Grapes