Generally, there are two user interfaces for managing Linux UI:
A shell is a type of command-line interface (CLI). Shell is essentially a text-based tool for communication between the user and the operating system's Kernel. The path is: User => Shell => Kernel => Hardware.
Types of Shells:
To check the current shell:
# Current using Shell
[username@hostname ~]$ echo $SHELL
# List all shells
[username@hostname ~]$ cat /etc/shells
Each command in Linux has two parts:
Prompt Structure
The general command structure is:
username@hostname current_directory shell_type
Example:
username@hostname:~$[username@hostname ~]$Explanation:
$ indicates a regular user# indicates the root user~Command Structure
We can complete the commands with tab. Linux commands structure:
command [options/parameters] [arguments]
Example:
If we want to use whole world, we need to use -- and with one letter we use -:
# List all files with hidden files
[username@hostname ~]$ ls -a /home/username
# List all files with hidden files as a long list
[username@hostname ~]$ ls -l /home/username
# Same as -a but -a is shorter
[username@hostname ~]$ ls --all /home/username
[username@hostname ~]$ ls -la /home/username
Built-in (Internal = Shell Built-in) Commands:
External Commands:
To check the type of the command:
[username@hostname ~]$ type command-name
List of all built-in commands
| Command | Description |
|---|---|
alias | Create an alias for a command. |
bg | Place a job in the background. |
bind | Edit and display the command line editor's key bindings. |
break | Exit from a loop. |
cd | Change the current directory. |
command | Run a command, ignoring shell functions. |
echo | Display a line of text. |
enable | Enable or disable shell built-ins. |
eval | Execute arguments as a shell command. |
exec | Replace the shell with a command. |
exit | Exit the shell. |
export | Set environment variables. |
fc | List or edit the command history. |
fg | Bring a job to the foreground. |
getopts | Parse command options. |
hash | Remember the full path of commands. |
help | Display help for built-in commands. |
history | Show the command history. |
jobs | List active jobs. |
kill | Terminate a process. |
let | Evaluate arithmetic expressions. |
local | Create a variable that is local to a function. |
logout | Exit a login shell. |
mapfile | Read lines from standard input into an array variable. |
popd | Remove directories from the directory stack. |
pushd | Add directories to the directory stack. |
pwd | Print the current working directory. |
read | Read a line of input. |
readonly | Mark variables as read-only. |
return | Exit a function and return a value. |
set | Set shell options and positional parameters. |
shift | Shift positional parameters. |
source | Execute commands from a file in the current shell. |
test | Evaluate conditional expressions. |
times | Print the user and system times for processes. |
trap | Execute a command when a signal is received. |
type | Describe a command. |
ulimit | Control the resources available to the shell. |
umask | Set the file creation mask. |
unalias | Remove an alias. |
unset | Remove variable or function names. |
wait | Wait for a process to complete. |
Switching between different directories using the cd command:
[username@hostname ~]$ cd /home/username/Documents
Viewing the current directory using the pwd command:
[username@hostname ~]$ pwd
Notes:
. Represents the Current Location... Represents the Parent Location.Examples:
[username@hostname Documents]$ cd ..
[username@hostname ~]$ cd ./etc
[username@hostname ~]$ cd ../Downloads
[username@hostname ~]$ cd ../../..
## To switch between last two paths
[username@hostname ~]$ cd -
Printing custom text using the echo command. We can use " or ' or without any characters:
[username@hostname ~]$ echo "Lets start learning Linux"
You mush use a backslash before the following special characters if you don't use single or double quotes:
| & ; ( ) < > [ ] { } * ! ? ` ' " $ \ / #
Example:
[username@hostname ~]$ echo Hello\;Bye
[username@hostname ~]$ echo Foo\\Bar
# With ' or " you do not need a \
[username@hostname ~]$ echo "Hello;Bye"
[username@hostname ~]$ echo 'Foo\Bar'
[username@hostname ~]$ ls
- it is a file and if starts with d it is a directory. We can have the access, size and last modified time:# Long-list
[user@host ~]$ ls -l
# All with hidden files
[user@host ~]$ ls -a
# Show inside a directory, d = directory
[user@host ~]$ ls -d
# With time
[user@host ~]$ ls -t
# With size and reverse
[user@host ~]$ ls -Sr
# Long-List + Reverse Time + Human-Readable
[user@host ~]$ ls -ltrh
Viewing the name of the logged-in user using the whoami command:
[username@hostname ~]$ whoami
Viewing the list of all users currently logged into the system using the w command:
[username@hostname ~]$ w
Explanation:
For the TTY column, we can have two options:
tty: In Linux, tty stands for teletypewriter. It refers to the terminal device that is used for input and output in a Linux system. The term originates from the early days of computing when physical teletype machines were used for communication with computers. In a normal Linux OS, we can have 1 to 6 TTY.
pts: stands for Pseudo-Terminal Session. If we connect with SSH protocol (ssh username@hostname). To close the ssh terminal and close the pts and back to the tty, enter: exit
To run something with the root access
We have two options:
su command. We can switch to the root user to do some actions:[username@hostname ~]$ su - root
sudo (superuser do). We can use sudo if the user is a suder and can access to sudo.# Change the hostname
[username@hostname ~]$ sudo hostname newhostname
Note:
In Ubuntu Desktop, the default root user has no password by default. We need to sudo su - root then passwd to set the password for the root user. After this password set, we can switch to root user with su - root
Note:
The root user can switch to any user without knowing the password. To reset the password for the user, with the root user: passwd username
Filtering files based on their names that have a specific pattern:
Note:
It is better to name files with the date to search better in the future like file20250712. To search and show all the files in the 07 month: ls file202507*
Examples:
# List all files in the current directory that start with "file" and end with ".conf"
[username@hostname ~]$ ls file*.conf
# List all files that start with "file1" and end with "1", with any characters in between
[username@hostname ~]$ ls file1*1
# List all files that start with "file202007" followed by any characters
[username@hostname ~]$ ls file202007*
# List all files that start with "file", have exactly one character in between, and end with "1"
[username@hostname ~]$ ls file?1
# List all files in the current directory that have a three-letter extension
[username@hostname ~]$ ls *.???
# List all files that start with "file2020070" followed by exactly one character
[username@hostname ~]$ ls file2020070?
# List all files that start with "file" followed by a single digit between 1 and 5
[username@hostname ~]$ ls file[1-5]
# List directories (and their details) that start with any letter from "c" to "h"
[username@hostname ~]$ ls -ld [c-h]*
# List files that start with "file" followed by either "1", "5", or "7"
[username@hostname ~]$ ls file[1,5,7]
# List files that start with "file" followed by either "1", "5", or "7" (no commas needed)
[username@hostname ~]$ ls file[157]
# List files that start with "file" but do not have "1", "5", or "7" as the next character
[username@hostname ~]$ ls file[!157]
# List all files in the current directory that have a ".txt" extension
[username@hostname ~]$ ls *.txt
# List all files that start with any letter from "e" to "z"
[username@hostname ~]$ ls [e-z]*
# List all files that do not start with any letter from "e" to "z"
[username@hostname ~]$ ls [!e-z]*
# List all files that either start with "file" or have a name that starts with "log" and has a two-letter extension
[username@hostname ~]$ ls {file*,log.??}
# List files that start with "file_2020070" followed by either "1", "2", "3", "4", or "5"
[username@hostname ~]$ ls file_2020070[1,2,3,4,5]
There are two kind of variables in Linux:
Local Variables
We can define a variable in Linux and assign a value to it. To show the content of the variable, we can use $var_name. These variables store on the memory and will remove after reboot.
[username@hostname ~]$ myname="Foo"
[username@hostname ~]$ let "myname=Foo"
[username@hostname ~]$ echo $myname
[username@hostname ~]$ set ${myname:=Bar}
[username@hostname ~]$ export $myname
Note:
Getting the value of a variable from user input using the read command:
# p is print
[username@hostname ~]$ read -p "Please enter your age: " myage
# n is the number of characters
[username@hostname ~]$ read -n 8 myname
# t is the timeout value for the user to enter the input
[username@hostname ~]$ read -t 60 myvar
Environment Variables
Environment variables in Linux are dynamic values that affect the behavior of processes and applications, providing configuration settings such as system paths, user preferences, and system-wide settings that can be accessed by programs and scripts. They don't remove after reboot.
[username@hostname ~]$ export $myname
env command:[username@hostname ~]$ env
Some important and useful environment variables:
Examples:
[username@hostname ~]$ echo "My hostname is $HOSTNAME"
[username@hostname ~]$ ls file_$HOSTNAME_202105*.log
Each alias is specific for each user.
To define an alias the syntax is:
[username@hostname ~]$ alias name="command"
Viewing the list of defined aliases and removing a previously defined alias:
[username@hostname ~]$ alias
[username@hostname ~]$ unalias list
[username@hostname ~]$ unalias yesterday
Examples:
[username@hostname ~]$ alias list="ls -la"
[username@hostname ~]$ alias yesterday="date -d yesterday"
Adding Permanent Aliases
.bashrc file inside the home directory of each user. This .bashrc file is a hidden file. (rc = run command)[username@hostname ~]$ vim ~/.bashrc
...
# User specific aliases and functions
alias yesterday="date -d yesterday"
alias list="ls -lrth"
...
bash.bashrc file in etc directory. We put all the config files inside the etc directory:[username@hostname ~]$ su - root
[username@hostname ~]$ vim /etc/bash.bashrc
Viewing the usage guide of a command and its options with the man command. Go to the next page with space and back to the previous page with b. To search a word, enter /search-keyword and for iterating other found options use n key. To back to the previous found items, use shift + n. Exit with q.
[username@hostname ~]$ man date
man command has 9 sections. For example passwd is in section 1 (as a command) and 5 (as a config file). To get the passwd from section 5, we need to pass -S. To check all the sections use man man:
[username@hostname ~]$ man -S 5 passwd
# To check all the sections that passwd is involved
[username@hostname ~]$ man -a passwd
Finding the document file path of a man page with the -w option:
[username@hostname ~]$ man -w ls
Some of the commands don't have man description, we need to use help or apropos or info command:
# Using the help command only for built-in Linux commands
[username@hostname ~]$ help cd
[username@hostname ~]$ cd --help
[username@hostname ~]$ cd -h
# Using apropos as another method to get help
[username@hostname ~]$ apropos cd
# The last command for getting help is using info
[username@hostname ~]$ info cd
File timestamps:
Atime (Access Time): The last time the file was viewed by a command.Mtime (Modified Time): The last time the content of the file was changed.Ctime (Change Time): The last time the content of the file or its attributes (like chmod and permissions) were changed.Viewing the timestamps of a directory/file with the stat command:
[username@hostname ~]$ stat /etc/hosts
Updating the all timestamps of a directory/file with the touch command:
[username@hostname ~]$ touch File1.txt
If the file name doesn't exist, touch command creates a new file:
[username@hostname ~]$ touch File_new.txt
Useful options for the touch command:
-a : Change Access time only.-d : Change Access & Modification times.-m : Change Modification time only.-t : Change Access & Modification Times using a specified time.Updating the Atime and Mtime of a file to a specified date. We can't change the Ctime and birth with touch command:
# The number is Unix timestamp
[username@hostname ~]$ touch -t 9812010510 File1.txt
Displaying the current date:
[username@hostname ~]$ date
Displaying the date in the format DD-MM-YYYY:
[username@hostname ~]$ date +%Y%m%d
Changing the system date to 09:30 AM on February 8, 2015:
[username@hostname ~]$ date -s "8 Feb 2015 09:30"
Changing the system time to 18:10:
[username@hostname ~]$ date -s "18:10"
Displaying the UTC (Greenwich) time:
[username@hostname ~]$ date --utc
List of files/directories
List files and directories with ls:
# Long-list
[user@host ~]$ ls -l
# All with hidden files
[user@host ~]$ ls -a
# Show inside a directory, d = directory
[user@host ~]$ ls -d
# With time
[user@host ~]$ ls -t
# With size and reverse
[user@host ~]$ ls -Sr
# Long-List + Reverse Time + Human-Readable
[user@host ~]$ ls -ltrh
View the list of files and directories as a tree:
[username@hostname ~]$ tree
View Content of a file
Viewing the content of text files with the cat command:
[username@hostname ~]$ cat file1.txt
Displaying line numbers at the beginning of each line of the requested text:
[username@hostname ~]$ cat -n file1.txt
Create a new directory
Creating three directories using mkdir (make directory):
[username@hostname ~]$ mkdir dir1 dir2 dir3
Creating nested directories with the -p option:
[username@hostname ~]$ mkdir -p dir1/dir2/dir3/dir4
Displaying a message/status report upon the creation of a directory with the -v option. The v is the first character of verbose that means making noise and being so noisy:
[username@hostname ~]$ mkdir -v dir1
To clear the command line:
CTRL + L or clear command
Copy directories/files
Copying directories/files with the cp command:
[username@hostname ~]$ cp File1 File2 File3 /tmp
[username@hostname ~]$ cp /etc/passwd .
-r option.-v option for displaying a status report of the copy operation.-f option for copying and overwriting a file without prompting.[username@hostname ~]$ cp -r /etc ~/dir1
Move directories/files
Moving directories/files with the mv command. This command does not need -r option.
-v option for displaying a status report of the move operation.-f option for moving and overwriting a file without prompting.[username@hostname ~]$ mv /home/arash/File1.txt /tmp
[username@hostname ~]$ mv File1 File2 File3 ~/dir1
Renaming files/directories with the mv command:
[username@hostname ~]$ mv File1 New_File1
Remove directories/files
Deleting files using the rm command:
[username@hostname ~]$ rm File1.txt
Deleting directories using the -r option.
-v option for displaying a status report of the deletion.-f option for deleting and overwriting a file without prompting.[username@hostname ~]$ rm -r dir1
Naming Rules of File & Directories:
dot is used at the beginning of its name.# < > # @ & | space tab newline & } { $ ! ~ ) ( ] [ ? - ; * ' " \ /
A
A
A
$, #, and ~ represent?A
A
- and when do you use -- in commands?A) - for users, -- for groups
B) - for files, -- for directories
C) - for commands, -- for arguments
D) - for short options, -- for long options
A
A
A
A
A
. and .. represent?A
A) cd ..
B) cd -
C) cd ~
D) cd /
A
A
A
A
A
A
A
A
A
A
A
?, &, #, {}#, @, &, {}*, ^, ), {}*, ?, [], {}A
A
A
A) ls .txt
B) ls file[1-5]
C) ls file202007
D) ls [e-z]*
A
A
A
A
A
A
A
A
A
A
A
A
ls [!e-z]*ls file[1-5]ls *.txtls {file*,log.??}A
A
define myvar="value" | display myvar | protect myvar | set myvarvar myvar="value" | usemyvar | const myvar | env myvarlocal myvar="value" | print myvar | lock myvar | export myvarmyvar="value" | echo $myvar | set ${myvar:=newvalue} | export $myvarA
A
A
A
.bashrc for user - /etc/bash.bashrc for allA
A
A
A
A
A
A
A
A
A
A
A
A
A
A
A