0% found this document useful (0 votes)
9 views

Linux Commands

Uploaded by

tahatarek7770
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Linux Commands

Uploaded by

tahatarek7770
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Linux Commands

Two ways to write the command :


command -option /argument
command /argument -option

Two ways to change the shell (Virtual Shell) :


alt + ctrl + (f1 or f2 or f3 or f4 or f5 or f6)
alt + (< or >)

Ways to write multiple commands:


Ls ; cal ; pwd ---→ all will executed
Ls && cal ---→ the first one should executed to execute the second
Ls /et || cal ---→ or (any one will executed if it could)

Every thing in LINUX is a file.


d → directory
l → link
c → character device
b → block device

command option Argument illustration


ls ll (List) Showing every file and folder in the working
directory

Note: the file with black color


the folder with blue color
/another To show files and folders for another directory
ls directory
ls -a /home To show all hidden files and folders for that are ib home
-a (all) Showing every hidden file and folder in the working
ls
directory
--all
Note: each file or folder start with . means that is hidden
one
-l (long list) Showing more details about files and
ls
directories
Note: in terminal each permission for file start with –
each permission for folder start with dr
-lh (Long list Human readable)
ls
-l -a You can write them together to return long list and
ls -la hidden files and folders
--help For showing all abbreviations for ls
ls
-lt (Access Time)For showing them according to time
ls
-lr For showing them according to alpahbet
ls
-R (Recursive) showing all content in every folder and
ls
directory
dir For showing files and folders but without color
dir --color = ls
pwd (Print Working Directory) for showing the current
directory
Date For showing the current time and date
Cal (Calendar) For showing a calendar for the current month
cal 4 For showing a calendar for April in the current year
cal 2027 For showing a calendar for 2027
cal 4 2023 For showing a calendar for April in 2023
who For showing all users
whoami For showing the current user name
hostname For showing the machine name
clear To clear terminal
ctrl + l To clear terminal but the previous command will be
above
reset To clear terminal
tty (Telnet Terminal Type) Making terminal telling you the
number of the current terminal (result pts 0)(Pesudo
Terminal Screen) or tty 0
exit To close the shell

ctrl + d To close the shell

passwd To change the password

passwd To change the password of another user

ahmed Note: if you try to change password it required the old


password, but if you tring to change it from root shell it
will not be required
file /home To know if this is editable directory or executive(Connot
be edited)
cat /passwd To show all lines in this directory

head /etc/pass To show first 10 lines

wd
head -n 5 /etc/pass To show first 5 lines
-5
wd
tail /etc/pass To show lastest 10 lines

wd
tail -f /etc/pass To show every in same terminal after changing in
otherone
wd
tail -n 5 /etc/pass To show lastest 5 lines
-5
wd
Which passwd To know where it’s locate (result /etc/passwd)
whereis To know where it’s locate (result /etc/passwd)
passwd
su root To swish users

wc To show how many lines, litters, charactrers in this


directory
wc -l To show how many lines

wc -w To show how many words

wc -c To show how many bits

wc -m To show how many characters

history To show all commands that you wrote

history -c To clear the file that have the history (.bash_history)

!5 To execute the 5th command in history

!-5 To execute the 5th command from behind

cd To change current directory to home

cd /home/ah To change current directory to ahmed


Note: you can write directory that cannot exceed 4095
cd med byte
~ahmed
cd - To change directory to the last directory not previous one

cd . To go to current directory (You will need it for copung)

cd .. To go to previous directory

cd ../.. To go to previous directory twice

touch File1 Creating file it’s name is File1

touch ‘File 1’ For files that them name have a space

touch “File 1”
mkdir /home/a Creating a directory and it start to create from right of
pass (dir first -→ Desktop)
hmed
/Desktop
/dir
mkdir -p /Desktop (Positive) Creating a directory and it start to create from
left of pass (dir1 first → dir2 → dir 3)
/dir1
/dir2/”dir 3”
File1 To copy File1 from current directory to Desktop
cp /home/ahmed
/Desktop

cp -r dir1 To copy dir1 from current directory to Desktop


/home/ahme should make -r to copy directory
d only directory will be coped but content will not
/Desktop

