Linux Summary

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

Linux commands

To open the terminal: ctrl + alt + T o Shift + ctrl + c : to copy a text inside a terminal Press the up_arrow key to see the previous commands man
To end a terminal session: ctrl + d or write : ‘exit’ o Shift + ctrl + c : to paste it (command name)  to see the details

It mean : username@machinename followed by the current working directory and a dollar. If the last
character of the prompt is a pound sign (“#”) rather than (“$”) this means either we are
logged in as the root user or we selected the administrative terminal

1 – Navigation :
The first directory in the file system is called the root directory. The root directory contains files and subdirectories, which contain more files and subdirectories and so
on.
● pwd – Print name of current working directory ● ls – List directory contents
$ ls [options] [files or directory]
ls (Name of directory/folder) to see the content directory ~ : home directory ls /  to
show the content of the root directory

ls ..  which means that you want to print one step back directory structure
● cd – Change directory ls ../.. which means that you want to print two steps back directory structure
cd 'directory name' / cd "directory name" The most common options are :
-a: which means show the hidden folders/directories -l : list
the content directory in long format -S : list the directories
cd /  we are in the root directory by their size -t : sort by modification time -h : in long format listing, display file
sizes in human-readable in bytes -r : display the results in reverse order -R : list
cd or cd ~  go to home directory subdirectories recursively -F : This option will append an indicator character to the end
of each listed name, ex: forward slash (/) if the name is a directory.
cd ..  it means that we want to go to the previous directory of the
NOTE : we combine btw the options by written : ls -lSh
current working directory

you can use the path in the command

ex :

cd home/lynx/test.txt
cat command

First.txt Second.txt
cat : concatenate files and print on the standard output Line 1 Line 4
$ cat [Options] [file1 file2 file3...] Line 2 Line 5

Line 3 Line 6

1-diplay the content of text file or multiple files 2- Redirection : it means capturing output from a file
command or program and sending it as an input to another file command or
Ex : cat First.txt program
Line 1
First option: if u want to transfer some output and redirect it to a file
Line 2 SYNTAX: cat output > file
Line 3 ex : cat > test.txt
Options : hmmmmm

it working

just keep writing

-Now let's see this move: cat > test.txt


line 1

line 2

line 3

-IF we want to append a text to the existing content of a file just write this command

cat >> file_name

ex : cat >> test.txt let's show the result after the appending :

Line 4 cat test.txt Line 1


Line 5 Line 2
Line 3
Line 6 Line 4
Line 5
Line 6

-To combine/transfer a copies of text files to another file


to exit the cat function u should use (ctrl + D)
ex : cat First.txt second.txt > output.txt
mkdir command

mkdir : make directories $ mkdir [Directory]


ex : cd Desktop
mkdir Cours if the file doesn't exist and u wanna create a
to create a multiple directories write this
ls directory inside it
command :
Cours u should use this option :
$ mkdir [directory]/{ , , }
-p : No error if existing, make parent directory as
You can also add more files to show needed (we use it to create directory structure)
-We can use a forward slash after the directory
them ex: cat First.txt Second.txt
to create a subdirectory inside it without going ex : pwd pwd
to the specific file then you will create it Home/Lynx/Desktop Home/Lynx/Desktop
(The file should be existing) mkdir -p a/b/c/d ls
ex : pwd ls N.b : firstly, the
Coursfile will
TD created .then THIS
-n : number all output lines
Home/Lynx/Desktop -b : number nonempty output a CONTENT WILL BE WRITTEN
mkdir TO THE FILE 'test.txt'
Cours/{Math,Eln,Elt}
. exmkdir
: cat -bCours/S1
First.txt lines ex : cat -b First.txt ls a ls Cours
ls Desktop b Eln Math Elt
1- Line 1 1- Line 1
Cours 2- ls a/b
ls Cours c
3- Line 2 2- Line 2
Math 4- ls a/b/c N.b : we see that the content we've written to the
d file is overwritten by the new content
5- Line 3 3- Line 3
ex : pwd
Home/Lynx/Desktop
-s : suppress repeated empty -E : add $ sign in the end of ls every
output lines. line. ex : cat -E Second.txt Cours
ex : cat -s Second.txt Line 4$
Line 4 $ N.B : as we see that we've just one directory
Line 5$ which named : Cours
Line 5
$ mkdir -p TD/Serie_1
Line 6 $ pwd
Line 6$ Home/Lynx/Desktop
ls
Cours TD
ls TD
Seire_1
rmdir command

rmdir : remove the directory or directories structures (the


directory must be empty of both files and subdirectories
otherwise you will get an error)

$ rmdir [-Options] [Directory_name]

Ex : pwd Ex : pwd
Home/Lynx/Desktop Home/Lynx/Desktop
mkdir Test1 Test2 mkdir -p a/b/c/d
rm : $ rm [-Options] [Directory_name]
ls ls
Test1 Test2 a Test1 rm options :
rmdir Test2 ls -R
ls a Test1
Test 1 .:
a
let's create a directories
./a:
structure
b
pwd ./a/b:
Home/Lynx/Desktop c
mkdir -p a/b/c/d ./a/b/c:
ls d Ex :
a Test1 ./a/b/c/d:
ls -R cd a/b
a Test1 cat > text_file.txt
.: keep going
a ./ rmdir -pv a/b/c/d
a: rmdir: removing directory, 'a/b/c/d'
b rmdir: removing directory, 'a/b/c'
rmdir: removing directory, 'a/b'
./a/b:
rmdir: failed to remove directory 'a/b':
c
Directory not empty
./a/b/c:
ls -R
d N.B :
.: Ex :
./a/b/c/d: we see that a/b doesn't
a Test1 rm -rv a
rmdir -p a/b/c/d removed cuz b contain a
.a: rmdir: removing directory, 'a/b/text_file.txt'
ls file
b rmdir: removing directory, 'a/b/'
Test1 so we'll use rm command
.b: rmdir: removing directory, 'a'
text_file.txt
cp command

cp : copy
$ cp [options] [name or location of your source directory] [destination/absolute path or relative path]
(absolute path : the entire path beginning from the root directory. Ex: /home/lynx/Documents
whereas relative path begins from the current working directory. Ex : Documents)

Ex 2: (copy a file to a directory)


Ex 1: pwd
pwd
home/lynx/Desktop
home/lynx/Desktop
ls ls
Serie1.txt Serie2.txt dir1 dir2 Serie1.txt Serie2.txt dir1 dir2
cp Serie1.txt Serie1_cp.txt cp Serie2.txt dir1
ls ls dir1
Serie1.txt Serie2.txt dir1 dir2 Serie1_cp.txt Serie2.txt (the file is copied in dir1)

Ex 3: (if u wanna copy the two files into dir1 but the file 'Serie2.txt' is already
Ex 5: pwd
existing there so this command will overwrite the content of 'Serie2.txt')
home/lynx/Desktop
pwd
ls
home/lynx/Desktop
dir3
ls
cp dir3 dir4 (it works if the dir3 was empty)
Serie1.txt Serie2.txt dir1 dir2 Serie1_cp.txt
ls
cp Serie1.txt Serie2.txt dir1
dir3 dir4
*To avoid the overwriting just add this flag(option) in your command : -i
which stands for interactive interface so will ask you before overwriting cp -vr dir3 dir4
pwd
(it works if the dir3 was not empty so the content of dir3 will copy to the
home/lynx/Desktop
dir 4 without the dir3 cuz dir4 doesn't exist )
ls
-r means the recursive copying
Serie1.txt Serie2.txt dir1 dir2 Serie1_cp.txt
-v means verbose (explain what is being done)
cp -i Serie1.txt Serie2.txt dir1
cp: overwrite 'dir1/Serie2.txt'? (Y :yes/N :no)
Ex : (copy a multiple file)
Ex 4: pwd pwd
home/lynx/Desktop/dir1 home/lynx/Desktop
ls ls
Serie1.txt Serie2.txt Serie1.txt Serie2.txt dir1 dir2 Serie1_cp.txt
cp ../Serie1_cp.txt . ('.' represents the current working directory cp Serie1.txt Serie2.txt dir2
(it's going to copy the file to the directory) ls dir2
Serie1.txt Serie2.txt (the files are copied in dir2)
mv command

mv : is used to move a source file to given destination / (Move/rename files and directories)
$ mv [options] [name or location of your source directory] [destination/absolute path or relative path]

Ex 1: (rename the files) Ex 2: (move a file to a directory)


pwd pwd mv options :
home/lynx/Desktop home/lynx/Desktop
ls ls
dir 1 script_1.txt dir 1 script_2.txt
mv script_1.txt script_2.txt mv script_2.txt dir1
ls ls dir1
dir 1 script_2.txt script_2.txt

Ex 3: (the file will overwritten) Ex 4: (move a dir to another dir)


pwd pwd
home/lynx/Desktop home/lynx/Desktop Ex :
ls ls
dir 1 script_2.txt ls dir1 dir2
dir1 mv dir1 dir2
script_2.txt ls -R dir2
mv script_2.txt dir1 dir1
ls .:
dir1 dir1
ls dir1 ./dir1:
script_2.txt script_2.txt

Sum :
mv file1 file2 // will over write file2 with file1 if the file exist otherwise will create a new file

mv -i file1 file2 // -i stands for interactive which will ask before overwriting file

mv dir1 dir2 //to move dir1 inside dir2 with her content along it if dir2 exist otherwise only content
Less command Touch command

less : is used to see the file contents from the starting/Read the file or search touch: The touch command is usually used to set or update the access, change,and
something inside it modify timestep of file or directory. However, if a filename argument is that of a non-
$ less [options] [file] existent file, an empty file (like .txt /.cpp or simple file) is created.
The most common keyboard commands used by less : $ touch [options] [file]
. Timestamp the time of create or update the file .

Ex : Ex : (To modify the timestap of a file)


pwd pwd
home/lynx/Desktop home/lynx/Desktop/dir1
ls ls -l dir1
dir1 dir2 -rw-rw-r 1 lynx lynx jan 29 10:00 file1
ls -l dir1 -rw-rw-r 1 lynx lynx jan 29 10:00 file2
-rw-rw-r 1 lynx lynx jan 29 10:00 file1 -rw-rw-r 1 lynx lynx jan 29 10:00 file3
-rw-rw-r 1 lynx lynx jan 29 10:00 file2 -rw-rw-r 1 lynx lynx jan 29 10:10 file4
-rw-rw-r 1 lynx lynx jan 29 10:00 file3 touch file1
touch file4 ls -l dir1
ls -l dir1 -rw-rw-r 1 lynx lynx jan 29 10:46 file1
-rw-rw-r 1 lynx lynx jan 29 10:00 file1 -rw-rw-r 1 lynx lynx jan 29 10:00 file2
-rw-rw-r 1 lynx lynx jan 29 10:00 file2 -rw-rw-r 1 lynx lynx jan 29 10:00 file3
-rw-rw-r 1 lynx lynx jan 29 10:00 file3 -rw-rw-r 1 lynx lynx jan 29 10:10 file4
-rw-rw-r 1 lynx lynx jan 29 10:10 file4

nano command

nano : is used for creating and editing text files directly from the command line. The nano interface :
$ nano or $ nano [options]
Features:
Easy-to-Use Interface: Nano provides a straightforward and beginner-friendly text-
based interface.
Syntax Highlighting: Nano supports syntax highlighting for various programming
languages, making code easier to read.
Search and Replace: Users can search for specific text strings and replace them within
the file.
Line Numbers: Line numbers are displayed for easy navigation.
Basic Usage:
Open a file: nano filename Save changes: Press Ctrl+O, then Enter.
Exit nano: Press Ctrl+X. Ctrl+K (cut), Ctrl+U (uncut/paste), and Ctrl+Shift+6 (copy).
sudo command
Ex 1: cd / Ex 2: sudo -s
cd etc [sudo] password for lynx :
mkdir new_file sudo : (super user do) Execute a command as another user root/machine_name/etc#
erreur : Permission denied $ sudo [command] we’re in super user mode
sudo mkdir new_file
[sudo] password for lynx :

top command

top : (Display Linux processes) /is used to view the CPU usage of all processes.for ex if u wanna know which process is taking
most of your CPU or which program is taking most of your computer memory
$ top

top  The name of the program.


14:59:20 The current time of day.
up 6:30  This is called uptime. It’s the amount of time
since the machine was last booted. In this ex the sys has been
up for 2h:35min
2 users  There are two users logged in.
Tasks  This summarizes the number of processes and
their various process states.
Cpu(s)  This row describes the character of the
activities that the CPU is performing.
Mem  This shows how physical RAM is being used.
Swap  This shows how swap space (virtual memory)
is being used.
0.7%us  0.7 percent of the CPU is being used for user
processes. This means processes outside the kernel.
1.0%sy 1.0 percent of the CPU is being used for system S : to change the refreshing of the data
(kernel) processes. k : kill any processes with their Process ID
0.0%ni  0.0 percent of the CPU is being used by “nice” i : filter out any Idle processes
(low-priority) processes
98.3%id  98.3 percent of the CPU is idle.
0.0%wa  0.0 percent of the CPU is waiting for I/O.
kill command

kill : is used to “kill” processes. This allows us to terminate programs that need killing (that is, some kind of pausing or termination). The command uses one or more
PID’s as its argument $ kill <PID of the program> or $ kill [Option] <PID of a process>
-The PID of a program is changing after the killing process
-To kill a process by force : $ kill -KILL <PID>

Ps : is used to display the process status .submission time, required CPU time , sleep : it will suspend the execution of shell. The maximum limit of sleep is
Process name (u can see the name of process and their ID) 18000 s,Unfortunately keep the terminal idle.
$ ps [Option]
$ Sleep [time in second]
PS options : -a  Print all terminal process
-e  Print all current process
-t  Print all processes of listed terminal
-u  Displays processes of listed user

echo command

echo : (Display a line of text) is a shell built-in that performs a very Ex : (echoing a text) Ex : (create a var and display it) Ex : (create a var and display it)
simple task. It prints its text arguments on standard output. echo ‘’Hello world’’ age=20 age=20 name=’’LYNX’’
$ echo [ Option ] [str,var…..] Hello world echo $age echo ‘’My name’s $name and I’ve $age
20 years old’’
-All var are valid for the session of the terminal when u’re close the echo ‘’I’ve $age years old’’ My name’s LYNX and I’ve 20 years old
terminal the var will be gone. I’ve 20 years old

-Use the dollar symbol to disp ur var

Arithmetic Expansion : Expression = arithmetic expression ’supports only integers’ + op [+ - * / % **]


It uses the following form: $((expression))
Ex : echo $(((5**2) * 3)) echo ‘’Five divided by two equals $((5/2))’’
75 Five divided by two equals 2
echo with $((5%2)) left over.
with 1 left over.

Escaping Characters : Adding the -e option to echo will enable interpretation of escape sequences. You may also place them inside $' '.

\a  alert (bell) \\  backslash \n  new line


\b  backspace \v  vertical tab \t  horizontal tab
File Permission

Ex 1: id id : (Display user identity) it means : Print user and group info for each specified Ex 2: id -u
uid=1000(lynx) gid=1000(lynx) USER, or (when USER omitted) for the current user. 1000
groups=1000(lynx),4(adm),24(cdrom), $ id or $ id [Option] <username>
27(sudo),30(dip),46(plugdev) Options : id -g
-g  Print only the effective group ID 1000
uid  user id -G  Print all group IDs
gid  group id -u  Print only the effective user ID id -G
-r  Print the real ID instead of the effective ID, with -ugG 1000 4 24 27 30 46
-n  Print a name instead of a number, for -ugG

Access rights to files and directories are defined in terms of read access, write
access, and execution access
EX : cd home/lynx/desktop
ls -l file.txt
-rw-rw-r-- 1 lynx lynx 49 Jan 29 11:04 file.txt

The first character is the file type: The nine characters are the file mode:
-  a regular file
d  a directory
l  a symbolic link
The read,write,execute permissions for
c  a character special file
the file's owner,the file's group owner,
b  a binary special file And everybody else.

1  File's number of hard links. See the sections "Symbolic Links" and "Hard
Links"
Lynx  The username of the file's owner.
Lynx  The name of the group that owns the file
49  The size of the file in bytes
Jan 29 11:04  Date and time of the file's last modification
File.txt  The file name
chmod command

chmod : (Change File Mode) only the file’s owner or the superuser can change the mode of a file or directory.
$ chmod [options] <permissions> <file >
chmod supports two distinct ways of specifying mode changes :

Octal number representation Symbolic representation


File Modes in Binary and Octal:
chmod Symbolic Notation :

Symbol Meaning
NOTE : u Short for ‘user’
By using three octal digits, we g Group owner
can set the file mode for the
o Others but means world
owner, group owner, and
World. a Short for “all”. It’s combination of “u”, “g”, and “o”.

$ ls -l file.txt $ ls -l file.txt
-rwx - -x -wx 1 lynx lynx 49 Jan 29 11:04 file.txt - rw- r-- -w- 1 lynx lynx 49 Jan 29 11:04 file.txt

EX 01: (remove all permissions to the file) EX 01: (add a permission to everyone)
$ chmod 000 file.txt $ chmod +x file.txt
$ ls -l file.txt $ ls -l file.txt
- - - - - - - - - - 1 lynx lynx 49 Jan 29 11:04 file.txt - r w x r - x - w x 1 lynx lynx 49 Jan 29 11:04 file.txt
EX 02: (set the read, write permission to user and others) EX 02: (revoke/grant permission specifically to the user, group or world )
$ chmod 606 file.txt $ chmod g+w file.txt
$ ls -l file.txt $ ls -l file.txt
- r w - - - - r w - 1 lynx lynx 49 Jan 29 11:04 file.txt - r w x r w x - w x 1 lynx lynx 49 Jan 29 11:04 file.txt
EX 03: (give the read, write and execute permission to the group) EX 03: (modify the permissions at the same time )
$ chmod 676 file.txt $ chmod g-rwx,o=rx file.txt
$ ls -l file.txt $ ls -l file.txt
- r w - r w e r w - 1 lynx lynx 49 Jan 29 11:04 file.txt - r w x - - - r x - 1 lynx lynx 49 Jan 29 11:04 file.txt

EX 04: (add execute permission to the user and other)


$ chmod 777 file.txt
$ ls -l file.txt
- r w e r w e r w e 1 lynx lynx 49 Jan 29 11:04 file.txt

You might also like