html code

Initiating Your Linux Journey: An Introductory Guide for Beginners (Part 2)

Essential Commands to Know:

1.Command Manual

The initial command I’d like to introduce is one that serves as a valuable resource for comprehending various other commands.

The ‘man’ command offers an in-depth overview, including the synopsis, author’s name, available options, a brief description, and other informative details pertaining to the specified command.

$ man pwd

2.Command to Determine User Identity”

The ‘whoami’ command permits users to observe the currently authenticated user.

ayush@IN2$ whoami
ayush

3.Command for Displaying User and Group Information

The ‘id’ command displays the real user ID (UID) and group ID (GID), along with various other account-related details.

ayush@IN2$ id
uid=1000(ayush) gid=1000(ayush) groups=1000(ayush),4(adm),20(dialout),24(cdrom),25(floppy),27(sudo),29(audio),30(dip),44(video),46(plugdev),116(netdev)

The ‘-u’ option is employed to locate a user’s particular User ID (UID).

ayush@IN2$ id -u ayush
1000

4.Command to Display the Current Working Directory

The ‘pwd’ command, short for ‘Print Working Directory,’ assists in locating the path of the current working directory.

ayush@IN2$ pwd
/home/ayush

5.Command for Listing Directory Contents

The ‘ls’ command is utilized to display the files and directories contained within the current directory.

ayush@IN2$ ls
linux_demo

If a folder name or path is added, it will display the contents of that specific folder.

$ ls /bin

When a folder name or path is provided, it will output the contents of that particular folder.

6.The ‘ls -l’ command

The ‘-l’ option, short for ‘long format,’ furnishes comprehensive information about every file and directory within the specified directory.

$ ls -l
total 0
drwxr-x--- 1 ayush ayush 4096 Jun  3 17:21 ayush
drwxr-xr-x 1 root  root  4096 Nov 25  2022 linuxbrew
. . .

7.The ‘ls -a’ command

The ‘ls -a’ command is akin to ‘ls -l,’ but it additionally reveals hidden files.

$ ls -a
.  ..  ayush  linuxbrew

8.The ‘ls -ltr’ command

The ‘-ltr’ option is a combination of three distinct options: ‘-l’ for ‘long format,’ ‘-t’ for ‘sorting by time,’ and ‘-r’ for ‘reversing the sorting order.’ When executing the ‘ls -ltr’ command, it presents files and directories in long format, arranged by modification time in descending order, with the newest files appearing at the end of the list. This is valuable for easily identifying the most recently modified files.

$ ls -ltr
-rw-r--r-- 1 user group 2095 May 01 10:30 file1.txt
-rw-r--r-- 1 user group 4050 May 15 12:45 file2.txt
drwxr-xr-x 2 user group 8066 May 20 17:10 directory
. . .

9.Command to Create a Directory

By utilizing the ‘mkdir’ command, we can generate directories.

$ mkdir linux_demo
$ ls
linux_demo

The folder name is ‘linux_demo.’

With the ‘mkdir’ command, we can generate multiple folders.

$ mkdir bike car
$ ls
bike  car

It’s also possible to create multiple nested folders by including the ‘-p’ flag.

$ mkdir -p animals/dog
$ ls
animals
$ cd animals/
$ ls
dog

10.Command for Removing Directories

The ‘rmdir’ command erases a folder. The folder being deleted must be empty.

$ rmdir dog

It’s also possible to remove multiple folders simultaneously.

$ rmdir bike car

11.Command to Clear the Terminal Screen

This command is employed to remove all the previously executed commands from the current terminal’s display.

$ clear

12.Command for Displaying Command History

This command shows the commands that were run in the past.

$ history

13.Command to View Command History

The ‘cd’ command, which stands for ‘change directory,’ is used to switch to a different directory.

We will navigate to the ‘bin’ directory.

$ cd /bin

14.The ‘.’ directory and the ‘..’ directory

The symbol ‘.’ (dot) symbolizes the current directory.

The symbol ‘..’ (dot dot) signifies the parent directory of the current directory.

Using ‘cd .’ will keep the user in the current directory.

Using ‘cd ..’ will shift the user up one directory level.

$ cd ..
$ cd .

15.Command for Displaying Directory Tree Structure

“The ‘tree’ command displays the structure of directories.”

$ tree
 .
├── ayush
└── linuxbrew

16.Switching to the Root User

The root user is the superuser with the authority to make changes across various aspects of a Linux system.

The ‘su -‘ command is employed to assume the role of the root user.

$ su -

The symbol ‘/’ denotes the root directory in your file system.

$ ls /
bin  boot  ...

The ‘exit’ keyword is employed to leave the root user session.

17.Elevated Privilege Command

The ‘sudo’ command is utilized to execute a command with administrative privileges, also referred to as root access.

$ sudo apt update

18.Command to Create Empty Files

Using the ‘touch’ command, we can generate an empty text file.

$ touch orange
$ ls
orange

‘Orange’ is the file name.

The ‘touch orange.txt{1..5}’ command generates new files named ‘orange.txt1,’ ‘orange.txt2,’ ‘orange.txt3,’ ‘orange.txt4,’ and ‘orange.txt5’.

