Getting Started with Linux Commands
Published: 2022-11-01 19:54:05

Let's get started with basic Linux Commands we need in our day to day activities!!

## __System Commands:__


- ## who

        - gives login name of the user
        - Date and time of login
        - Remote host name of the server



- ## whoami

        - Displays the systems username

- ## id

        - It displays the user idnetification (real and effective group ids)

        - It is used to print the genuine and effective user ID and group ID.

        - A user ID (UID) is a particular user identity, whereas group IDs (GIDs) can contain more than one  user's identity. This command is a useful tool for user management.

- ## groups [username]

        - This command is used to display the groups for which the user is belonging to

        - If no username is provided, groups og current user is provided

- ## users 

        - Displays the username of all the users currently logged in the system

- ## last log 

        - lastlog formats and prints the contents of the last login log

        - The lastlog file is a database which contains info on the last login of each user

## __ROOT USER__

- We can say that __Root__ user is like a __SuperAdmin__.

-> 5 ways to become a root user :

1. sudo -i
2. sudo -s
3. sudo su -s
4. su -root
5. su -


- ## clear

        - As the name suggests, it clears the terminal screen


## __File and Directory Commmands:__

- ## pwd 

        - Displays current working directory


- ## cd [directory name]

        - Move to the particular mentioned directory

- ## cd ..

        - To move one directory back

- ## cd

        - To go to home directory

- ## ls

        - Lists down all the the contents inside a directory

- ## mkdir [directory name]

        - This command allows us to create a directory

- ## rmdir [directory name]

        - Allows to remove empty directories from file system

        > Syntax : rmdir [-p] [-v | –verbose] [–ignore-fail-on-non-empty] directories …

- ## touch [filename]

        - This is used to create a file of any type

        > Syntax : touch hello.txt

- ## cat > [file_name] or cat >> [file_name]

        - The above command will create a text file and will enter in the editor mode
        
        - Now, we can  enter the text and press command + D key to save and exit the file 


___NOTE___ : Difference between > and >> is, > overwrites the text that is already present in the file. But where as >> appends the text in the file.


- ## cat [file_name]

        - To read contents of the mentioned file

- ## echo "file content" > [file_name]

        - Above command will create a new file with given name and add the content mentioned in the terminal

- ## printf "file content" > [file_name]

        - We can also create a file using printf command. 
        
        - For this we need to specify the file content on the command line.

- ## cp [file1] [file2]

        - Copy contents of file1 to file2

        - Here, file1 is source file and file2 is destination file

- ##  cp [src_file1]   [src_file2]   [src_file3]   [Destination_Directory]

        - This will copy all mentioned source files into the Destination_Directory. If these files already exist, it simply replaces them.

- ## mv [file_name]  [destination_directory]

        - It moves the file to the mentioned destination directory

        - If in case the destination_directory already contains a file with the same name, then it is overwritten


- ## rm [file_name]

        - This command removes the file present in the directory

- ## rm - [directory/*]

        - To deleet all the files present in the directory

- ## ls -l 

        - The ls command lists the files inside a folder

- ## file [file_name]

        - This command is used to determine the file type

- ## diff [file1] [file2]

        - This diff command is used to check the differences between two files

- ## find [where to search] -name [what to search]

        - This command helps to search the necessary data in the specified location

## __system $ Hardware Information :__

- ## uname

        - We can use this to find out the system name


- ## uname -n 

        - To view network hostname


- ## uname -v

        - To get information about kernel version

- ## uname -r

        - To get information about kernel release

- ## uname -m

        - To get machine hardware name

