Basic Commands: Difference between revisions

Line 98: Line 98:
</syntaxhighlight>
</syntaxhighlight>


==rm Command==
==''rm'' Command==
Use the <code>rm</code> (stands for remove) command to remove the given file, multiple files, or a group/type of files. By default, the <code>rm</code> command does not require user confirmation to delete a file, but you can enable user confirmation using the <code>-i</code> option.
Use the <code>rm</code> (stands for remove) command to remove the given file, multiple files, or a group/type of files. By default, the <code>rm</code> command does not require user confirmation to delete a file, but you can enable user confirmation using the <code>-i</code> option.


Line 108: Line 108:
*<code>-d</code> deletes the specified empty directory.
*<code>-d</code> deletes the specified empty directory.


Removes the file named documents.txt from the current directory:
Removes the file named documents.txt from the current directory:<syntaxhighlight lang="shell">
rm documents.txt
rm documents.txt
</syntaxhighlight>To remove directories and their contents recursively, type:<syntaxhighlight lang="shell">
rm -r Documents
</syntaxhighlight>To remove the directory without being prompted, type:<syntaxhighlight lang="shell">
rm -rf dir1
</syntaxhighlight>The <code>rm-rf</code> command should be used with caution.


To remove directories and their contents recursively, type:
==''man'' & ''help'' Command==
rm -r Documents
 
To remove the directory without being prompted, type:
rm -rf dir1
 
The <code>rm-rf</code> command should be used with caution.
 
==Man & help Command==
Use the <code>man</code> command to display the user manual of the [[Linux]] command. Almost all the Linux commands have man pages, which is a kind of documentation that is displayed on the terminal. The Linux commands manual page explains what a particular command does, syntax, and accepted arguments.
Use the <code>man</code> command to display the user manual of the [[Linux]] command. Almost all the Linux commands have man pages, which is a kind of documentation that is displayed on the terminal. The Linux commands manual page explains what a particular command does, syntax, and accepted arguments.


Type man followed by the command name to display the command user manual page.
Type man followed by the command name to display the command user manual page.
man mkdir
 
<syntaxhighlight lang="shell">
man mkdir
</syntaxhighlight>


Similarly, you can also use the –-help option to display the help pages of a particular command.
Similarly, you can also use the –-help option to display the help pages of a particular command.
mkdir --help


==chmod Command==
<syntaxhighlight lang="shell">
mkdir --help
</syntaxhighlight>
 
==''chmod'' Command==
The <code>chmod</code> command is used to change the mode (permission) of a file. The basic permissions a file can have are <code>r</code>(read), <code>w</code>(write), and <code>x</code>(execute).
The <code>chmod</code> command is used to change the mode (permission) of a file. The basic permissions a file can have are <code>r</code>(read), <code>w</code>(write), and <code>x</code>(execute).


Line 134: Line 137:


For example to give <code>file1.txt</code> owner and group read and write permissions and only read permission to all others, type:
For example to give <code>file1.txt</code> owner and group read and write permissions and only read permission to all others, type:
chmod 664 file1.txt
 
<syntaxhighlight lang="shell">
chmod 664 file1.txt
</syntaxhighlight>


You may use -R option to recursively set permissions for all files and directories in a given directory.
You may use -R option to recursively set permissions for all files and directories in a given directory.


To make the file executable, type:
To make the file executable, type:
chmod +x myscript.sh


==chown Command==
<syntaxhighlight lang="shell">
chmod +x myscript.sh
</syntaxhighlight>
 
==''chown'' Command==
The <code>chown</code> command is used to change file and directory ownership in Linux.
The <code>chown</code> command is used to change file and directory ownership in Linux.


To change both owner and group of file, type:
To change both owner and group of file, type:
chown ownername:groupname filename
 
<syntaxhighlight lang="shell">
chown ownername:groupname filename
</syntaxhighlight>


You may use <code>-R</code> option to recursively set ownership for all files and directories in a given directory.
You may use <code>-R</code> option to recursively set ownership for all files and directories in a given directory.


==pwd Command==
==''pwd'' Command==
<code>pwd</code> stands for present working directory. The pwd command prints the absolute path of the current directory on your Linux terminal. The present working directory is the directory in which you are currently working.
<code>pwd</code> stands for present working directory. The pwd command prints the absolute path of the current directory on your Linux terminal. The present working directory is the directory in which you are currently working.
pwd
 
<syntaxhighlight lang="shell">
pwd
</syntaxhighlight>


'''Output'''
'''Output'''
/home/linuxopsys
 
<syntaxhighlight lang="shell">
/home/linuxops
</syntaxhighlight>


In the example <code>/home/kagtain</code> is the current directory.
In the example <code>/home/kagtain</code> is the current directory.


==uname Command==
==''uname'' Command==
Use the <code>uname</code> (stands for UNIX name) command to display fundamental information about your [[Linux]] computer. By default, this command prints only the type of the operating system, such as Linux. You can use different options with this command to print other information about your computer such as OS, kernel version, machine name, network, and so on.
Use the <code>uname</code> (stands for UNIX name) command to display fundamental information about your [[Linux]] computer. By default, this command prints only the type of the operating system, such as Linux. You can use different options with this command to print other information about your computer such as OS, kernel version, machine name, network, and so on.