~/Downloads
/home/ahme
d
/Desktop
cp -r dir1/* To copy dir1 from current directory to Desktop
/home/ahme should make -r to copy directory
d * for making the content of directory coped to Desktop
/Desktop

mv “Ahmed” To rename file or directory


”Noaman”
mv “Ahmed” To move file or directory from current directory to
another
/home/ahme
d
/Desktop
rm File1 To remove file in current directory

rm -r dir1 (Recurisve) To remove directory in current directory

rmdir dir1
rmdir -rf Dir1 (Recursive Force) To remove directory and every thing
inside
nautilus To open file explorer

(Globbing)
ls a* Show each file or directory starts with a

ls *a* Show each file or directory with a is in the middle

ls *a Show each file or directory ends with a

ls [abc]* Show each file or directory starts with a or b or c

ls [a-z]* Show each file or directory starts with a or b or c _____x


or y or z
ls F??? Show each file or directory starts with f and only there is
3 characters after it
ls [^a]* Show each file or directory starts with anything but a

echo $Z=1 or $ Z is variable now and here you print it’s value
z=Ahmed
echo “Today is Here you showing string + date
$(date)”
echo “1+2= Here you showing an arithmetic operation
$[1+2]”
(Help)
ls --help For showing what ls can do and information about it

man ls
-k | grap 1 “print” For showing ever command have a print word in it’s
man describtion
man -K “print” For showing ever command have a print word in it’s
describtion and describe all of them
apropos “print” For showing ever command have a print word in it’s
describtion
passwd For showing what ls can do and information about it
Info
passwd For showing what ls can do and information about it with color
binfo
cd /usr/share/do ls command for showing all files that related to system
c
https://docs.redhat. For help
com

yelp For help

(Redirect)
> /home/ahmed The result go to text.txt file
ls /home /Desktop/text.txt
2> The error go to text.txt file
ls mm /home/ahmed
/Desktop/text.txt
> /home/ahmed The result and the error go to text.txt file
ls /home /Desktop/text.txt
mm 2>
/home/ahmed
/Desktop/text.txt
>> The result is expanded in text.txt
ls /home/ahmed
/Desktop/text.txt
ls /home &> /home/ahmed The result of two go to text.txt
/Desktop/text.txt
/Music
less text.txt To view the content of the file (should press q to close)

more text.txt To view the content of the file (show percentage and
close by itself)
(Pip) |
ls |head -n Show list of content in current directory and filter | to get
first 4 lines
4
ls | tee text.txt | Content of current directory is stored in text.txt and the
wc -l terminal will show no.of lines
Cat text.txt | less Showing content of file but in less way (you can do this
with more too)
Vim To edit files

gedit
nano
Users
useradd -p 123 To create an user and give him a password
ahmed
adduser
-G Ahmed To make this user in a group called Ahmed
ahmed -c IT To but Gecos to this user
-s Departm To change shell
-u ent To give the new user an id

/etc/sbin
/nologin
1001
usermod -p 123 To change password
ahmed
-G Ahmed To make this user in a group called Ahmed
-c IT To but new Gecos to this user
-s Departm To change shell
-u ent To give to an existing user an id

-L /etc/sbin To lock user


-U /nologin To unlock user

2000
ahmed
ahmed

passwd 123 To change password


ahmed
id Show info about current user
Username:X:UID-GID:GECOS:PASS:SHELL
Id ahmed Show info about ahmed

Id ahmed -u Show only uid of ahmed (not all informations)

groupadd Ahmed To make new group called Ahmed


groupmod To change id of group
Ahmed -g 2000
groupmod -n old new To rename the group

userdel ahmed To remove an user

userdel -r ahmed To remove user and his files


groupdel ahmed To remove a group

chage -l ahmed To list aging information about user

Permossions (Simpolic)
chmod u+r , dir1 To give the file or directory permission to read
To prevent the file or directory from reading
u-r (Note : the ,to do more than 1 in one command)
u+w To give the file or directory permission to write
To prevent the file or directory from writing
u-w
u+x To give the file or directory permission to be executable
To prevent the file or directory from being executable
u-x
(Note : you can do this with g or o instead of u)
chmod a+rwx dir1 To make theme all have all permissions

chmod a=rx dir1 To make theme all have r + x not w


(Note : if one of them have permmisions before this
command the old permissions is deleted to override new
permissions)
ll -d dir1 To show directory permissions

chmod -R a=rwx (Recursive) To make the directory and it’s content have this
dir1 Permissions
Numeric
chmod 4 dir1 To give r

chmod 2 dir1 To give w

chmod 1 dir1 To give x

chmod 754 dir1 u = 1+2+4 = 7 = u+rwx


g = 1+4 = 5 = g+rw
o=4 = 4 = o+r
chmod -R 754 dir1 u = 1+2+4 = 7 = u+rwx
g = 1+4 = 5 = g+rw
o=4 = 4 = o+r
(Recursive) fot the content too
/root/Desktop/dir1 To change the owner of the file or directory
chown ahmed
/root/Desktop/dir1 To change the owner of the file or directory
Chown ahmed -R (Recursive)
/root/Desktop/dir1 To change the group of the file or directory
Chown :ahmed
/root/Desktop/dir1 To change the group of the file or directory
Chgrp ahmed
/root/Desktop/dir1 To change the group of the file or directory
chgrp -R (Recursive)
Set permission (group) : (set permission for users should be deleted not be created)
/root/Desktop/dir1 To give set permission to dir1 directory
chmod g+s
/root/Desktop/dir1 To make any file will be created by ahmed group
chgrp ahmed
Sticky bit (t)
/root/Desktop/dir1 It given for other so the directory can be written by the owner
chmod o+t and others but content cannot be edited but owner

Processes:
echo $$ To know the process id of the processes in the current
Shell
ps To show processes that running in current shell

ps aux To show all processes that running on the whole


-aux computer now
ps aux | less You can use pip
pidof /sbin/init splash To know pid of this process

ps -l To show parent id

ps -ef
ps aux | grep 22 To get the process by it’s number is 22

pstree To make Hierarchical Design

ps fax
Real Time process : (Task Manager for Linux)
top To show all information about the time consuming
through running processes
1 To show all cores

Press n 5 Show only 5 processes

Press P
To show processes acourding to CPU

Press M
To show processes acourding to Memory

Press W
To save

Press q
To quit

su root To install the monitor that views processes Visualy


apt install gnome-system-monitor
apt install gnome-system-monitor To run it
Controlling Jobs :
sleep 2000 The terminal will be sleep for 2000 seconds it means that
the terminal will not able to write commands for 2000 s
This process running in fg
Press ctrl + c To kill this job
Press ctrl + z To stop this job
jobs To show the running processes, it shows anumber of the
processes
fg %1 You use number of process 1 after % to make the stopped
process to run in background not fg

Sleep 2000 & The same command but the process will run in bg or
ctrl + z and bg %1 to make the process in bg
Press

not fg
kill 22743 To kill process by pid

pkill vim To kill process by it’s name

kill -9 22743 The process will be killed by signal 9

Priority between(-20 : 19)(if it low is good because the cpu will look at this process more than the others)
nice firefox Give the process 10 priority

nice -n 15 firefox Give the process 15 priority

renice -n -20 22743 To regive priority but should by pid

Services
systemctl To view all services and running users in the machine

systemctl --type service Here only services will be viewed

systemctl status ssh To view information about this service like (Enabled or
disabled) …etc
systemctl status ssh.service
systemctl status -l ssh To view more information
systemctl start ssh To start service if it is inactive

systemctl stop ssh To stop service if it active

systemctl enable ssh To enable service

systemctl disable ssh To disable service

systemctl Is-active ssh To know if the service active or not


systemctl Is-enable ssh To know if the service enable or not
systemctl restart ssh Here the service will be inactive then be active

systemctl reload ssh Here the service will reload configuration files without
restart
list- To know what sevicea is depend on ssd
systemctl dependencies ssh
list-units To view all services in list
systemctl --type service

IP:(Network)
ifconfig To view all network slots that exist on your machine

ifconfig Lo To view information about lo slot

ip add Show To view all network slots that exist on your machine
Powerful more than ifconfig
ip add Show Lo To get information about :o
ip link Show To get more informations about the network slot
ip -s link Show To get more info more than the previous command
addr add dev To change ip address
ip Lo
172.16.0.1/24 make it secondary ip
Ifconfig Lo To change ip address
172.16.0.1/24 make it overwrite on last ip it will be deleted when
restarting the machine
nmcli To view and control networks in this machine

nmcli con show To view the networks on your machine


nmcli con show eth0 To view information about the eth0 only
nm-connection- editor Program to manage and control the network
Nmtui (Network Manager Text User Interface) Program to edit
and manage the network
hostname To view name of machine
Set-hostname To change name of machine
hostnamectl Kali
hostnamectl View: name of machine
Distribution of linux
Name of vm
Name of vm company
Kernel name …etc
Routing
route -n To view routing table

ip route
ip route Show
Installing Pachages
RPM:
rpm -ivh package Installing package

rpm -qa (quert all) view all packages

rpm -qc To view (Configuration files)

rpm -qi To know information about package

rpm -e (eraise) Delete package

rpm -Uvh Update or install and update package

rpm -Fvh Only update package

arch To know which kind of the architecture that the processor


can supports
uname -a To view all info about the operating system

uname -m View machine name

uname -o View operating system name

Logs:
systemd-journald To ensure that service of journald is active
systemctl status
journalctl To view all actions that have been done from first day you
installed the os until now
journalctl -n To view last 10 lines in logs
journalctl -n 5 To view last 5 lines in logs

journalctl -f Like tail

journalctl -p err For showing all error logs according to priority(priority


between 0 and 7)
Journalctl -b For showing errors that happened when system boot

journalctl --since Yesterday – To view all actions from yesterday until now
until now
journalctl --since “2024-2-1” To view all actions between this dates
--until
“2024-2-7”
journalctl _PID=1 To view the actions of Process ID=1
journalctl _UID=0 To view all actions that had done by root (uid=0)
journalctl _SYSTEMD_ To view all actions that beloge to this service
UNIT=shd
last Print the users that login
lastlog Print the content of the last login log
timedatectl Showing every thing about time zone
List-timezones Showing all time zones
timedatectl
set-timezone To change time zone to cairo
timedatectl Africa/Cairo
timedatectl Set-ntp true To synchro the time according to ntp
du -hs (Disk Usage) to know the current directory usage
-s for get the total usage not usage for each file and
directory
df -h

Archiving:
tar -cvf Archive.tar To meke archive file and its content is file1, 2, 3
file1
file 2
file 3
tat -tvf Archive,tar To list the content of the archive file
tar -xvf Archive.tar To extract the archive file
tar -xvf Archive.tar To extract the archive file in another working directory
-c /Desktop
tar Archive.tar.gz Stronger than tar (gzip)
-cvzf
tar Archive.tar.bz2 Stronger than gzip (bzip2)
-cvjf
tar Archive.tar.xz Stronger than bzip2 (xzip)
-cvJf
gzip Text.txt.tar Another way to archive by gzip
bzip2 Text.txt.tat Another way to archive by bzip2
xz Text.txt.tar Another way to archive by xzip
gunzip Text.txt.tar. To unzip/extract the file but the zip file will be deleted
gzip
bunzip2 Text.txt,tar.
bzip2
unxz Text.txt,tar
.zx
unzx -c Text.txt,tar This way the zip file will not deleted while extracting it
.zx > dir1.zx
scp Copy files between many machine

rsync Synchronizing files many system securly

Accessing:
mount /dev/media /media To can access this directory from another way
umount /dev/media To can access current directory from another way
blkid To get uuid of the directory
umount uuid To make mount by uuid
lsof /media Tp know the opened file in /media
ln /dev/media (Hard Link) samr id
To make a new way to access and if the source is deleted
the chiled directory will not be deleted
/media
ln -s /dev/media (Symbolic Link) diffient id
Same but thechiled will be deleted if the source is
deleted
/media
Searching in File System:
whatis passwd To fined a file
whereis Passwd
which passwd
locate passwd You should updatedb before locate
find / -name passwd To search in whole system
find / -name “*.txt” To search in whole system
find / -user ahmed To search in whole system
lsmod View all modules

You might also like