$ touch orange.txt{1..5}
$ ls
orange.txt1, orange.txt2, orange.txt3, orange.txt4, orange.txt5

19.Text Editor – Vim

Vim is a highly regarded text editor, often referred to as the ‘Vi Improved’ editor. You can use the ‘vim’ command to open an existing file for editing. If the file doesn’t exist, it will create a new, empty file.

In this case, ‘flower’ is the name of the file.

$ vim flower

To exit Vim, press the ‘Esc’ key followed by ‘:q’.

To save your changes and quit Vim, press ‘:wq’.

If you want to exit Vim without saving changes, press ‘:q!

20.Command for Displaying File Content

The ‘cat’ command holds significant importance in our daily activities as it outputs the content of a file to the standard output.

$ cat flower
Hello

(The word ‘Hello’ is written in the example file)

21.Text Editor – Nano

Nano is another text editor.

You can run it with the command ‘nano <filename>’

22.Command for Displaying Text

The ‘echo’ command is employed to output the text provided as an argument.

$ echo "Hi"
Hi

We can add the output to a file by utilizing the ‘>>’ operator.

echo <text> >> <filename>

23.Utilizing ‘>’ and ‘>>’ to redirect output to a specific file

By employing the ‘>’ operator, we can redirect the output of a command to a file. If the file already contains content, it will be replaced with the output of the command, effectively overwriting the old content.

$ ls > a.txt
$ ls
a.txt  app  cat  dog
$ cat a.txt
a.txt
app
cat
dog

The ‘>>’ operator can be utilized to append the output of a command to a file, preserving the existing content in the file.

24.Command for Word Count (wc)

The ‘wc’ command, an abbreviation for ‘word count,’ assists in tallying the lines, words, and bytes within a file.

The ‘-l’ flag specifically provides the count of lines in a file.

$ wc -l <filename.txt>

The ‘-w’ flag tallies the total number of words in the file.

$ wc -w <filename.txt>

25.Redirecting the Output of a Command

By employing the pipe (|) operator, we can redirect the output of one command as input to another command.

$ ls
bin  boot  ...
$ ls | wc -l
23

26.Command for Removing Files and Directories

The ‘rm’ command is a highly versatile tool for deleting files and directories. The ‘-r’ option is employed for recursive removal, while the ‘-f’ option is used for forceful deletion.

$ rm -rf <folder_name>

The ‘rm <file_name>’ command deletes all files with names that start with ‘<file_name>’

27.Command for Copying Files and Directories

Using the ‘cp’ command in the Linux shell, we duplicate a file. In the following example, we copy the contents of ‘a.txt’ into ‘b.txt’.

$ cp a.txt b.txt

“In the example below, we duplicate the ‘images’ directory, along with its entire contents, from the ‘Pictures’ directory within the home directory to the ‘/tmp/’ directory.”

$ cp -r ~/Pictures/images /tmp/

In a different instance, we replicate the file ‘image.png’ from the ‘Pictures’ directory within my home directory to the present directory.

$ cp ~/Pictures/image.png .

28.Command for Moving or Renaming Files and Directories

The ‘mv’ command is employed to rename or relocate a directory or file.


The file ‘good.txt’ has been renamed to ‘bad.txt’

29.Command for Displaying Calendar

The ‘cal’ command is utilized to show a calendar within the shell.

$ cal

30.Command for Displaying Date and Time

The ‘date’ command shows the current date.

$ date

31.Command for Changing File Permissions (chmod)

Short for ‘change mode,’ the ‘chmod’ command is employed to modify the access permissions of files and directories in Linux systems.

There are two primary methods for using ‘chmod’:

  1. Symbolic arguments.
  2. Numeric arguments.

In symbolic notation, the letters represent:

  • ‘a’ for all
  • ‘u’ for user
  • ‘g’ for group
  • ‘o’ for other

Then, use the ‘+’ symbol to add a permission or ‘-‘ symbol to remove a permission. After that, you can specify one or more permission symbols, such as ‘r’ (read), ‘w’ (write), and ‘x’ (execute).

Here are some examples:

  • chmod a+r filename: This allows everyone to read the file.
  • chmod u+rw filename: This grants the user read and write permissions.

Numeric values can also be used to assign permissions. To set permissions for all three groups (user, group, and others) together, use numeric values in sets of three. The numeric values are as follows:

  • 0: No permissions
  • 1: Execute
  • 2: Write
  • 3: Write and execute
  • 4: Read
  • 5: Read and execute
  • 6: Read and write
  • 7: Read, write, and execute

Here are some examples:

  • chmod 644 filename: This grants read and write permissions to the user and read-only access to the group and others.
  • chmod 755 filename: This provides read, write, and execute permissions to the user and read and execute permissions to the group and others.”
chmod 777 filename
chmod 000 filename
chmod 655 filename

32.Command for Changing Ownership (chown)

Both the owner and the root user have the capability to modify the ownership of a file or directory using the ‘chown’ command.

sudo chown <owner> <file>

33.Command for Changing Group Ownership (chgrp)

The ‘chgrp’ command allows for the alteration of group ownership of files and directories.

chgrp <group> <filename>

Wishing you a productive learning experience!