Line 167: Line 185:


The output of uname -a command on my machine:
The output of uname -a command on my machine:
Linux linux 5.13.0-37-generic #42~20.04.1-Ubuntu SMP Tue Mar 15 15:44:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
 
<syntaxhighlight lang="shell">
Linux linux 5.13.0-37-generic #42~20.04.1-Ubuntu SMP Tue Mar 15 15:44:28 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
</syntaxhighlight>


All system information including OS type, name, user name, kernel release, and hardware platform is displayed.
All system information including OS type, name, user name, kernel release, and hardware platform is displayed.


==who Command==
==''who'' Command==
Use the <code>who</code> command to list currently logged-in users on your Linux computer. This command also supports few options to display specific information, such as:
Use the <code>who</code> command to list currently logged-in users on your Linux computer. This command also supports few options to display specific information, such as:


Line 179: Line 200:


The output of who command looks like this:
The output of who command looks like this:
root    pts/0        2022-04-28 01:30 (192.168.1.10)
 
bob      pts/1        2022-04-28 02:40 (192.168.1.22)
<syntaxhighlight lang="shell">
kangtain pts/2        2022-04-28 02:05 (192.168.1.22)
root    pts/0        2022-04-28 01:30 (192.168.1.10)
bob      pts/1        2022-04-28 02:40 (192.168.1.22)
kangtain pts/2        2022-04-28 02:05 (192.168.1.22)
</syntaxhighlight>


It shows 3 users with their logged in date and time.
It shows 3 users with their logged in date and time.


==uptime Command==
==''uptime'' Command==
Use the uptime command to display the current system time, the duration for which the system has been up, the number of logged-in users, and average load time for 1, 5, and 15-minute intervals. This Linux command retrieves information from the <code>/proc/uptime</code> file.
Use the uptime command to display the current system time, the duration for which the system has been up, the number of logged-in users, and average load time for 1, 5, and 15-minute intervals. This Linux command retrieves information from the <code>/proc/uptime</code> file.


The output of uptime command from my system:
The output of uptime command from my system:
17:11:28 up 6 days,  3:23,  1 user,  load average: 0.00, 0.01, 0.00


==hostname Command==
<syntaxhighlight lang="shell">
17:11:28 up 6 days,  3:23,  1 user,  load average: 0.00, 0.01, 0.00
</syntaxhighlight>
 
==''hostname'' Command==
Use <code>hostname</code> command to display the Domain Name System (DNS) name and set the hostname or Network Information System (NIS) of your system.
Use <code>hostname</code> command to display the Domain Name System (DNS) name and set the hostname or Network Information System (NIS) of your system.


Display host name and domain name, type:
Display host name and domain name, type:
hostname
 
kangtain
<syntaxhighlight lang="shell">
hostname
kangtain
</syntaxhighlight>


Some distributions allow you to set hostname for your machine, type:
Some distributions allow you to set hostname for your machine, type:
sudo hostname NEW_HOSTNAME
 
<syntaxhighlight lang="shell">
sudo hostname NEW_HOSTNAME
</syntaxhighlight>


You can replace NEW_HOSTNAME with your actual machine name.
You can replace NEW_HOSTNAME with your actual machine name.


==top, htop Command==
==''top'', ''htop'' Command==
Use the <code>top</code> or <code>htop</code> commands to monitor the server’s processes or vital system resources in real-time. Both these commands are used for the same purpose, but their output is different. The default output displays information about top CPU-consuming processes along with RAM usage.
Use the <code>top</code> or <code>htop</code> commands to monitor the server’s processes or vital system resources in real-time. Both these commands are used for the same purpose, but their output is different. The default output displays information about top CPU-consuming processes along with RAM usage.
top
 
<syntaxhighlight lang="shell">
top
</syntaxhighlight>


The command also displays system up time, load average, number of logged in users, memory, total tasks, and running tasks.
The command also displays system up time, load average, number of logged in users, memory, total tasks, and running tasks.
Line 211: Line 247:
<code>Htop</code> is an improved version of the <code>top</code> command. It display colorful text and allows for output scrolling. Htop doesnt comes preinstalled on most Linux distros.
<code>Htop</code> is an improved version of the <code>top</code> command. It display colorful text and allows for output scrolling. Htop doesnt comes preinstalled on most Linux distros.


==touch Command==
==''touch'' Command==
Use the <code>touch</code> command to create an empty file on your Linux computer, or to modify the timestamp of a file. The primary function of this command is to update file timestamps, but it is most commonly used to create a file. The touch command can update access and modification timestamps of the specified file, and create the file if it does not already exist.
Use the <code>touch</code> command to create an empty file on your Linux computer, or to modify the timestamp of a file. The primary function of this command is to update file timestamps, but it is most commonly used to create a file. The touch command can update access and modification timestamps of the specified file, and create the file if it does not already exist.