What is PS1 in Linux?

 


Last time, I have written a short article on Linux environment variables. In that article, we have also seen a types of environment variable, the purpose of PATH variable and so on. But, in Linux there is also one very important variable that we are going to discuss in this article. It's a PS1 variable.


What is PS1 variable?


You can  look at PS1 variable as a bash prompt configuration variable. It's a basically variable that defines the way your bash prompt is going to look like. So, every time you launch your terminal, you will see something like this.




What you see on this picture above, is the way my terminal looks like when I launch it. Now, as you can see, there is: 


# amar@ubuntustation:~$


Here's a short brake up of the text above. In the first you can see my name. 


- amar - my username

@ - separator between username and hostname

ubuntustation - hostaname (machine name)

~ - user home directory

$ - designates regular user

# - designates super user



You can change your hostname via the hostname command in terminal.


# man hostname 




The look of the prompt is stored within the PS1 variable. The system settings of the prompt (for all users on the system) are stored in /etc/bashrc file. 


Once you run echo PS1, you will get different information. By default, PS1 will list the following information. To see it, run the following command.


# sudo vim /home/amar/.bashrc 




By default, PS1 lists the following information when you open up your terminal.


# PS1="[\u@\h\W]\$" 


(u) - username 

(h) - hostname

(\W) - the base of the name of current working directory

(\$) - regular user

