Linux Notes

commands, text commands, user management, file management and processes

Linux commands

echo

prints whatever you write afterwards

bash -stands for "Bourne again shell"-

almost all Linux distributions default to bash for the terminal

pwd -print working directory-

gives you your current path

cd -change directory-

  • cd .   current directory
  • cd ..   parent directory
  • cd ~   home directory (/home/sergio)
  • cd -   previous directory

ls -list directories-

  • ls     only visible ones
  • ls -a   all, visible and invisible
  • ls -l   l for long, so gives you info about permissions and more
  • ls -al   the two things together

touch

You can create a file like this: touch mysuperduperfile
It is used as well to change timestamps in existing files and directories

file

eg: file banana.jpg
Tells you what kind of file that is and gives you more info about it

cat -concatenate-

Used basically to see the content of files:
cat dogfile.txt

less -it shows the whole file, so it's ironic-

eg: less /home/pete/Documents/text1

And then you can use this to navigate through the file:

  • q - Used to quit out of less and go back to your shell.
  • Page up, Page down, Up and Down - Navigate using the arrow keys and page keys.
  • g - Moves to beginning of the text file.
  • G - Moves to the end of the text file.
  • /search - You can search for specific text inside the text document. Prefacing the words you want to search with /
  • h - If you need a little help about how to use less while you’re in less, use help.

history commands

  • history ==> gives you a list of the commands that you have used, in inverse order
  • !! ==> runs the last command that you ran
  • ctr + R ==> reverse search, matching the commands depending of what you type

clear

clears all lines, just like alt + K

cp -copy-

  • cp mycoolfile /home/pete/Documents/cooldocs ==> renames the first file with the second param path
  • cp *.jpg /home/pete/Documents/coodocs ==> wildcard para renombrar todo lo que termine en ".jpg"
  • cp -r Pumpkin/ /home/pete/Documents ==> Para copiar recursivamente todo el contenido de un directorio
  • cp -i mycoolfile /home/pete/Pictures ==> I'll will notify you before overwritting if that's the case

mv -move-

User for moving files and renaming them. Quite similar to the cp command.
The only different behaviour I could find is this -and quite interesting-

mv -b directory1 directory2  ==> Instead of copies it will rename the new ones with a "~" prefix

mkdir -make directory-

mkdir books painting ==> you can create several directories at the same time
mkdir -p books/que/paish ==> you can create subdirectories as well with "-p"

rm -remove-

  • rm file ==> removes file
  • rm -r directory ==> to remove directories
  • rmdir directory ==> exactly the same
  • rm -f ==> force to remove, ignoring protections -if you have the right permissions-
  • rm -i ==> gives you a prompt before removing
  • rm -rfi ==> you can combine all this stuff

find

find /home -type d -name MyFolder ==> in the type it indicates that it is looking for a directory and the name is the name of the directory that we want to find

help

help echo
or 
echo --help

is the same

man -manual-

Manual of whatever command you write after.
eg: man ls // man cp // ...

whatis

Brief description of what the command does

Linux text commands

stdout (standard out) -- stdin (standard in) -- stderr (standard error)

  • stdout

echo Hello world > canijo.txt   ==> modifies the stdout and, instead of print it -standard- put it inside canijo.txt. If the file doesn't exists, it's created. It will overwrite whatever is in that file.

echo Hello killo >> canijo.txt  ===> same but won't override, will add the text to the end

  • stdin

you can modify the stdin as well. In this case instead of taking the code from "echo" it takes it from a file:

cat < peanuts.txt > banana.txt

  • stderr

can be modified as well, but probably not very important

pipe (|) and tee

  • pipe takes the stdout of a command and pass it as the stdin for another process

ls -as | less

  • tee let us write the output of a command in two different streams:

ls | tee peanuts.txt   ==> will print the result and will put it in peanuts.txt as well.

env -Environment-

env  ==> displays a list of our global variables

if you want to check a global var in particular:

echo $USER   ==> we just have to put "$" in front of the name of the variable

head

displays the first 10 lines of a file:

head /var/log/syslog

you can modify the number of lines to be displayed:

head -n 15 /var/log/syslog

tail

tail is like head but will display the last 10 lines of a file.

tail -n 15 /var/log/syslog   ==> and you can modify the number of lines as well

IMPORTANT!!

tail -f will follow the file as it grows   ==> very useful to read logs !!

wc -word count-

wc displays the number of lines, words and bytes:

$ wc /etc/passwd

96 265 5925 /etc/passwd

grep

It allows you to search files for characters that catch a certain pattern.

grep fox sample.txt   ==> will search "fox" at sample.txt


grep -i  somepattern somefile  ==> -i will make the search case insensitive

you can use pipes and regular expressions:

ls /somedir | grep '.txt$

Linux User Management

UID and GID

UID: the user id reference of the system to identify users
GID: the group id to identify groups of users

both are important to specify permissions

root or superuser

root is the most powerful user on the system. Can access any file and terminate any process.
the "sudo" command allows to run a command as root.

With the command "su" -substitute user- you can run commands as other user -for example root- as long as you know the password.

In the file "/etc/sudoers" there is a list of the users that can run "sudo". You can edit this file with the "visudo" command.

/etc/passwd

List the users and their UIDs

/etc/shadow

Stores information about user authentification

/etc/group

This file allows for different groups with different permissions.

user management commands

sudo useradd bob ==> adds a new user

