Command Line basics¶
When you work with GNU/Linux Operating Systems such as OSGeoLive, Ubuntu, etc., it is good to know how to work on the command line.
Don’t worry. It is not difficult to learn and you will discover that it is fun and very powerful.
In this quickstart you will learn some basic commands.
The following commands run from within a Terminal Emulator window.
Start a Terminal Emulator (QTerminal currently) from the Applications menu in the System Tools section. This gives you a Unix shell command prompt.
pwd shows the path of your current location.
You can ask all commands for help and get information on how they can be used and find out about additional options.
pwd --help pwd: pwd [-LP] Print the name of the current working directory. Options: -L print the value of $PWD if it names the current working directory -P print the physical directory, without any symbolic links By default, `pwd' behaves as if `-L' were specified. Exit Status: Returns 0 unless an invalid option is given or the current directory cannot be read.
You have write access in your user-directory. You can create files and directories there. Next we will create a directory for some command line tests.
cd ~ mkdir demo - creates the directory demo cd demo
You have a command history. You can navigate in the history with the arrow up and down key.
touch creates a new empty file.
cd ~/demo touch hello.txt
You can pipe the information from a command to a file i.e. the command history. history displays the command history. With the following command it can be saved in a file.
history > history.txt
You would like to know more about the content of a directory. ls is the program that gives you a lot of information.
ls lists all files and directories
ls -l shows a more detailed list of the files/directories with time, user access rights
ls -a also shows hidden files
ls -al you can combine the different options
ls -1 shows only the file names
cd ~/demo ls -l total 4 -rw-rw-r-- 1 user user 0 Dec 26 16:23 hello.txt -rw-rw-r-- 1 user user 255 Dec 26 16:24 history.txt
You can copy files from one location to another.
Copy a file to a new file
cp hello.txt hello_again.txt
Copy a file to another directory
cp hello.txt /home/user/
Copy a whole directory to a new location (-R recursive)
cp -R /home/user/demo /tmp
With sudo you can do things that you are normally not allowed to. sudo runs commands with the rights of the superuser also known as root With sudo you can i.e. install more software, administrate services, change access rights and more fun. You will see the use of sudo in some of the following commands.
Show information about programs.
apt show postgis
Search for programs
apt search postgis
sudo apt-get install sl
You will love the program sl. Run the new program with sl see also apt show sl.
Some programms run as services like PostgreSQL, tomcat or Apache Webserver. You can start or stop the services.
Restart your Apache Service
sudo service apache2 restart sudo service apache2 --help sudo service apache2 status
You can either edit files in the terminal or open them with an external program like Geany.
In the terminal window you can use the vim editor (Vi IMproved). vim is very powerful and has many options.
Open a file with vim:
press i to switch to the input mode
ESC leaves the input mode
:w saves your changes
:wq saves your changes and closes the file
:q closes the file
:q! closes the file without saving
Access rights define whether a user or group or others have access to a file and what sort of access is given. You can have read, write or excecute access.
Directories or files have an owner and a group definition. By default the creator of a file/directory is the owner. But this definition can be changed. You will learn this in the next section.
You can show the owner and group via ls -l
cd /home/user/demo ls -l -rw-rw-r-- 1 user user 122 Dec 26 16:11 history.txt
the initial character can be - for a file or d for directory
user is defined as the owner and the group
Access rights are listed at the beginning of the row:
first 3 letters for the owner
then next 3 letters for the group
followed by the last 3 letters for others
sudo chown -R user:www-data /var/www/html/TBD
first pass the user then the group - like user:www-data
sudo chmod -R 777 /var/www/html/TBD
777 everyone can do everything
number: owner (u)
number: group (g)
number: other (o)
660 - owner and group are allowed to read and write, other have no rights
744 - owner can do everything, group and others can only read
or use it like this
sudo chmod -R u+rwx /var/www/html/TBD
u = user
g = group
o = other
a = all
+/- right: r = read / w = write / x = execute
Here are other commands you may try.
grep - search for a pattern in a text
history | grep cd
rm - remove a file
rmdir - remove an empty directory
mv - rename/move
head - show the beginning of a file
tail - show the end of a file
find - search for files in a directory hierarchy
locate - find files by name
You have learned some important commands and information to work with the command line. Hopefully you have discovered how powerful the command line is.