Day 3 Task: Basic Linux Commands

Day 3 Task: Basic Linux Commands

Β·

9 min read

🐧 LINUX COMMAND πŸ’»

  • πŸ“‚ Navigate through Directories:

    • cd: Change directory, to move to a different folder.

    • ls: List files and directories in the current folder.

  • πŸ“„ Work with Files:

    • touch: Create an empty file.

    • cp: Copy files or directories.

    • mv: Move or rename files or directories.

    • rm: Remove files or directories.

  • πŸ—ƒοΈ Manage Directories:

    • mkdir: Create a new directory.

    • rmdir: Remove an empty directory.

  • πŸ” Search for Information:

    • grep: Search for a specific pattern in files.

    • find: Search for files and directories based on various criteria.

  • πŸ“Š Check System Information:

    • uname: Display system information.

    • df: Show disk space usage.

    • free: Display memory usage.

  • βš™οΈ Manage Processes:

    • ps: List running processes.

    • kill: Terminate a running process.

  • πŸ“ Compress and Archive Files:

    • tar: Create or extract tar archives.

    • gzip: Compress files using gzip compression.

  • πŸ”„ Control System Services:

    • systemctl: Control system services, like starting or stopping them.
  • πŸš€ Network-related Commands:

    • ping: Check network connectivity to a specific host.

    • ifconfig or ip: Display network interface information.

  • 🚧 Administrative Tasks:

    • sudo: Execute commands with administrative privileges.

πŸ“„To view what's written in a file πŸ‘€

  • You can read the text or data stored inside a file by viewing its contents. This can be helpful for reading documents, notes, configuration files, or any other text-based data.

πŸ” Follow these steps to view what's written in a file:

  1. πŸ–₯️ Open a Terminal:

    • On Ubuntu, Fedora, or other Linux distributions, you can open the Terminal from the Applications or System Tools menu.
  2. πŸ“‹ Use the "cat" Command:

    • In the Terminal, type the command cat followed by a space.
  3. πŸ“‚ Specify the File Name:

    • After the cat command, type the name of the file you want to view (e.g., myfile.txt).
  4. ⌨️ Press Enter:

    • Press the Enter key to execute the cat command and view the contents of the file.

πŸ‘€πŸ’» You will see the complete text or data written in the file displayed directly in the terminal window.

πŸ”’To change the access permissions of files πŸ’»

  • Each file in Linux has access permissions that specify who is allowed to read(πŸ‘οΈ), write(πŸ“), and execute(▢️) the file. For sensitive data to remain secure and under control, certain permissions are crucial.

  1. βš™οΈ Understanding Access Permissions:
  • There are three types of permissions for each file: read (πŸ‘οΈ)7, write (πŸ“)7, and execute (▢️)7.

  • Permissions are set for three categories of users: the file owner (πŸ‘‘), the group the file belongs to (πŸ‘₯), and others (🌐).

  1. πŸ”’ Representation of Permissions:
  • Read (πŸ‘οΈ) is represented by r = 7.

  • Write (πŸ“) is represented by w = 7.

  • Execute (▢️) is represented by x = 7.

  • A dash - indicates that a specific permission is not granted.

πŸ”§ Changing Access Permissions Using chmod:

  • chmod 777 command is used to modify access permissions.

  • After granting permissions using chmod, you can use this command to view your file as well.

./<your filename.txt>
  • To check if the permission was granted, you can use ls and notice that your file will appear in green color, indicating that it has been permissioned.

πŸ”‘ Examples of Changing Access Permissions:

Note:-This will learn detail on day 6 of the Task click on this to see

πŸ”To check which commands you have run till nowπŸ“œ

  • With Linux, it's simple to see a history of the commands you've run in the terminal. This history feature makes it simple to remember and reuse prior commands for routine operations.

  1. πŸ”§ Using History Command:
  • The history command displays a list of recently executed commands along with their line numbers.

πŸ’‘ Examples of Checking Command History:

Remember, the command history feature is a useful tool for recalling and reusing commands, saving time and effort in the Linux terminal. It allows you to be more productive and efficient in your command-line tasks! πŸ”πŸš€πŸ”§

πŸ—‘οΈTo remove a directory/ FolderπŸ“‚

  • The rmdir or rm command in Linux can be used to eliminate directories or folders from your file system. When executing these commands, though, it's important to exercise caution because erased data cannot be easily retrieved.

  1. πŸ”§ Using the rmdir Command:
  • The rmdir command is used to remove an empty directory.

πŸ’‘ Example of Removing an Empty Directory: To remove an empty directory named new-folder

  1. πŸ”§ Using the rm Command:
  • The rm command is used to remove directories and their contents recursively.

πŸ’‘ Example of Removing a Directory and Its Contents: To remove a directory named my-demo.txt and all its contents:

πŸ“To create a fruits.txt file and to view the content🍎

  • Different text editors are available in Linux for creating new files and viewing their contents. Here, we'll create the file using the touch command and then open it with the cat command to see what's inside.

  1. πŸ”§ Using the touch Command to Create fruits.txt File:
  • The touch command creates an empty file if it does not exist. If the file already exists, it updates the file's modification timestamp.