sudo userdel bob ==> deletes an user

passwd bob ==> change password of user

Linux file permissions

file permissions explained

example: drwxr-xr-x

we can divide it like this:

d | rwx | r-x | r-x

the first letter is the type of file. this is typically "d" for directory or "a" for file

then the first group of three is user permissions

then the second group of three is group permissions

and the last group of three is everyone else's permissions

And they mean this:

r: readable
w: writable
x: executable
-: empty

chmod -modifying permissions-

chmod u+x myfile  => adding executable permissions to user for myfile
chmod u-x myfile => removing executable permissions to user for myfile
chmod ug+w myfile => adding writable permissions for user and group for myfile

there is another way of using chmod, using numbers:

4: read permissions
2: write permissions
1: execute permissions

chown and chgrp -modifying user and group ownership-

sudo chown patty myfile  ==> will set the ownership of myfile to patty

sudo chgrp whales myfile ==> will set the group of myfile to whales

And doing the two at once:

sudo chown patty:whales myfile

the sticky bit

If you add the sticky bit to a file you make that only root can delete that file:

sudo chmod +t mydir ==> only root can delete mydir now

Linux processes

ps -lists processes-

processes are the programs that are running on your machine. They are managed by the kernel and each one has a process ID.

$ ps



PID TTY STAT TIME CMD

41230 pts/4 Ss 00:00:00 bash

51224 pts/4 R+ 00:00:00 ps
  • PID: Process ID
  • TTY: Controlling terminal associated with the process (we'll go in detail about this later)
  • STAT: Process status code
  • TIME: Total CPU usage time
  • CMD: Name of executable/command

ps aux

a: displays all processes running
u: shows more detail 
x: show all processes that don't have a TTY associated

it shows way more fields. This fields are:

  • USER: The effective user (the one whose access we are using)
  • PID: Process ID
  • %CPU: CPU time used divided by the time the process has been running
  • %MEM: Ratio of the process's resident set size to the physical memory on the machine
  • VSZ: Virtual memory usage of the entire process
  • RSS: Resident set size, the non-swapped physical memory that a task has used
  • TTY: Controlling terminal associated with the process
  • STAT: Process status code
  • START: Start time of the process
  • TIME: Total CPU usage time
  • COMMAND: Name of executable/command

Different values for "STAT":

  • R: running or runnable, it is just waiting for the CPU to process it
  • S: Interruptible sleep, waiting for an event to complete, such as input from the terminal
  • D: Uninterruptible sleep, processes that cannot be killed or interrupted with a signal, usually to make them go away you have to reboot or fix the issue
  • Z: Zombie, we discussed in a previous lesson that zombies are terminated processes that are waiting to have their statuses collected
  • T: Stopped, a process that has been suspended/stopped

top

Gives you a list of the processes ordered by consumption of resources. Updates over time.

TTY

TTY is the terminal that executed the command. It can be a pseudoterminal -they emulate terminal with the shell terminal window, the ones that we use- or terminals -pure terminal we don't really use-

Daemon processes

Run in the background continuously. Typically start when the computer starts and basically keep the system working.

Processes and kernel

The Kernel is in charge of the processes. It allocates the memory, CPU, I/O and all the rest for each of the processes.

kill a process

kill PID

you can specify the signal as well: kill -9 1245

niceness

When you are running several processes in your computer it seems that they are running at the same time, but it's actually not like that. They use the CPU one after the other, taking a fraction each.

If they would behave normally they would take a similar amount of time of the CPU. However there is a way to influence the kernel to get more -or less- priority to get CPU time. That parameter is call "nice". A high number means that the process is nice and will take less preference. A low number is not nice.

This nice number can be set.


Packages

compression

To compress a file:

gzip mycoolfile

to decompress:

gunzip mycoolfile.gz

I order to compress several files in one, user tar (it will have the tar extension):


tar cvf mytarfile.tar mycoolfile1 mycoolfile2

where cvf means:

- c create

- v be verbose

- f filename (must have the .tar extension)


to decompress with tar:

tar xvf mytarfile.tar

where xvf means:

- x extract

- v be verbose

- f the file you want to extract


* create tar file and gzip it: tar czf myfile.tar.gz (Create all Zee Files!)

* extract those: tar xzf file.tar (eXtract all Zee Files!)

dpkg and rpm

dpkg for Debian based distributions and rpm for Red Hat based distributions

Will install a package but not its dependencies


Install a package

Debian: $ dpkg -i some_deb_package.deb

RPM: $ rpm -i some_rpm_package.rpm

The i stands for install. You can also use the longer format of --install.


Remove a package

Debian: $ dpkg -r some_deb_package.deb

RPM: $ rpm -e some_rpm_package.rpm

Debian: r for remove
RPM: e for erase


List installed packages

Debian: $ dpkg -l

RPM: $ rpm -qa

apt and yum

apt for Debian based distributions and yum for Red Hat ones.

Install a package and its dependencies:


Install a package from a repository

Debian: $ apt install package_name

RPM: $ yum install package_name

Remove a package

Debian: $ apt remove package_name

RPM: $ yum erase package_name

Updating packages for a repository


It's always best practice to update your package repositories so they are up to date before you install and update a package.

Debian: apt update; apt upgrade

RPM: yum update

Get information about an installed package

Debian: apt show package_name

RPM: yum info package_name


Don't be shy, leave us a comment