Basic Linux Commands
Basic Linux Commands
Part 3
James KiarieLast Updated: February 16, 2024 Read Time: 12 minsCategoriesLFCA, Linux Certifications 9 Comments
288.2K
Linux system provides a vast pool of commands that you can use to administer
and manage your system and they are as follows.
1. uptime Command
The uptime command displays how long your system has been running since the
last time it was turned on. Without any arguments, it displays a host of
information such as the time the system has been running, users with running
sessions, and load average.
$ uptime
To get the exact date and time since the system was turned on, use the -s flag.
$ uptime -s
2021-03-17 09:20:02
To get the exact duration in a more user-friendly format append the -p flag.
$ uptime -p
up 1 hour, 55 minutes
The output below shows that the system has been up for 1 hour, 55 minutes.
2. uname Command
The uname command prints out basic information regarding your operating
system and underlying hardware. Without any arguments, the uname command
only prints out the operating system – which in this case is Linux.
$ uname
Linux
Append the -a flag to reveal all the information such as the kernel name,
version, release, machine, processor, and operating system.
$ uname -a
Linux ubuntu 5.4.0-65-generic #73-Ubuntu SMP Mon Jan 18 17:25:17 UTC 2021
x86_64 x86_64 x86_64 GNU/Linux
5.4.0-65-generic
$ uname -v
To see the type of kernel you are using, use the -s flag.
$ uname -s
Linux
$ uname --help
3. whoami Command
The whoami command displays the currently logged-in user as shown below.
$ whoami
tecmint
4. w Command
The w command provides information about currently logged-in users.
$ w
5. free Command
The free command gives information about the swap and main memory’s usage.
It displays the total size, used up and available memory
$ free
$ free -h
6. top Command
This is among the useful tools in a Linux system. The top command gives a
glimpse of the currently running processes and also provides a real-time
overview of the system resource usage.
At the very top of the output, you get information about the uptime, running tasks,
CPU, and memory usage.
$ top
Linux Resource Usage Summary
7. ps Command
The ps command lists the currently running process on the current shell
alongside their PIDs.
$ ps
8. sudo Command
A portmanteau for Super User do, sudo is a command-line utility that grants a
regular user ability to perform administrative or elevated tasks. Before using the
command, ensure that the user is first added to the sudo group. Once added,
begin the command with sudo first.
You will be prompted for the password upon which the task will be executed.
Update Ubuntu Using Sudo User
9. echo Command
The echo command does quite a number of things. First, it can print out the value
of a string on the terminal as shown.
You can also save a string to a file using the ( > ) redirection operator. If the file
does not exist, it will be created.
$ cat file1.txt
$ echo “We hope you will enjoy the ride” >> file1.txt
$ cat file1.txt
$ echo $USER
tecmint
$ echo $HOME
/home/tecmint
$ history
$ head /etc/ssh/ssh_config
View Lines in Text File
You can add the -n flag to specify the number of lines to be displayed. For
example, to display 5 lines run the command as follows:
$ head -n 5 /etc/ssh/ssh_config
$ tail /etc/ssh/ssh_config
View Last Lines in Text File
Just like the head command, you can define the number of lines to be displayed.
For example, to view the last 5 lines of a file, run:
$ tail -n 5 /etc/ssh/ssh_config
In its basic form, it downloads a file from a given URL. In the command below,
we are downloading the latest Linux kernel.
$ wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.11.4.tar.xz
The command begins by first resolving the IP address of the URL, upon which it
connects to the remote servers, and begins to download the file. The file is
downloaded to the current directory.
Wget Download File in Linux
To save a file to a different directory, use the -P flag followed by the path to the
directory followed by the URL. For example, to download a file to
the /opt directory, run the command.
To download and save a file under a different name, use the -O flag followed by
the desired file name.
$ finger tecmint
Login: tecmint Name: Tecmint
No mail.
No Plan.
$ alias show=ls -a
$ show
Create Alias in Linux
The passwd command allows you to change your password. Simply run the
passwd command as shown.
$ passwd
You will be prompted for your current password, upon which you will provide a
new password and later confirm it.
$ groups
OR
$ groups tecmint
tecmint sudo
18. du Command
Want to keep an eye on the disk usage of your files and folders? The du
command – short for disk usage – is the standard command for checking disk
usage of files and directories.
$ du OPTIONS FILE
To check the disk usage in another directory, for example /var/log/ run the
command:
$ du -h /var/log
19. df Command
The df command – short for disk free – checks the total disk space, space being
used and the available disk space in various file systems. It takes the syntax
shown below:
$ df OPTIONS FILE
The most crucial options are -T and -h . The -T flag prints the file system type
whilst the -h flag displays the output in a human-readable format.
The command below lists the free disk space in all the filesystems.
$ df -Th
$ ls -l
List Files in Linux
In columns 3 and 4, you can clearly see tecmint tecmint. The first of these points
to the user and the second entry refers to the group, which is also tecmint. When
a new user is created, they are assigned a new default group, of which they are
the only member by default. This is an indicator that the file(s) or directories are
not shared with anyone.
Using the chown command, you can change the file ownership quite easily.
Simply provide the name of the owner followed by the group name, separated by
a full colon ( : ) This is an elevated task and you will have to invoke the sudo
command.
For example, to change the group of the file1.txt to james but retain the owner
as tecmint run:
$ ls -l
Change File Ownership
To change both the owner as well as the group, run the command:
$ ls -l
$ ls -l
There are two ways of assigning file permissions: Numeric and symbolic (text)
notation. For Numeric notation, each of the flags represents a value as shown.
r = 4
w = 2
x = 1
No permissions = 0
To get the file permissions of a file simply add the corresponding values in all the
sets. For example:
drwxrwxr-x
We will set this to 775. This gives the owner and group of the file all permissions
– i.e rwx, and other users read and execute permissions only.
To assign group members read permissions only and not write and execute, run.
For example:
$ poweroff
Another command that accomplishes the same task is the shutdown command
as shown.
$ shutdown -h now
The -h flag stands for a halt, implying stopping the system. The second
parameter is the time option which can also be specified in minutes and hours.
The command below displays a message to all logged-in users notifying them of
the system shutdown that’s scheduled in 5 minutes.
$ reboot
Alternatively, you can reboot using the shutdown command with an -r option as
shown.
$ shutdown -r now
$ exit
$ man ls
Conclusion
That was a list of system commands that should help you get started in
managing your system and gather various insights. As the saying goes, practice
makes perfect. And it goes without saying that practicing these commands from
time to time will help you get better and sharper with your system.