πŸ’‘ Example of Creating the fruits.txt File: To create the fruits.txt file, simply run the following command:

touch fruit.txt
  1. πŸ”§ Using the cat Command to View File Content:
  • The cat command is used to concatenate and display the content of a file.

πŸ’‘ Example of Viewing the fruits.txt File's Content: To view the content of the fruits.txt file, use the cat command as follows:

cat fruit.txt

πŸ“Add content in fruit.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava βž•

  • To add content to the fruits.txt file, you can use a text editor like nano or vim. For a non-IT audience, vim is more user-friendly.
  1. πŸ”§ Using the vim Text Editor and press i to Add Content:
  • Open the fruits.txt file in the vim text editor:
vim fruit.txt
  1. Add the desired content (e.g., list of fruits) to the file.
Apple
Mango
Banana
Cherry
Kiwi
Orange
Guava
  1. Save what you wrote using Esc:wq and then press Enter.

Viewing the fruits.txt File's Updated Content Use the cat command to view the changed content after adding content to the fruits.txt file.

I'm done now! The fruits.txt file was successfully created, its contents were added using vim, and its contents were seen using the cat program. You can now maintain a list of your preferred fruits or any other text data in the file. πŸ“πŸŽπŸš€

🍎🍌 To Show only the top three fruits from the file 🍊

  • With the head command in Linux, you can only see the top (first) few lines of a file. We'll use the head command, in this case, to just display the first three fruits from the fruits.txt file.
  1. πŸ”§ Using the head Command:
  • The head command is used to display the beginning (top) lines of a file.

πŸ’‘ Example of Showing the Top Three Fruits from fruits.txt Suppose the fruits.txt file contains the following content:

  • To display only the top three fruits from the file, use the head command with the -n option, specifying the number of lines you want to display (in this case, three):
head -3 fruit.txt

πŸ” Explanation: The head -3 command displays the first three lines from the fruits.txt file, which are the names of the top three fruits.

Now you can easily view the top three fruits from the file without having to go through the entire content. πŸŽπŸŠπŸŒπŸš€

πŸ‡To Show only the bottom three fruits from the file🍍

  • The tail command in Linux allows you to just see the final few lines of a file. We'll use the tail command in this case to only display the bottom three fruits from the fruits.txt file.
  1. πŸ”§ Using the tail Command:
  • The tail command is used to display the ending (bottom) lines of a file.

πŸ’‘ Example of Showing the Bottom Three Fruits from fruits.txt Suppose the fruits.txt file contains the following content

  • To display only the bottom three fruits from the file, use the tail command with the option, specifying the number of lines you want to display (in this case, three):

πŸ” Explanation: The tail -3 command displays the last three lines from the fruits.txt file, which are the names of the bottom three fruits.

Now you can quickly view the bottom three fruits from the file without having to go through the entire content. πŸ‡πŸπŸ‘πŸš€

πŸ“To create another file Colors.txt and to view the content🌈

  • Different text editors are available in Linux for creating new files and viewing their contents. Here, we'll create the file using the touch command and then open it with the cat command to see what's inside.

  • To create a file named Colors.txt and view its content in Linux, you can use commands like touch to create the file and a text editor like vim to add content.

Here's an example:

touch Colors.txt 
vim Colors.txt
  • After running these commands, you'll be in the vim text editor where you can type or paste the desired content into the file. To save and exit, press Esc, then confirm with :wq to save changes and press Enter.

  • If you want to view the content of the file directly in the terminal, you can use the cat command:

cat Colors.txt

πŸ“ Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey 🌈

  • To add content to the colour.txt file, you can use a text editor like nano or vim. For a non-IT audience, vim is more user-friendly.
  1. πŸ”§ Using the vim Text Editor and press i to Add Content:
  • Open the colour.txt file in the vim text editor:
vim colour.txt
  1. Add the desired content (e.g., list of fruits) to the file.
Red
Pink
White
Black
Blue
Orange
Purple
Grey
  1. Save what you wrote using Esc:wq and then press Enter.

Viewing the colour.txt File's Updated Content Use the cat command to view the changed content after adding content to the colour.txt file.

I'm done now! The colour.txt file was successfully created, its contents were added using vim, and its contents were seen using the cat program. You can now maintain a list of your preferred fruits or any other text data in the file. πŸ“πŸŒˆπŸš€

🌈To find the difference between fruits.txt and Colors.txt file 🍎

  • To compare the contents of two text files and identify discrepancies, use the diff command in Linux. Here, we'll compare the contents of the Colors.txt and Fruits.txt files to see if there are any differences.
  1. πŸ”§ Using the diff Command:
  • The diff command is used to compare text files line by line.

πŸ’‘ Example of Finding the Difference Between fruits.txt and Colors.txt Suppose the fruits.txt & colour.txt file contains the following content:

To find the difference between the two files, use the diff command as follows:


Happy Learning

Thanks For Reading! :)

-SriParthuπŸ’πŸ’₯

Β