Shell Commands
Shell Commands
Ubuntu Shell
Commands P2
Shell - Bash
TABLE OF CONTENTS
01 02
Overview Learning
topics Objectives
03 04
Quiz hands on lab
questions practice
01
OVERVIEW topics
OVERVIEW Topics
pwd
ls
cd
less
touch
Ubuntu Shell cp
Commands mv
rm
Topics
mkdir
clear
rmdir
OVERVIEW Topics
grep
ping
Commands ps
Topics sudo
su
chown
chmod
Slides On Telegram
https://t.me/alx_2023
Ubuntu Shell
Commands
Topics
02
Learning Objectives
pwd
This command stands for "print working directory."
pwd
current_dir=$(pwd)
echo "Current directory: $current_dir"
cp some_file.txt $(pwd)/../some_other_directory/
ls
This command stands for "list." When
executed, it displays a list of files and
directories in the current directory
(folder) you are in within the terminal.
ls –a
This will list all files and directories in the current
directory, including hidden files.
ls -l example.txt
display additional information about each file, such
as permissions, ownership, size, and modification
date
ls –lt
sort files by modification time
ls -ls
sort files by modification time (in reverse
order) or by size
cd
This command stands for "change directory."
When executed, it allows you to navigate to a
different directory (folder) within the
terminal.
cd ~
This command takes you to your home
directory. Your home directory is a default
directory created for each user on the system,
and it's usually located at
/home/yourusername/.
cd /
This command takes you to the root directory
of the file system. The root directory is the
top-level directory in the file system
hierarchy and contains all other directories
and files on the system.
less
This command is a program used to view text
files within the terminal. It displays the
contents of a file one screenful at a time and
allows you to scroll through the file.
less /path/to/file
/search_term
This will search for the text "search_term" in the
file.
less file.gz
it will show you the contents of the compressed file
without extracting it, another formats supported
“.bz2” “.xz”
touch myfile.txt
cp file.txt /path/to/new/location/
cp -r /path/to/source/ /path/to/destination/
This will copy the "source" directory and all of its
contents to the "destination" directory recursively.
cp file.txt newfile.txt
This will create a copy of "file.txt" with the new name
"newfile.txt" in the same directory as the original file.
mv
This command stands for "move." When executed,
it moves one or more files from one location to
another. It can also be used to rename files.
//Moving files
mv ~/example.txt ~/documents/
//Renaming files
mv example.txt exam.txt
mv /path/to/source/ /path/to/destination/
This will move the "source" directory and all of its
contents to the "destination" directory.
rm
This command stands for "remove."
When executed, it deletes one or more
files or directories from the system. Be
careful when using this command, as it
permanently deletes files and
directories.
rm ~/example.txt
rm ~/*.txt
rm –rf ~/directory
This will forcefully remove the "directory"
directory and all of its contents without
prompting for confirmation.
mkdir
This command stands for "make directory."
When executed, it creates a new directory
(folder) in the current directory (folder) you
are in within the terminal.
mkdir ~/documents
mkdir -p my_parent_directory/my_directory
This will create the directory my_directory within the
directory my_parent_directory. If
my_parent_directory does not exist, it will be created
along with my_directory.
rmdir
This command stands for "remove
directory." When executed, it deletes
an empty directory (folder) from the
system. If the directory contains
files or subdirectories, you must first
remove those before using rmdir.
rmdir ~/documents
clear
This command empty the console
grep
This command searches for a specific pattern
within a file or files. It can be used to search
for specific text or to filter results.
grep "example" file.txt
ping -i 2 google.com
This would send packets to the google.com host with an interval of 2
seconds between them
ping -6 2001:4860:4860::8888
This would send packets to the IPv6 address
top
This command is a program that displays a real-
time view of the processes running on the system,
including CPU usage, memory usage, and more.
if you want to sort processes by memory usage, you can press the
M key. Similarly, if you want to sort processes by process ID, you
can press the P key.
top -p 1234
This will display information about the process with ID
1234.
top -n 5
This will display the top 5 processes by CPU usage.
top -d 5
This will update the display every 5 seconds.
ps
This command stands for "process status." It displays
information about the currently running processes on the
system.
ps -e --sort=-pcpu --format='pid,comm,%cpu,%mem'
This will display all processes, sorted in descending order by
CPU usage, and the process ID, command name, and percentage
of CPU and memory used.
sudo
It allow users to execute commands with elevated privileges
or permissions. It stands for "superuser do" and allows a
regular user to temporarily elevate their privileges to those of
the system administrator or root user. This is necessary for
carrying out certain system-level tasks that require special
permissions.
sudo vi /etc/fstab
open and edit the system file.
When you run sudo -i and enter your password, you will be
switched to the root user's environment and will have full
root-level access to the system until you exit the shell by
typing exit or pressing Ctrl-D.
su
The su command is used to switch to another user account or
become a superuser.
su <username>
Share
To let the others know more
Thanks