Basic command Flashcards
CTRL+D == exit
man ‘command’
command manual
@6 jeld darad
** bazi dastoorat dar chand jeld hastand
man 5 passwd => mige az jeld 5 baraam bekhon
env
print envirenment variable
USER = The name of the logged-in user
PATH = List of directories to search for commands, colon separated
EDITOR = Default editor
HISTFILE = Where bash should save its history (normally .bash_history)
HOSTNAME = System hostname
PS1 = The Prompt! Play with it
UID = The numeric user id of the logged-in user
HOME = The user’s home directory
PWD = The current working directory
SHELL = The name of the shell
$ = The process id (or PID of the running bash shell (or other) process
PPID = The process id of the process that started this process (that is, the id of the parent process)
? = The exit code of the last command
if we want to print or use this character without interpretation we have to ?
* ?[]’”$;&()|^<>
quote them or use with \
echo -e “hello\nhello\thello\v”
@-e chizi ke baad az \ miaad ro tafsir mikone
mhdadizadeh=’mohamad\ hadi\ dadizadeh’
echo $mhdadizadeh
define envirenment var and print by echo
**space to bayad ba \ scape kard
**dastoor aval bayad bedoon fasele bashe
**var moghe echo baayad $ dashte baashe
export mhdadizadeh=hadi
@dar hame level ha taarif mishe
what happen when enter a command?
first : check if is a builtin command then execute
second : @file ha ee ka dar $PATH adderess dadim be tartib check mikone va age onja ham chizi peyda nakard error mide
PATH=$PATH:/home/mhdadizadeh/bin
add path to $PATH
@linux current dir ro ham baraye ejraye barname ha check mikone? why?
@na , be dalayel amniati
which ‘command’
@age in command ro bezanam chi run mishe?
The which command searches through the directories that are defined in the $PATH environment variable for a given filename.
unset env_var
unset envirenment variable
-v : unset shell var if exist
-f : unset shell func if exist
$HISTFILE Vs history
$HISTFILE : baad az exit command ha tooch save mishan
history : yek shell builtin command ast va dastorat haman moghe zakhire mishavan
Ctrl+R = Backward Search
Ctrl+O = Run the command you found with Ctrl+R
!! = Run the last command
!10 = Run command number 10 of history
!text = search backward for text, and run the first found command
whereis ‘command’
@khodesh va mokhallafatesh koja hastan
**baazi dastoorat chand ja hastan ya maani haye motafaveti daaran
whatis ‘command’
print first line of discription
** age chand ta tafsir dasht hamasho mide
type ‘command’
@yek command ro midi behesh va mige chetor tafsir mishe
**mamolan path file ejraee ro mide
cd /home/mhdadizadeh
Absolute Paths
** / means root dir
cd dir1/dir2/dir3
Relative Paths
cd
cd ~
cd $HOME
home dir
cd ~[username]
goes to another user’s home directory.
cd ..
moves one directory up.
cd -
moves to your previous directory
uname -a
Gives you data about the system
-s = Print the kernel name. This is the default if no option is specified.
-n = Print the nodename or hostname.
-r = Print the release of the kernel.
-m = Print the machine’s hardware (CPU) name.
-o = Print the operating system name.
-a = hameche ba ham
ls -R
lists all the files in the subdirectories
ls -a
shows hidden files in addition to the visible ones
ls -lh
shows the file sizes in easily readable formats, such as MB, GB, and TB
ls -ltrh
l : long detail
t : sort by time
r : reverse sort
h : human readable
** -S : sort by time
cp file dir
copy file to dir
cp file1 file2
copy file to another file
@age nabood misaze
@age bood, overwrite
cp file1 file2 file3 dir
@chand ta file ro beriz to ye dir
cp -R dir1 dir2
@copye ye dir va cole mohtaviatesh to ye dir dige
cp -b file1 file2
-b : (–backup) will make backups of overwritten files
cp -p file1 file2
-p : will preserve the attributes.
** moshakhasat file aval ro ham nigiri
cat file1 file2 file3
@file haye voroodi ro migire concat mikone va namayesh mide
** cat -n file1 : shomare khat mizane
bzcat, xzcat, zcat, gzcat
These let you see the contents of compressed files without uncompressing them first
cat file1 file2 > dir/file3
@chand ta file ro yeki kon beriz to ye filee dige
less file
or
cat file | less
q = Exit
/foo = Searches for foo
n = Next (search)
N = Previous (search)
?foo = Search backward for foo
G = Go to end
nG = Go to line n
PageUp, PageDown, UpArrow, DownArrow = You guess!
od file
(Shows files in formats other than text). Normal behavior is OctalDump (showing in base 8)
-t will tell what format to print:
-t a for showing only named characters
-t c for showing escaped chars.
You can summarize the two above to -a and -c
mv file1 file_name
rename file to another name
mv file1 dir
move file to another place
mv Vs cp
@mv file ro as maghsad pak mikone
@cp file ro as maghsad pak nemikone
@har do dar har sorat ya file ro misazan ya overwrite mikonan
mkdir dir_name
@saakht dir
mkdir dir/dir/dir_name
@saakhte dir dar yek dir dige
**parent dir bayad mojood bashe
mkdir -p dir1/dir2/dir3
@saakhte hamzaman dir va parent hash
mkdir -m777 dir/dir/dir_name
@saakhte file hamraah bo dadan permissio
mkdir -v file1
prints a message for each created directory
switch: -v
@harf bezan begoo dare chi mishe
switch: -r
@baazgashti amal kon
rmdir dir
delete empty dir
rmdir -p dir1/dir2/dir3
@khodesh va hame ajdadesho paak kon
** agar khaali boodan
rm file1 file2 file3
remove files
rm -i
@ghabl as pak kardan soal dkon
rm -f
@bedoon soal har chi didi pak kon
rm -r
@be soorat baazgashti boro toye pooshe ha va hame file ha ro pak kon
touch file1
create new file or change time of creation of exist file
touch -t [timestamp] file1
specify time of file by timestamp
touch -d [unix_time] file1
specify time of file with unix base time
$ touch -t 200908121510.59 file1
$ touch -d 11am file2
$ touch -d “last fortnight” file3
$ touch -d “yesterday 6am” file4
$ touch -d “2 days ago 12:00” file5
$ touch -d “tomorrow 02:00” file6
$ touch -d “5 Nov” file3
specify creation time of file
touch -r refrens_file file1
specify file creation file by referencing to another file
split -n 2 file1
split file into pieces
-n = split file by number of pieces
-l = split file by number of line
-b = split file by number of bytes
-d = use numeric suffix start from 0
cut -f1 -d, file1
cut one or more columns from a file
-f = selected columns 1,3,4 or range 1-3
-d = delimiter (only one character)
** default delimiter is TAB
nl == cat -n
sort file1
-r : reverse sort
-n : sort numerically
-d : ignore blankes
uniq file1
** the input most be sorted file
-c = count of each item
-u = show only non-repeated item
-d = show only repeated item
find ./ -type f
_______
-type d
-type l
@to in directory hame file ha ya dir ha ya link ha ro peyda k
find ./ -iname “file_name”
@to in dir har chi ba in name hast to bedo
**iname case sensitive nist
find . -iname ‘*.png’ -type f -size -100k
finish with .png
with type file
with size less than 100k
find . -type f -mmin -30
mmin : Modified Minutes
cmin : Status Change Min
amin : Access Minutes
** -ls az file ha ls migire
-delete file ya dir haye peyda shode ro pak mikone
find ~ -type f -mmin -1 -exec wc -l ‘{}’ \;
@-exec aakhar find miad va ye command roye natayej ejra mikone
grep
for search by regex in file or stdin
grep ‘text’ file1
search text in file
- du command
head file1
print first 10 lines of file
-n : count of line you want to display
-c : count of byte you want to display
-q or –quiet : will not print headers specifying the file name.
head -n 5 file1
print first 5 line of file
tail file1
print last 10 line of file
-n : count of line you want to display
-c : count of byte you want to display
-q or –quiet : will not print headers specifying the file name.
tail -n 5 file1
print last 5 line of file
tail -f file1
follow end of the file and if new data added then display
cat file | tr ‘123’ ‘۱۲۳’
or
echo 123 | tr ‘123’ ‘abc’
@ mitoone yek majmooee character ro ba ye mahmooee dige jaa be jaa kone
** pure filter ast va input nmigirad
sed s/hadi/mohamad/g file
sed is stream editor
Sed is a great tool for replacing text with using regular expressions
replace hadi with mohamad all over the file
wc file1
word count
-l = line
-w = word
-c = bytes
wc -l file | cat file - file
echo “bar” |cat file - file
the output of previos step replace with -
tell me three hashing function ?
@yek input ba size delkhah migire yek outup uniqe ba size yeksan mide
md5sum
sha256sum
sha512sum
** mishe chand ta file dad va hash hamashono zire ham did
Wildcards: *
means any string
Wildcards: ?
means any single character
Wildcards: [ABC]
matches A, B, or C
Wildcards: [a-k]
matches a, b, c, …, k (both lower-case and upper-case)
Wildcards: [0-9a-z]
matches all digits and numbers
Wildcards: [!x]
means NOT X
- diff command
gzip file1
gunzip file1
bzip2 file1
bunzip2 file1
xz file1
unxz file1
xz file1 -k
-k = keep the original file when create compress file
zip [file_name.zip] file1 file2 …
unzip file.zip
tar -cvf file.tar [file or dir] [file or dir] [file or dir]
@archive kardan file ha dar file.tar
-c : create
-f : file
-v : verbus
tar xf file.tar–directory ~/dir
extract tar file to differant dir
-x : extract
-f : file
tar -xf file.tar
extract tar file
-x : extract
-f : file
tar xfz 2file.tar.gz
** first decompres from zip then extract from tar
tar tf file.tar
-f : file name
-t : lists the content of a file
tar uf file.tar file3
-u : archives and adds to an existing archive file.
tar
x : Extract files
f : Tar archive name
–directory : Set directory name to extract files
-C : Set dir name to extract files
-z : Work on .tar.gz (gzip) file format
-j : Work on .tar.bz2 (bzip2) file format
-J (capital J) : Work on .tar.xz (xz) file format
-v : Verbose output i.e. show progress on screen
- chmod command
- chown command
touch -r [refrens file] file1
specify file creation file by refensing to another file
file file_name
@formate file ro mide
file -i filename
@mime type ro mide
dd if=file1 of=file2 count=2 bs=4096
@block size ro migire va be tedad count as file1 mirize too file2
dd if=file | gzip > file.dd.gzip
copy and zip the file
@linux current dir ro ham baraye ejraye barname ha check mikone? why?
@na , be dalayel amniati
tell me number of the term bellow:
stdin?
stdout?
stderr?
stdin : 0
stdout : 1
stderr : 2
ls > file
Redirect STDOUT to a file; Overwrite if exists
tree»_space; file
Redirect STDOUT to a file; Append if exists
ls undefine.txt file.txt»_space;stdout.txt 2»stderr.txt
@it’s clear?
command 2> file
Redirect STDERR to a file; Overwrite if exists
command 2» file
Redirect STDERR to a file; Append if exists
command &> file
Redirect both STDOUT and STDERR; Overwrite if exists
command &» file
Redirect both STDOUT and STDERR; Append if exists
tr ‘۱۲۳’ ‘123’ < file.txt
@file hadi be onvan stdin be tr dade mishe
command >log.txt 2>&1
@stderr ro ham beriz hamon ja ke stdout ro rikhti
&0 : hamon ja ke stdin
&1 : hamon ja ke stdout
&2 : hamon ja ke stderr
ls 2>&1 >file1
@ stderr ro beriz hamon ja ke stdout ro mirizi , hala std out ro beriz to file1 , yanii hamashon to file1
ls j* x* >file1 2>/dev/null
stdout to file1
stderr ro beriz door
** /dev/null == aashghali
cat «_space;END > file
create and write file at the same time
command1 | command2
stdout of command1 == stdin of command2
question?
bishtarin distribution linux be tartib tedad (ba pipe)
echo hadi | xargs -I variable cat variable
@roo khorooji dastor ghabl ye command run mikone
** -I : variable misaze
echo text | tee -a file
append text to file also write in stdout
**without -a , text will be overwriten
[process or program] &
start and send the process to background
when process is running:
Ctrl+c ?
Ctrl+z ?
Ctrl + c : break
Ctrl + z : suspend
jobs
list all the jobs
-l : lists process IDs along with their information.
-n : lists jobs whose statuses have changed since the last notification.
-p : lists process IDs only.
bg %[process_number]
send a process to background
fg %[process_number]
send the process to foreground
jobs -l
list all the jobs + process id
nohup ping google.com
The nohup command lets you run your commands even after you close the terminal or logout. By default it writes its output to nohup.out
ping google.com &>~/logs &
send output and error to logs and send process to background
ping [option] [hostname_or_IP_address]
send ICMP ECHO_REQUEST to network hosts
kill -SIGNAL_ID_OR_NAME process_id
signal id and name:
1 = HUP = Informing the process that its controlling terminal (like an ssh connection) is terminated
15 = TERM = normal termination request
9 = KILL = forcefully kills the process
SIGTERM requests a program to stop running and gives it some time to save all of its progress. The system will use this by default if you don’t specify the signal when entering the kill command.
SIGKILL forces programs to stop, and you will lose unsaved progress.
Remember the nohup command ? :) It means “ do not respond to the hup signal “.
killall process_name
This command Will send the given signal (or by default 15) to all processes with the given name
pkill *ping
Will send the given signal (or 15) to all the processes with a specific pattern in their name
ps -ef
show all the process on a system
-e == -A : all process
-f == full format
ps -u
show all the process on the system
-a = -e = all
-u = user
ps -aux Vs ps -ef
??? => ps -af –sort +comm,-sid
sort process by key
pgrep ‘text’
pgrep ‘text’ == ps -ef| grep ‘text’
top
-display Linux processes
simple monitoring of the system
You can see the processes, system load, uptime, CPU status, memory, … and do some stuff.
The top command can also help you identify and terminate a process that may use too many system resources.
h = help
q = quit
M = sort based on memory usage”
c = show full commands
k = kill after asking pid and signal
free
The free command will show you info about the system memory
-m = for megabytes
-g = for gigabytes or even
-b = for bytes
-h = for human readable
uptime
command shows the time, systems uptime (how long the system has been running), how many users are logged in, and the load average of 1, 5 & 15 minutes
Although it’s one of the most important KPIs of the system status, some of the experienced Linux admins do not know what the load average means. The load average shows how many processes are in the to be run queue. If this number is higher than the number of your CPU cores, you are in a bad situation. If it’s close to the number of your cores constantly, it’s kind of dangerous, and if it’s less than 1/10th of your core numbers, your system is kind of idle. Do you remember how to check the number of your cores? Its in /proc/cpuinfo or nproc.
watch ‘command’
It lets you run and check the output of a command in specific time intervals (default is 2 seconds).
-n =To specify the interval in seconds
-b = Beep if the command has a non-zero exit
-d = Shows the difference between runs
sudo ‘command’
lets you perform tasks that require administrative or root permissions.
tac file1
displays content in reverse order
locate file_name
locate is a Unix utility which serves to find files on filesystems. It searches through a prebuilt local database of all files on the filesystem generated by the updatedb command.
Note it is essential to update the database as recent files saved over a period of fewer than 24 hours are not updated into the database by default and the database is updated once within 24 hours span.
** age donbal file ha ee hasti ke kamter az 24 saat ghabl dorost shoden. ‘updatedb’ bezan
df -h
report file system space usage
-h : human readable
du -sh dir
estimate file space usage
-s : totla
-h :human readable (GB)
-m : size in MB
-l : size in KB
diff -c file1 file2
compare files line by line
-c : displays the difference between two files in a context form.
-u : displays the output without redundant information.
-i : makes the diff command case insensitive.
tar tf file.tar
-f : file name
-t : lists the content of a file
ls -l file1:
-rw-r–r–. 1 mhdadizadeh mhdadizadeh 0 Jan 4 16:00 file1
1 ( - / d ) : file or dir
2 ( rwx ) : owner’s permissions
3 ( rwx ) : group’s permissions
4 ( rwx ) : others’ permissions
1 : the number of hard links. A hard link is an additional name for an existing file
mhdadizadeh mhdadizadeh : the owner and group owner of the file.
0 : the size of the file in bytes
Jan 4 16:00 : the last modification date
file1 : the name of the file/folder
tell me number of each permition :
r (read) :
w (write) :
x (execute) :
tell me permition of this number:
3 :
5 :
6 :
7 :
r (read) : 4
w (write) : 2
x (execute) : 1
3 : execute - write
5 : read - execute
6 : read - write
7 : read - write - execute
** owner - group - other
** If you don’t want to give any permission to a user, enter 0 into the corresponding spot.
chmod 777 [file or dir]
@dadan hame permissions ha be hame user ha
chown user_1 file1
The chown command lets you change the ownership of a file, directory, or symbolic link to a specified username.
**chown [option] owner[:group] file(s)
? : har file yek owner va yek group darad?
? : mishe owner va group kamelan az ham part bashan?
&&& cut -d: -f1 /etc/passwd
username of the system user
&&& wget [option] [url]
The non-interactive network downloader
hostname == uname -n
Run the hostname command to know the system’s hostname
&&& useradd
create a new user or update default new user information
alias rm=’echo “boro in daam bar morghi degar neh”’
unalias [rm]
alias allows you to create a shortcut with the same functionality as a command
&&& su [options] [username [argument]]
The switch user or su command allows you to run a program as a different user
-p or –preserve-environment keeps the same shell environment, consisting HOME, SHELL, USER, and LOGNAME.
-s or –shell lets you specify a different shell environment to run.
-l or –login runs a login script to switch to a different username. Executing it requires you to enter the user’s password.
ps
ps -u mhdadizadeh
@ye snapshot as process haye feeli mide
process ID (PID)
type of terminal (TTY)
running time (TIME)
command that launches the process (CMD)
switches:
-T : displays all processes associated with the current shell session.
-u username : lists processes associated with a specific user.
-A or -e shows all the running processes.