(\#) - super user


Note that the prompt can be changed as well and you need to modify it's file in order to add time, current date, uptime or any other desired information you want to be shown in your prompt. But I suggest you to not change these information unless you need so, it's a better to leave it the way it is. But if you wish to change the prompt, feel free to visit the article bellow that will guide you into that process. Till next time.


How to change your prompt?

[1] https://www.cyberciti.biz/tips/howto-linux-unix-bash-shell-setup-prompt.html


Share:

What are environment variables in Linux?

 


Environment variables are set of dynamic named values, stored within the system that are used by the apps launched in shells. In this article, you can read more on environment variables, how to list them and how to read them. 


In simple word, an environment variable is a variable with name and associated value. Each environment variable are written with capital letters, such as HOME, BASH and etc. 


We can list all the variables within shell by typing the following command:


# env 




On the image above are listed all the variables within shell. Besides the shell, there are global variables as well. 


Some of the most common used global variable are as follow HOME, PWD, SHELL, USER, TERM, PATH and etc. 


The HOME variable will display the user home directory. TO access it, we simply type in the following command.


# echo $HOME




The SHELL variable will list the name of the shell launched when you open up the terminal window. It will be bash because it is currently common used shell in most Linux distribution. To view the shell variable, simply type in the following command.


# echo $SHELL 




The USER variable will display the current logged user on a Linux machine. To view it, type in the following.


# echo $USER




The most important variable in Linux is the PATH. This variable list directories that the shell will search through to find command executables. To display it, type in the following command.


# echo $PATH




When you run a command, it is the PATH environment variable which is being used to determine where to look for a command. One common location is the /bin directory. It's a directory which contain some of the most commonly used commands such as pwd, ls, cp, mkdir, touch, dd, df, du, tail, less, more, and etc. 


Therefore the PATH variable is very useful because it allows the command to be executed without knowing or specifying the full path to the command. 


Each directory within the PATH variable is separated by a (:) semicolon sign. If you wish to add your /home directory into a PATH, you must type in /home/amar: and the way you can do that is being shown in the following command.


# PATH = /home/amar/:$PATH

# export PATH=/home/amar/:$PATH




As you can see, my /home directory is now available inside the PATH variable. Other words, now I can execute any application from within the /home directory simply by typing the following command ./name_of_file.


In the following sample, I have created a very simple bash script that will print out the present working directory followed by a quick note: 'Your present working directory is?'. Check out the sample script. 




Once you type in your script, you must run the following command to make the script executable.


# chmod +x  myfile.sh 


Once you do that, you can run the script by typing the following command.


# ./myfile.sh




As you can see, I have run this script within my /home directory which I have added into PATH variable. This way, I don't have to type in the full path to the .myfile.sh script in order to run it, I have simply type in ./myfile.sh and that's it. 


That was on environment variables for this time. Please make sure to read other online resources and websites on the environment variables that are listed bellow. Till next time. 


[1] Environment variables in Linux and how to set them?

https://www.howtogeek.com/668503/how-to-set-environment-variables-in-bash-on-linux/

Share:

10 sed command examples in Linux

 



The sed command is a very useful and powerful command in Linux. Since most of us have each own way of learning, we don't pay to much attention to this command. In this article, check out following 10 examples on using sed command. 


What is sed command?


The sed command is a stream editor for filtering and tranformating text. With sed command, you can:


- select text

- substitute text

- add lines to text

- delete lines

modify the file and much more



1. The following command shall list the file stream.txt without options.


# sed '' stream.txt 




2. The following command uses -p flag to print the current pattern space.


# sed 'p' stream.txt 




Pay close attention that the p flag in above example, has doubled the text line in file stream.txt


3. In order to remove a single character on the fly using sed command, we can type the following command.


# sed 's/a//' stream.txt 




In the above example we have:


- s replace swith

- a character to remove 

- // remove single space 


4. In order to make a permanent changes to a file, we can use the following command:


# sed -i  's/a//' stream.txt 




In above sample -i flag is used to make a permanent changes to a file. 


5. In order to replace a character inside a file, we can type the following command.


# sed  's/sample/example/' stream.txt




In the above example, sed command has replaced 'sample' word with 'example'. 


6. In order to make a globally replacement, we can type the following command.


# sed -i  's/sample/example/g'  stream.txt




The -g flag in above example is used to make a globally changes into a file. 


7. In the following example, the 'a' will be replaced with 'the'. Here's the command.


# sed  's/a/the/'  stream.txt




8. Replace the character on a start of the line. Here's the command.


# sed  's/^this/This/'  stream.txt




The ^ sign tells the sad command in above example, to replace 'this' with 'This' on the beginning of the line.


9. Replace the character at the end of a line. Here's the command.




In the above example, sed command uses the dollar $ sign, to replace the 'file' word at the end of a file into '1'. 


10. List specific number of lines in a file. Here's the command.


# sed   -n   '1,6p'  stream.txt




As we can see in above example, the command has listed using -n flag, range of lines from 1 to 6. 



Conclusion


This was 10 useful examples on using the sed command. For further reading, use the man page of sed command as well as other online resources that are detailing the work with this powerful command in more detail with more examples. Till the next time. 

Share:

Osnove upotrebe vim editora

 



Vim editor je jedan od najčešćih editora koji se koristi u Linux-u a osim toga, ovaj editor je bitan za Linux LPIC-1 jer se na istom testu nalaze pitanja iz upotrebe Vim editora. Ovaj članak je kao i prethodni, dio mog aktuelnog Udemy Linux LPIC-1 Certification Exams 101-102. U ovom članku, pogledajte nekoliko osnovnih primjera upotrebe Vim editora.


Šta je vim editor?


Vim editor je text editor koji se koristi za čitanje i uređivanje datoteka na Linux-u. Dostupan je na svim Linux distribucijama i trenutno je jedan od najčešće upotrebljavanih text editora. Osim vim-a, tu su elvis, nano, Emacs i drugi. No, vim je daleko napredniji komandama razne namjene. 


Uglavnom, vim editor ima nekoliko modova, ali su  dva moda najčešće korištena. Prvi mod je komandni mod u koji vim otvara svaku datoteku te omogučava izvršavanje komandi kao što je kopiranja, brisanje, pomjeranje kursora sa jedne pozicije na drugu itd. 


Drugi mod je insert mod u kojem korisnik unosi tekst unutar datoteke itd.  Za pristup insert modu, dovoljno je pritisnuti malo i na tastaturi a za ulazak u komandni mod, koristimo taster Esc. Evo nekoliko primjera sa Vim editorom.


1. Kako bismo kreirali datoteku upotrebom vim editora, koristimo sljedeću komandu.


# vim test.txt 


Ova komanda će kreirati datoteku test.txt gdje će po defaultu datoteka biti unutar komandnog moda. 


2. Da bismo ušli u insert mode, pritisnemo taster i gdje će u dnu pisati --INSTER--. 





3. Da bismo napustili instert mode te se vratili u komandni mod, dovoljno je pritisnuti Esc taster. 


4. Da bismo spasili datoteku test.txt, možemo to učiniti na nekoliko načina: 


- ZZ komanda će spasiti datoteku i vratiti nas natrag u terminal

- :w komanda će spasiti datoteku ali nas neće vratiti u terminal

- :wq spašava datoteku i napušta vim

- :q! - napusti vim


5. Ukoliko strelice ne rade na tastaturi, moguće je pomjerati kursor u komandnom modu upotrebom sljedećih tastera:


- h pomjeri kursor lijevo 

- l pomjeri kursor desno

- j pomjeri kursor dole

- k pomjeri kursor gore


6. Da bismo obrisali liniju na kojoj se trenutno nalazi kursor, pritisnemo dvaput dd




Obratite pažnju na malo d, na gornjoj slici. Svaki put kada pritisnemo dd, briše se linija na kojoj se trenutno kursor nalazi. 


7. Da bismo kopirali tekst na trenutnoj liniji gdje se kursor nalazi, potrebno je dvaput pritisnuti taster yy




Jednom pošto pritisnete taster yy, linija će biti kopirana. 


8. Da bismo zalijepili trenutno kopiran tekst, koristimo taster p.




Pritiskom tastera p, gornja linija je zalijepljena više puta na novu liniju što možete vidjeti na gornjoj slici. 


9. Ukoliko želite obrisati riječ unutar linije, potrebno je pritisnuti taster kombinaciju dw - delete word. 


10. Da bismo kopirali riječ na liniji gdje se trenutno nalazi kursor, koristimo komandu yw


11. Ukoliko želimo postaviti kursor na željenu rijeć unutar trenutne linije ili teksta datoteke, potrebno je unjeti kosu crtu / zatim riječ koja nas zanima. 






Kao što vidite, prilikom unosa riječ, u tekstu je ona pronađena i markirana. Tako važi za bilo koju drugu riječ koja vas zanima. 


12. Zamijeni riječ ili slovo unutar trenutne linije




Vim omogučava zamjenu riječi ili jednog slova (karaktera) untuar linije gdje se nalazi kursor ili druge linije u datoteci. Za zamjenu riječi ili slova, potrebno je pritisnuti taster R koji vas ubaca u replace mode. Jednom kada obavite zamjenu riječi, pritiskom na Esc taster se vraćate u komandni mod.  


13. Ukoliko ste greškom obrisali liniju i želite ju vratiti natrag, pritiskom na taster u ili undo, linija će biti vraćena. Pritiskom  kombinacije Ctrl + r možete poništiti undo operaciju. 


14. Za brisanje jednog karaktera unutar linije ili druge linije datoteke, koristi se taster x.


15. Za brisanje trenutne linije, koristi se taster d koji jednim pritiskom briše jednu liniju dok dd briše dvije linije unutar datoteke. 


16. Kako bismo izvršili komandu unutar vim editora, u komandnom modu je potrebno unjeti sljedeću komandu:


#:!pwd | ova komanda će prikazati trenutni radni direktorij




17. Ukoliko želite pomjeriti kursor na kraj linije, dovoljno je pritisnuti veliko slovo A. 

18. Ukoliko želimo obrisati tri linije unutar datoteke, unosimo sljedeću komandu u komandnom modu.

# d3d 

19. Za kopiranje tri linije unutar datoteke, u komandnom modu unosimo sljedeću komandu.

# y3y 

20. Za kopiranje tri riječi unutar linije, u komandnom modu unosimo ovu komandu.

# y3w


Ovo bi bilo nekoliko osnovnih primjera i komandi u radu sa vim editorom. Ukoliko ste primijetili da sam negdje prilikom pisanja iznjeo pogrešnu tvrdnju ili naveo pogrešnu sintaksu komande, slobodno napišite u komentaru ispod a ja ću ukloniti i ispraviti grešku. 

Nadam se svakako da je ovaj članak bio koristan, a mi se svakako čitamo i naredni put. 
Share:

Nekoliko primjera upotrebe tail komande

 



Tail komanda u Linux-u je izuzetno korisna. Omogučava prikazivanje posljednjih 10 linija unutar datoteke ali korisnik može navesti broj linija koje želi prikazati. Ovaj članak donosi nekoliko primjera upotrebe tail komande. 


1. Izlistaj datoteku bez ikakvih argumenata


U ovom primjeru, tail komanda će prikazati sadržaj datoteke output.txt bez ikakve opcije. Za prikazivanje komandi, bit će navedena rešetka # koja označava bash prompt. 


# tail output.txt 


Ova komanda će po defaultu prikazati deset zadnjih linija ove datoteke. 


2. Prikaži pet zadnjih linija datoteke


Kako sam rekao u samom uvodu, komanda tail omogučava korisniku da navede broj linija koje želi prikazati iz datoteke. U ovom primjeru, tail komanda će prikazati pet zadnjih linija datoteke output.


# tail -n 5 output.txt 


-n unutar gornje komande označava broj linija.


3. Prikazi više datoteka odjednom 


U ovom primjeru, komanda tail će prikazati sadržaj datoteke text1 i text2. Komanda izgleda ovako.


# tail -n 3 text1 text2


Jednom kada se izvrši ova komanda, iz datoteka text1 i text2 bit će prikazane 3 zadnje linije, od dna ka vrhu, što izgleda ovako.


==> text1 <==

zsh

ksh

csh


# ==> text2 <==

cp

ls

tail


4. Prikaži sadržaj datoteke dmesg upotrebom tail komande


U ovom primjeru, tail komanda će prikazati deset zadnjih linija dmesg komande.


# dmesg | tail 


Komanda dmesg se koristi za prikazivanje Linux kernel bafera, grešaka i uređaja koji se nisu pravilno pokrenuli tijekom butanja Linux distribucije itd. 


5. Upotreba tail komande sa | pipe-om


U ovom primjeru, komanda tail će izlistati pet datoteka unutar /home direktorija. Komanda je sljedeća:


# ls -tl | tail -5 




6. Prikaži procese upotrebom tail komande


U ovom primjeru, tail komanda će prikazati posljednjih pet procesa koji su trenutno aktivni.Komanda je sljedeća:


# ps aux | tail -5




Kratko objašnjenje: ps komanda se koristi za prikazivanje procesa koji su trenutno aktivni. Opcija a izlistava sve procese, opcija u izlistava korisnički orjentisan output te opcija x izlistava sve procese pa i one koji trenutno nisu aktivni u TTY terminalu. 


7. Prikaži ažuriranje datoteke u realnom vremenu


U ovom primjeru, komanda tail koristi opciju -f koja joj govori da prikazuje datoteku u realnom vremenu kako se ona ažurira. Komanda izgleda ovako.


# tail -f syslog.1 




Ovo bi bilo nekoliko korisnih primjera upotrebe tail komande. Kao i prethodni članci, i ovaj članak je dio mog aktuelnog Udemy Linux LPIC-1 Certification Course Exams 101-102. Ukoliko Vam se članak dopao a iz njega ste nešto naučili, onda podijelite dalje. Naredni put, čitamo nekoliko primjera upotrebe head komande. Do čitanja.

Share:

GNU/Linux bilješke 2 - dio

 


Ovo je drugi dio mojih GNU/Linux bilješki koje prate moj aktuelni Udemy Linux LPIC-1 Certification Course Exam 101-102 kurs. Ove bilješke pokrivaju Standard Input/Output gdje sam imao priliku raditi sa manipulacijom datoteka koristeći komande tail i head. 


1. The command read is used to read from a file descriptor. Here's a quick sample.

# read ME
# echo 'This is a ME file' > ME
# echo $ME

In the above sample, ME file has been created. Afterwards, using echo command, a sample text was written into a ME file. Using echo command, we can display the ME file content in bash prompt. 


2. In the following sample, the content of a file output.txt is passed to previously created ME file.

# read ME < output.txt
# echo $ME
# This is a newly created file

Note: The read command can only read the first line of a file. 


3. We can pass a content of directory into a file using the following command.

# ls /etc > output.txt 

This command will display the content of /etc directory and save it into a output.txt file. 


4. In order to send standard output error into a file, instead of screen, we can use the following command.

# ls sample.txt 2> error

This command try to list the content of a file sample.txt. Since this file does not exist, the error message: 'No such file or directory' is going to be stored into a error file. 


5. The tail command outputs the last 10 lines of file. In order to monitore a file in realtime as it is being updated, we can use the following command.

# tail -f syslog.1 


As each new log is added to a log file, tail updates it and display into terminal window. The -f option in tail command it telling to output appended data as the file grows. 


6. To display last 15 line of a file, we can use the following command.


# tail -n 15 output.txt

Since we recently saved the content of a /etc directory into output.txt file, it will be robust to read it completely on bash prompt, therefore we tell the tail command to display only 15 last line of a file.


7. In order to append a content into a file using tail command, we can use double redirection signs >>. Here's the quick sample.


# tail -f error
# echo 'Testing tail append mode' >> error

Every time we heat the enter key on keyboard, the text from echo command shall be send into a error file, adding new lines of text. 


8. The head command display the first 10 lines of a file, while tail command displays the 10 last lines of a file.


9. In order to display the first 5 lines of a file using the head command, we can use the following command.

# head -n 5 output


10. To display the last 5 lines of a output file, we use the following command.

# tail -n 5 output


Conclusion


This was my second part of ongoing Udemy Linux LPIC-1 course which I'm enrolled in. If you like my article, make sure to share it on to social network, leave a comment down bellow and I see you in third part, so soon. 
Share:

GNU/Linux bilješke 1 - dio


 

S obzirom da sam već uveliko uključen u svoj aktuelni Udemy Linux kurs, po imenu 'The Complete Linux LPIC-1 Certification Course Exams 101-102, prenosim u ovom svom članku prvi dio bilješki sa konkretnim primjerima. Kurs je zaista masivan a prvi dio bilješki uglavnom pokriva osnovne Linux komande i njihovu upotrebu. Ove bilješke će biti prenesene na Engleskom jeziku jer je sam kurs tako koncipiran. 


1. There are several shell in Linux such as bash, bsh, csh, ksh, zsh; most common one in use is the BASH (Bourne Again Shell) or bash.


2. The pwd command stands for present working directory. Once typed in bash prompt, it will display your current working directory. 


3. Second most important command is the ls command. This command is used for listing the content of directory. Once used with -l option, it will list the content in detail with files permissions. The (-) dash sign designates the file, where (d) sign designates the directory. Once listed, you will see bunch of characters such as -rwx-. These are file permissions which tell you, who can read, write and execute the file. 


4. You can list the content of directory with the following sample: 

# ls -l data - list the content of directory data with files permissions

# ls data - list the content od data directory without options


5. In order to navigate trough directories, we use cd command. Once we type cd .. we can jump straight to the parent directory. 

# cd /amar/data - enter data directory in /amar directory

# cd .. - return from data directory in /amar directory


6. Previously typed commands can be shown by pressing up and down arrow key. To see all the commands previously typed, we can use history command. The history command can store up to 1000 entries. To clear the history file, we can use 

# history -c 


7. To see the size or maximum number of lines that history file can store, we can use the following command: 

# echo $HISTSIZE | HISTSIZE is environment variable


8. All the commands typed in bash prompt resides inside user /home directory and that file is known as .bash_history.


9. In order to type multiple commands on a bash prompt, we can use semicolon (;) sign. The following command will create a directory test and rename it into test1. 

# mkdir test; mv test test1


10. Second way is using the && signs. It works the way that it waits for the first command to be executed successfully and than it moves on to next command, only if the first one is executed. Here's the quick sample. 

# cp doc1.txt /home/amar/doc && mkdir doc

This command will fail due to fact that there is no doc.1.txt file, second there is no doc directory within the /home and therefore the command will display a quick note: 'cp: cannot stat 'doc1.txt': No such file or directory'. 


11. In order to create a file, we can use the touch command. Besides creating a new file, this command is also used to change the time stamp of the files. To make a file, we can use the following command.

# touch test1.txt 


12. In order to copy the file from one location to another, we use cp command. The following command will copy foo file into bar file, automatically creating a new file.

# cp foo bar 


13. To copy a complete directory with it's files or sub-directories, we can use the following command.

# cp -R /home/Desktop/data   /home/amar/kopije

The above command use -R switch which tells the command, copy all the files and directories inside 'data' into directory by the name 'kopije'.


14. In order to remove the file or directory, we use rm command. Here's the sample.

# rm -rf data

Note: In this sample rm command uses -rf switch. The r character is for remove while the f character orders the rm command to forcefully remove all the content within this directory including files and sub-directories, if any. 

 
15. In order to make a new directory, we use the mkdir command. Here's a quick sample.

# mkdir data 


16. In order to write to a file, we use the echo, built in bash command. Here's a quick sample.

# echo 'This is a test1 file' > test1.txt 


17. In order to read the content of a file, we use the cat command. Here's a quick sample.

# cat test1.txt 

Once typed, we will get the following display output saying: ' This is a test file'. 


18. In order to display the size of directory, we use du command. Here's a quick sample.

# du -h data 

Note: The -h option is to display the size of the directory in human readable mode, or instead of numbers, there will be KB, MB, GB type of size. 


19. In order to list only directories inside desired location, we use the following command.

# ls -d */  

This command will display all the directories ending on back slash sign /. 


20. In case there are inside the directory files beginning on b character and ending on k character, and you would like to list them, the following command is going to be used.


# ls b??k 

Note: The ?? signs in above command are simply telling the ls command that I did not specify the characters in between. 


Ovo bi bio prvi dio mojih bilješki, aktuelnog Udemy Linux kursa kojeg već odavno pratim. Svakako ostavite sugestije, kritike, prijedloge, a ako nešto od ovoga naučite, bit će mi drago. Do čitanja. 

Share:

KURS LINUX ESSENTIALS

KURS LINUX ESSENTIALS

PRIRUČNIK - ZORIN OS & PRVI KORACI SA LINUXOM

POPULARNI POSTOVI

NAJNOVIJE