Important Linux Commands
Important Linux Commands
DUBAI - UAE
Important Linux
Command
Of course, we can’t just skip studying Linux Commands after taking a tour with the
Microsoft “DOS” Commands in the previous post. macOS and Linux have a massive number
of built-in command-line utilities. Which you could spend the rest of your life learning all of
the Terminal commands!
But, in here we just going to introduce you to the Basic Linux Commands, which have been
covered by CompTIA A+, and may be a little beyond that.
-h --help
mohamed@mohamed-VM:~/Desktop$ cal -h
Usage: cal [general options] [-jy] [[month] year]
cal [general options] [-j] [-m month] [year]
ncal -C [general options] [-jy] [[month] year]
ncal -C [general options] [-j] [-m month] [year]
ncal [general options] [-bhJjpwySM] [-H yyyy-mm-dd] [-s country_code] [[month]
year]
ncal [general options] [-bhJeoSM] [year]
General options: [-31] [-A months] [-B months] [-d yyyy-mm]
$ help cd;
mohamed@mohamed-VM:~/Desktop$ help cd
cd: cd [-L|[-P [-e]] [-@]] [dir]
Change the shell working directory.
Change the current directory to DIR. The default DIR is the value of the
HOME shell variable.
The variable CDPATH defines the search path for the directory containing
DIR. Alternative directory names in CDPATH are separated by a colon (:).
A null directory name is the same as the current directory. If DIR begins
with a slash (/), then CDPATH is not used.
If the directory is not found, and the shell option `cdable_vars' is set,
the word is assumed to be a variable name. If that variable has a value,
its value is used for DIR.
Options:
-L force symbolic links to be followed: resolve symbolic
links in DIR after processing instances of `..'
-P use the physical directory structure without
following symbolic links: resolve symbolic links in
DIR before
-@
Exit Status:
Returns 0 if the directory is changed, and if $PWD is set successfully when
-P is used; non-zero otherwise.
-d, -s;
mohamed@mohamed-VM:~/Desktop$ help -d cd
cd - Change the shell working directory.
mohamed@mohamed-VM:~/Desktop$ help -s cd
cd: cd [-L|[-P [-e]] [-@]] [dir]
man;
mohamed@mohamed-VM:~/Desktop$ man cal
NAME
cal, ncal — displays a calendar and the date of Easter
SYNOPSIS
cal [-31jy] [-A number] [-B number] [-d yyyy-mm] [[month] year]
cal [-31j] [-A number] [-B number] [-d yyyy-mm] -m month [year]
ncal [-C] [-31jy] [-A number] [-B number] [-d yyyy-mm] [[month] year]
ncal [-C] [-31j] [-A number] [-B number] [-d yyyy-mm] -m month [year]
ncal [-31bhjJpwySM] [-A number] [-B number] [-H yyyy-mm-dd] [-d yyyy-mm] [-s
country_code] [[month] year]
ncal [-31bhJeoSM] [-A number] [-B number] [-d yyyy-mm] [year]
DESCRIPTION
The cal utility displays a simple calendar in traditional format and ncal offers an
alternative layout, more options and the date of
Easter. The new format is a little cramped but it makes a year fit on a 25x80 terminal.
If arguments are not specified, the cur‐
rent month is displayed.
-J Display Julian Calendar, if combined with the -o option, display date of Orthodox
Easter according to the Julian Calendar.
info;
mohamed@mohamed-VM:~/Desktop$ info bash
NAME
bash - GNU Bourne-Again SHell
SYNOPSIS
bash [options] [command_string | file]
COPYRIGHT
Bash is Copyright (C) 1989-2018 by the Free Software Foundation, Inc.
DESCRIPTION
Bash is an sh-compatible command language interpreter that executes commands read
from the standard input or from a file. Bash
also incorporates useful features from the Korn and C shells (ksh and csh).
OPTIONS
All of the single-character shell options documented in the description of the set
builtin command, including -o, can be used as
options when the shell is invoked. In addition, bash interprets the following options
when it is invoked:
-c If the -c option is present, then commands are read from the first non-option
argument command_string. If there are ar‐
guments after the command_string, the first argument is assigned to $0 and
any remaining arguments are assigned to the
positional parameters. The assignment to $0 sets the name of the shell,
which is used in warning and error messages.
-i If the -i option is present, the shell is interactive.
-l Make bash act as if it had been invoked as a login shell (see
INVOCATION below).
Info: (*manpages*)bash, 4136 lines Top
whatis;
mohamed@mohamed-VM:~/Desktop$ whatis cal
cal (1)- displays a calendar and the date of Easter
mohamed@mohamed-VM:~/Desktop$ cal 2022 أكتوبر
ح ن ث ر خ ج س
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
mohamed@mohamed-VM:~/Desktop$
sudo lshw;
mohamed@mohamed-VM:~/Desktop$ sudo lshw
[sudo] password for mohamed:
mohamed-vm
description: Computer
product: VMware Virtual Platform
vendor: VMware, Inc.
version: None
serial: VMware-56 4d d4 46 27 6b cc e4-6b d4 d8 6f fa 5f 06 ca
width: 64 bits
capabilities: smbios-2.7 dmi-2.7 smp vsyscall32
configuration: administrator_password=enabled boot=normal
frontpanel_password=unknown keyboard_password=unknown power-
on_password=disabled uuid=564DD446-276B-CCE4-6BD4-D86FFA5F06CA
*-core
description: Motherboard
product: 440BX Desktop Reference Platform
vendor: Intel Corporation
physical id: 0
version: None
serial: None
*-firmware
description: BIOS
vendor: Phoenix Technologies LTD
physical id: 0
version: 6.00
date: 11/12/2020
size: 86KiB
Linux commands;
$ ls Display the directories and files.
$ ls -l | more Displays file’s ownership and permissions.
| more, enables you to see the output page by page.
$ cd “/” To change the directory.
$ cd ~ “cd ~” takes you to the home directory.
$ grep <pattern> To look for a text in a file (search).
[file]
$ ps aux | grep Grep in here is used to filter the output (filter).
“pattern”
$ su Elevates the privilege to Super User, until you exit the mode.
$ sudo “sudo” enables you to run a single command in the Privilege
mode and then get you back to the normal mode.
ls;
mohamed@mohamed-VM:/var/log$ ls -l
total 2604
-rw-r--r-- 1 root root 33052 Oct 5 15:54 alternatives.log
drwxr-xr-x 2 root root 4096 Oct 5 15:49 apt
-rw-r----- 1 syslog adm 10436 Oct 5 17:56 auth.log
-rw------- 1 root root 9137 Oct 5 14:54 boot.log
drwxr-xr-x 2 root root 4096 Oct 5 14:54 cups
drwxr-xr-x 2 root root 4096 Feb 8 2022 dist-upgrade
-rw-r--r-- 1 root adm 131892 Oct 5 15:52 dmesg
-rw-r--r-- 1 root root 10973 Oct 5 15:54 fontconfig.log
drwx--x--x 2 root gdm 4096 Jul 12 2021 gdm3
-rw-r--r-- 1 root root 1478 Oct 5 14:54 gpu-manager.log
drwxr-xr-x 3 root root 4096 Feb 23 2022 hp
drwxrwxr-x 2 root root 4096 Oct 5 14:42 installer
cd;
mohamed@mohamed-VM:~$ cd Documents/
mohamed@mohamed-VM:~/Documents$
Hint; we use the forward-slash “/” to specify the path with Linux, not the back-slash as in
windows.
Hint; the Linux Terminal is case-sensitive.
cd ~;
mohamed@mohamed-VM:/var/log cd ~
mohamed@mohamed-VM:~$
grep;
mohamed@mohamed-VM:/var/log$ grep Bluetooth dmesg
[ 2.771074] kernel: usb 2-2.1: Product: Virtual Bluetooth Adapter
[ 6.165141] kernel: Bluetooth: Core ver 2.22
[ 6.165165] kernel: Bluetooth: HCI device and connection manager initialized
[ 6.165169] kernel: Bluetooth: HCI socket layer initialized
[ 6.165171] kernel: Bluetooth: L2CAP socket layer initialized
[ 6.165175] kernel: Bluetooth: SCO socket layer initialized
[ 17.468829] kernel: Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[ 17.468834] kernel: Bluetooth: BNEP filters: protocol multicast
[ 17.468839] kernel: Bluetooth: BNEP socket layer initialized
[ 150.925637] kernel: Bluetooth: RFCOMM TTY layer initialized
[ 150.925645] kernel: Bluetooth: RFCOMM socket layer initialized
[ 150.925649] kernel: Bluetooth: RFCOMM ver 1.11
| grep;
mohamed@mohamed-VM:~$ ps aux | grep "libre"
mohamed 54009 0.0 0.1 96096 4848 ? Sl 17:17 0:00 /usr/lib/libreoffice/program/oosplash --
writer
mohamed 54026 6.5 5.4 1111876 217036 ? Sl 17:17 0:02 /usr/lib/libreoffice/program/soffice.bin
--writer
mohamed 54044 0.0 0.0 9044 656 pts/0 R+ 17:18 0:00 grep --color=auto libre
su / sudo;
mohamed@mohamed-VM:~$ su
Password:
shutdown commands;
$ shutdown 2 Shuts down the system in two minutes.
$ shutdown -r 2 Reboots the system after two minutes.
$ shutdown -r now Reboots the system immediately.
$ shutdown -c or ctrl + C Cancels the shutdown commands
shutdown -r ;
mohamed@mohamed-VM:~$ sudo shutdown -r 2
[sudo] password for mohamed:
Shutdown scheduled for Thu 2022-10-06 11:51:20 +04, use 'shutdown -c' to cancel.
mohamed@mohamed-VM:~$ sudo shutdown -c
mohamed@mohamed-VM:~$
pwd / passwd;
$ pwd (print working Display the current working directory path.
directory)
$ passwd (password) Used to change the user account password.
$ passwd [username] To change the password for another account, not the one you
currently using.
pwd;
mohamed@mohamed-VM:~$ pwd
/home/mohamed
passwd;
mohamed@mohamed-VM:~$
passwd Changing password for
mohamed. Current password:
New password:
Retype new password:
Hint; Linux does display the password while you’re typing it, so be careful.
File management;
$ mv Rename the file ($ mv first.txt second.txt)
$ cp Copy the file content.
$ rm Delete a file.
$ mkdir Create a directory
$ touch To create a file.
$ chmod Change mode of a file system object
$ chmod a- Will prevent all use from writing on the file. All users has only read permission.
w
$ chmod To the right of execution for the user only.
u+x
$ chown To change the file’s ownership.
mv;
mohamed@mohamed-VM:~/Documents$ ls -l
total 12
-rw-rw-r-- 1 mohamed mohamed13 Oct 6 12:08 first.txt
drwxr-xr-x 2 mohamed mohamed 4096 Oct 5 14:56 Music
drwxr-xr-x 2 mohamed mohamed 4096 Oct 5 14:56 Videos
cp;
mohamed@mohamed-VM:~/Documents$ cp second.txt third.txt
mohamed@mohamed-VM:~/Documents$ ls -l total 16
drwxr-xr-x 2 mohamed mohamed 4096 Oct
-rw-rw-r-- 1 mohamed mohamed13 Oct 14:56 Music
-rw-rw-r-- 1 mohamed mohamed13 Oct 12:08 second.txt
6 12:26 third.txt
drwxr-xr-x 2 mohamed mohamed 4096 Oct 5 14:56 Videos
mohamed@mohamed-VM:~/Documents$
rm;
mohamed@mohamed-VM:~/Documents$ rm second.txt
mohamed@mohamed-VM:~/Documents$ ls -l total 12
drwxr-xr-x 2 mohamed mohamed 4096 Oct
-rw-rw-r-- 1 mohamed mohamed13 Oct 14:56 Music
12:26 third.txt
drwxr-xr-x 2 mohamed mohamed 4096 Oct 5 14:56 Videos
Hint; “rm” allows you to remove only files or an empty directory, but if you need to delete an
entire directory with files in it you will need to use the “-r”.
mohamed@mohamed-VM:~$ rm Documents/
rm: cannot remove 'Documents/': Is a directory mohamed@mohamed-VM:~$ rm -r Documents/ mohamed@
total 32
mkdir;
mohamed@mohamed-VM:~/Documents$ mkdir note
STAY HUNGRY STAY FOOLISH MOHAMED AHMED IBRAHIM
MOHAMED AHMED IBRAHIM
DUBAI - UAE
mohamed@mohamed-VM:~/Documents$ ls -l
total 16
drwxr-xr-x 2 mohamed mohamed 4096 Oct 14:56 Music
drwxrwxr-x 2 mohamed mohamed 4096 Oct 6 12:45 note
-rw-rw-r-- 1 mohamed mohamed13 Oct 12:26 third.txt
drwxr-xr-x 2 mohamed mohamed 4096 Oct 5 14:56 Videos
chmod;
Change mode of a file system object
r=read, w=write, x=execute.
Can also use octal notation.
Set for the file owner (u), the group(g), others(o), or all(a).
Let’s edit the permissions on the folder “Note”
mohamed@mohamed-VM:~/Documents$ ls -l
total 16
drwxrwxr-x 2 mohamed mohamed 4096 Oct 6 12:45 note
# Permission r w x
7 Read, Write, and Execute r w x
6 Read and Write r w -
5 Read and Execute r - x
4 Read only r - -
3 Write and Execute - w x
2 Write only - w -
1 Execute only - - x
0 none - - -
To change these permissions, you need to use the numbers’ representation of permissions.
mohamed@mohamed-VM:~/Documents$ chmod 764 note
mohamed@mohamed-VM:~/Documents$ ls -l
total 16
drwxrw-r-- 2 mohamed mohamed 4096 Oct 6 12:45 note
mohamed@mohamed-VM:~/Documents$
This means;
STAY HUNGRY STAY FOOLISH MOHAMED AHMED IBRAHIM
MOHAMED AHMED IBRAHIM
DUBAI - UAE
chmod a-w;
mohamed@mohamed-VM:~/Documents$ chmod a-w third.txt
mohamed@mohamed-VM:~/Documents$ ls -l
total 16
-r--r--r-- 1 mohamed mohamed 13 Oct 6 12:26 third.txt
chmod u+x;
mohamed@mohamed-VM:~/Documents$ chmod u+x Script.sh
mohamed@mohamed-VM:~/Documents$ ls -l total 28
drwxrw-r-- 2 mohamed mohamed 4096 Oct
-rwxrw-r-- 1 mohamed mohamed 8341 Oct 6 12:45 note
6 13:23 Script.sh
-r--r--r-- 1 mohamed mohamed13 Oct 6 12:26 third.txt
Create files;
$ touch [filename.txt] Only creates the file, but don’t open it.
$ cat > [filename.txt] Creates the file and automatically give you access to edit it.
$ > [filename.txt] Only creates the file, but don’t open it.
touch file.txt;
mohamed@mohamed-VM:~$ touch test1.txt
mohamed@mohamed-VM:~$ ls
> file.txt;
mohamed@mohamed-VM:~$ > test2.txt
mohamed@mohamed-VM:~$ ls
ifconfig / iwconfig;
$ iwconfig Is used to view and configure the wireless network adaptors.
$ ifconfig Is used to view configure the wired network adaptors.
$ ip Some Linux distributions use “ip address” instead of
address “ifconfig”.
iwconfig
mohamed@mohamed-VM:~$ iwconfig
lo no wireless
ifconfig
mohamed@mohamed-VM:~$ iwconfig
Command 'ifconfig' not found, but can be installed with:
ip address
mohamed@mohamed-VM:~$ ip address
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen
1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UNKNOWN
group default qlen 1000
link/ether 00:0c:29:5f:06:ca brd ff:ff:ff:ff:ff:ff
altname enp2s1
inet 192.168.255.132/24 brd 192.168.255.255 scope global dynamic noprefixroute
ens33
valid_lft 1325sec preferred_lft 1325sec
inet6 fe80::7594:3f46:176e:a621/64 scope link noprefixroute
valid_lft forever preferred_lft forever
Process managing;
$ ps Display all the processes running by the current user.
$ ps -e | Display all the processes running on the system.
more
$ ps aux Display the Running process in a very detail view.
ps
mohamed@mohamed-VM:~$ ps
PID TTY TIME
CMD
50948 pts/0 00:00:00 bash
ps -e | more
mohamed@mohamed-VM:~$ ps -e | more
STAY HUNGRY STAY FOOLISH MOHAMED AHMED IBRAHIM
MOHAMED AHMED IBRAHIM
DUBAI - UAE
pa aux
mohamed@mohamed-VM:~$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 2 0.0 0.0 0 0 ? S 06:37 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 06:37 0:00 [rcu_gp]
root 4 0.0 0.0 0 0 ? I< 06:37 0:00 [rcu_par_gp]
root 18 0.0 0.0 0 0 ? S 06:37 0:00 [cpuhp/0]
systemd+ 47640 0.0 0.1 90908 6064 ? Ssl 09:37 0:00
/lib/systemd/systemd-timesyncd
root48139 0.0 0.2 314664 9268 ? Sl09:380:00 gdm-session-worker
[pam/gdm-password]
mohamed48147 0.0 0.2 19724 10440 ?Ss09:380:01
/lib/systemd/systemd --user
mohamed 48148 0.0 0.0 170536 3800 ? S 09:380:00 (sd-pam)
mohamed
mohamed@mohamed-VM:~$ ps -e
{you’ll see nothing about firefox}
STAY HUNGRY STAY FOOLISH MOHAMED AHMED IBRAHIM
MOHAMED AHMED IBRAHIM
DUBAI - UAE
mohamed@mohamed-VM:~/Documents$ vi first.txt
VI Editing commands:
i – Insert at cursor (goes into insert mode)
a – Write after cursor (goes into insert mode)
A – Write at the end of line (goes into insert mode)
ESC – Terminate insert mode
u – Undo last change
U – Undo all changes to the entire line
dw – Delete word
4dw – Delete 4 words
cw – Change word
x – Delete character at the cursor
r – Replace character
R – Overwrite characters from cursor onward
s – Substitute one character under cursor continue to insert
S – Substitute entire line and begin to insert at the beginning of the line
~ – Change case of individual character