Basic Commands: Difference between revisions
No edit summary |
|||
| Line 102: | Line 102: | ||
Use the mv (stands for move) command to move files or directories from the source to the destination directory. It can be used to rename a file/directory. | Use the mv (stands for move) command to move files or directories from the source to the destination directory. It can be used to rename a file/directory. | ||
The following mv command moves the users.txt file to the Documents/records directory. | The following mv command moves the <code>users.txt</code> file to the <code>Documents/records</code> directory. | ||
mv users.txt Documents/records | |||
To rename the employees.txt file to <code>users.txt</code>: | |||
To rename the employees.txt file to users.txt: | mv employees.txt users.txt | ||
==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 rm (stands for remove) command to remove the given file, multiple files, or a group/type of files. By default, the rm command does not require user confirmation to delete a file, but you can enable user confirmation using the -i option. | |||
Options can be used with the rm command for the following needs: | Options can be used with the rm command for the following needs: | ||
-i deletes files in interactive mode. | *<code>-i</code> deletes files in interactive mode. | ||
-f forcefully removes the write-protected file. | *<code>-f</code> forcefully removes the write-protected file. | ||
-r recursively deletes files, directories, and subdirectories in the specified parent directory. | *<code>-r</code> recursively deletes files, directories, and subdirectories in the specified parent directory. | ||
-d 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: | ||
rm documents.txt | |||
To remove directories and their contents recursively, type: | To remove directories and their contents recursively, type: | ||
rm -r Documents | |||
To remove the directory without being prompted, type: | To remove the directory without being prompted, type: | ||
rm -rf dir1 | |||
The <code>rm-rf</code> command should be used with caution. | |||
The rm-rf command should be used with caution. | |||
==Man & help Command== | |||
Use the man 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 | |||
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== | |||
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 chmod command is used to change the mode (permission) of a file. The basic permissions a file can have are r(read), w(write), and x(execute). | |||
Using numeric mode you can set read (value is 4), write (value is 2 and execute (value is 1) permissions for owner, group and all others. | Using numeric mode you can set '''read (value is 4)''', '''write (value is 2 and execute (value is 1)''' permissions for owner, group and all others. | ||
For example to give file1.txt 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 | |||
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== | |||
The <code>chown</code> command is used to change file and directory ownership in Linux. | |||
The chown 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 | |||
You may use <code>-R</code> option to recursively set ownership for all files and directories in a given directory. | |||
You may use -R option to recursively set ownership for all files and directories in a given directory. | |||
==pwd Command== | |||
pwd 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 | |||
'''Output''' | |||
Output | /home/linuxopsys | ||
/home/linuxopsys | |||
In the example <code>/home/kagtain</code> is the current directory. | |||
Use the uname (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. | |||
==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. | |||
*<code>-a</code> display all information about your computer. | |||
*<code>-s</code> display Linux kernel name. | |||
*<code>-m</code> displays machine information. | |||
*<code>-i</code> display hardware platform information. | |||
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 | |||
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== | |||
Use the who 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: | ||
*<code>-a</code> displays all available information about logged-in users. | |||
*<code>-b</code> displays boot time. | |||
*<code>-H</code> displays header names. | |||
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) | |||
kangtain pts/2 2022-04-28 02:05 (192.168.1.22) | |||
It shows 3 users with their logged in date and time. | It shows 3 users with their logged in date and time. | ||
==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 /proc/uptime 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== | |||
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 hostname 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 | |||
hostname | kangtain | ||
Some distributions allow you to set hostname for your machine, type: | Some distributions allow you to set hostname for your machine, type: | ||