> To get more insignts on System & Hardware commands, follow this link -> [Link](https://www.tecmint.com/commands-to-collect-system-and-hardware-information-in-linux/)



## __Package Installations Command :__

- ## sudo apt install [package/aplication name]

        - Installing a package or an application in UBUNTU

- ## sudo apt remove [package/aplication name]

        - Removing a package in UBUNTU

- ## sudo apt update

        - o update the local package index with the latest changes made in the repositories

- ## sudo apt upgrade

        - To upgrade the system, first, update the package index as outlined in above command then use this command


## __Disk Uage Command:__

- ## du 

        -  Display the amount of disk space used by the specified files and for each subdirectory.

        - If we add du -h , it gives output in human readable format

- ## df

        - Shows the amount of disk space used and available on Linux file systems.

        - If we add du -h , it gives output in human readable format

        - --help , to find to get all the options we can use with df

> To get more indepth understanding on __Disk Uage Commands__, follow this link -> [Link](http://blog.imm.cnr.it/content/linux-check-disk-space-command-view-system-disk-usage-df-and-du)


## __Administration and Management:__

- ## adduser [user_name]

        - We can add a user by using this command

        - Only a root user can create a user

        - We have to assign password to the user while creating the account

- ## userdel -r [user_name]

        - Deletes the user

- ## deluser [USER] [GROUPNAME]

        - Delete a user from a userGroup

- ## passwd -l [username]

        - To Disable a user

- ## usermod -a -G [GROUPNAME] [USERNAME]

        - To add a user to a group name

- ## finger

        - Gives information on all logged in user

- ## finger [user_name]

        - Displays information about a particular user


## __Softlink and Hardlink:__

> A softlink is an actual link to the original file, where as hardlink is a mirror copy of a original file

- ## ln -s [source.file] [softlink.file]

        - Soft link is just a link that points to the original file. The softlink is like a shortcut to a file.

        - If we delete source file, and try to access softlink file. We'll get an erorr of 'no such directory/file'

- ## ln [source.file] [hardlink.file]

        - Even if delete the source file, we can still view contents of hardlink file

        - If you create a hard link to a file and change the content of either of the files, the change will be seen on both. This is the difference between copying a file and creating a haedlink.

For more info on Hardlink and Softlink -> [Link](https://ostechnix.com/explaining-soft-link-and-hard-link-in-linux-with-examples/)

## __Networking Commands :__

- ## ifconfig

        - Displays the route and networking interface

        - It displays information like : IP address, MAC address

- ## tracepath [destination]

        - This command is used to find delays in network. This doesn't require root privileges


        - It traces the route to the specified destination and identifies each hop in it. If your network is weak, it recognizes the point where the network is weak.

- ## traceroute [destination]

        - It is used to troubleshoot the network. It detects the delay and determines the pathway to your target. It basically helps in the following ways:

                It provides the names and identifies every device on the path. 
                It follows the route to the destination
                It determines where the network latency comes from and reports it.

- ## ping [destination]

        - Linux ping is one of the most used network troubleshooting commands. It basically checks for the network connectivity between two nodes.

        - ping stands for Packet INternet Groper.

        - The ping command sends the ICMP echo request to check the network connectivity.

        - It keeps executing until it is interrupted.

        - Use Ctrl+C Key to interrupt the execution.

- ## dig [destination]

        - Linux dig command stands for Domain Information Groper. This command is used in DNS lookup to query the DNS name server. It is also used to troubleshoot DNS related issues.

        - It is mainly used to verify DNS mappings, MX Records, host addresses, and all other DNS records for a better understanding of the DNS topography.

- ## route

        - Linux route command displays and manipulates the routing table existing for your system.

        - A router is basically used to find the best way to send the packets across to a destination.

- ## host [hostname]

        - Linux host command displays the domain name for a given IP address and IP address for a given hostname. It is also used to fetch DNS lookup for DNS related query.

- ## curl -o [filepath]

        - curl is used to download a file from internet using CLI

- ## wget [link]

        - This performs same as curl

- ## whois [website]

        - Linux whois command is used to fetch all the information related to a website. You can get all the information about a website including the registration and the owner information.


## __Process Management__ :

- ## top 

        - To track the running processes on our machine we can use the top command

- ## kill [pid]

        - To stop a process in Linux, use the 'kill’ command. kill command sends a signal to the process

        - There are different types of signals that you can send. However, the most common one is ‘kill -9’ which is ‘SIGKILL’

        - To list all the signals, we can use kill -L commans

        - The default signal is 15, which is SIGTERM. Which means if you just use the kill command without any number, it sends the SIGTERM signal


## __chmod__:

- In Linux, who can do what to a file or directory is controlled through sets of permissions. There are three sets of permissions. One set for the owner of the file, another set for the members of the file’s group, and a final set for everyone else.

- There are three characters in each set of permissions. The characters are indicators for the presence or absence of one of the permissions. They are either a dash (-) or a letter. If the character is a dash, it means that permission is not granted. If the character is an r, w, or an x, that permission has been granted.

        The letters represent:

        r: Read permissions. The file can be opened, and its content viewed.
        w: Write permissions. The file can be edited, modified, and deleted.
        x: Execute permissions. If the file is a script or a program, it can be run (executed).


- We can change the permission by using numbers. How ? lets see
        read(r) = 4
        write(4) = 2
        execute(x) = 1

        - Total of these 3 permissions is 7.
        - If I want group to have only read and write permission. I would give 6(4+2) for group. Similarly we can give numbers for users and everyone
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2m86dbai6jam6twodne5.png)

permission. I would give 6(4+2) for group. Similarly, we can give numbers for users and everyone


> Thanks for reading

> Hope you find this useful. Let me know your thoughts or any feedback to improve my writing