Contents

Linux Fundamentals

Linux Fundamentals

Commands Syntax

All the commands in Linux have options and arguments.

Options

These modify the way that the command functions. Usually consists of a hyphen followed by a letter, some accept multiple options which can be grouped after a single hyphen.

eg ls -ltri this command lists the files with file properties sorted by the time they were created in reverse order and also prints their inode values.

Arguments

Many commands are used along with arguments, these are the additional values that can be passed on to the command, if no arguments are passed some commands assume a default argument while others give an error.

To know more about the command and its options use the man command which is short for manual
eg usage man ls

File Permissions

Linux based on UNIX is a multi-user system. Every file in the system can be protected and accessed by others by changing its access permissions. Every user had responsibility for controlling access to their files.

Permissions to a directory/file can be restricted by types

  • r - read
  • w - write
  • x - execute

each of these permissions can be controlled at three levels

  • u - user
  • g - group
  • o - other users

these permissions can be viewed by using the command ls -l and the command to change the permissions is chmod

these permissions can be set by enabling the bits for read, write and execute permissions

To add and remove permissions using chmod

  • chmod g-w <filename> remove the write permissions from the group
  • chmod a+r <filename> add the read permissions for everyone
  • chmod a+x <filename> grant execute permissions to everyone
  • chmod g-r <filename> remove the read permissions for the group users.

you can add multiple permissions in the same command too, eg chmod g+rx which grants read and write permissions to the group.

you need executable access to cd into a directory

the same permissions can also be granted using the numerical fashion for either the user, group or others.

number permission symbol
0 no permissions
1 execute –x
2 write -w-
3 execute+write -wx
4 read r–
5 read+execute r-x
6 read+write rw-
7 read+write+execute rwx

File Ownership

There are two owners of a file, user and group, there are two commands to change the ownership of the files

  • chgrp - this changes the group ownership of the file
  • chown - this changes the user ownership of the file.

As usual, we can use the -R flag command option/flag to recursively change the ownership in the directory.

Access Control List

Access Control Lists provide an additional, more extensible and flexible way to manage permissions on UNIX-based file systems. It is designed to assist with UNIX file permissions, it allows us to give permissions to any user or group to any discrete resource.

A use case - when a user is not part of a group but we still need to grant them the read or write permissions, using ACL we can grant them the permissions they need without the need to add them to any group.

ACLs are used to define more fine-grained discretionary access rights for files and directories. using setfacl and getfacl we can assign and remove ACL permissions.

command usage

  • setfacl -m u:user:rwx /path/to/file - to add permission to user
  • setfacl -m g:group:rw /path/to/file - to add permission to group
  • setfacl -Rm "entry" /path/to/dir - to allow files to recursively inherit ACL entries from the directory it is within.
  • setfacl -x u:user /path/to/file - to remove the ACL permissions use -x option/flag for a specific entry.
  • setfacl -b /path/to/file - removes all ACL permissions for all users.

giving a user write permission doesn’t allow them to delete the file.

Redirects

File

Text can be added to files using the editor or redirects using >, using > adds the text or content to the file by overwriting the file, all previous data on the file is lost, when we want to append data to the file we can use >>.

eg - ls -li > hello.txt

Input and Output

There are three redirects in Linux

  • Standard In (stdin) - file descriptor number is 0
  • Standard Out (stdout) - file descriptor number is 1
  • Standard Err (stderr) - file descriptor number is 2

stdin

This command can be used when feeding file contents to a file.

examples

  • cat < filename
  • mail -s "Office Memo" example@email.com < memoletter

stdout

This is the output of various commands or applications to the console, these can be redirected using > to write and >> to append the content to the file.
eg ls -l > list.txt

stderr

when a command is executed it can either give a stdout or an error stderr these errors can be redirected to be written to a file. eg ls -l /root 2> errorfile.

tee

./tee.png
tee command | Source: phoenixnap.com
Using tee command we can see the output on the console and also write the contents to the file at the same time. it is derived, using the tee command overwrites the file, to append the content to the file use the option -a such as tee -a

Pipes

Pipes | are used to connect or redirect the output of one command as input to another command.

File Display

  • cat - displays the contents of the file on the console/terminal
  • more - displays all the information in pagination format
  • less - reduces the information given on the console
  • head - outputs the first few lines of the file
  • tail - outputs the last few lines of the file

Text Processing and Filters

  • cut
  • awk
  • grep and egrep
  • sort
  • uniq
  • wc

cut

This is a Command Line utility that allows us to fetch or print very specific data from the files or a piped output, it can be used to cut the data using delimiters and range, byte position and character.

command description
cut filename fails
cut --version displays the version of cut
cut -c1 filename displays the first letter of each line
cut -c1,4,7 filename displays the 1st, 4th and 7th characters of each line
cut -c1-3 filename displays the characters 1-3 from each line
cut -c1-3,5-7 filename displays the charcters in positions of ranges 1-3 and 5-7
cut -b1-3 filename list by byte size
cut -d: -f6 filename list the 6th word of the line split with the delimiter :
cut -d: -f6-7 filename list the 6th and 7th word of the line split with the delimiter :
ls -l | cut -c2-4 show the 2-4 char of the piped output

awk

awk is a utility/language designed for data extraction from a file or piped input

command description
awk --version displays the version of awk
awk '{print $1}' filename displays the first word of file
ls -l | awk '{print $1,$3}' print the 1st and 3rd feild of the piped output
ls -l | awk '{print $NF}' display the last word of each entry in the piped output
awk /searchme/ {print} filename searches for the word in the file
echo "Kalyan Mudumby" | awk '{$2="M"; print $0}' replaces the second word in the line with the given input
cat filename | awk {if($9 == "kalyan") print $0} if the 9th word validated with the given word it will be printed
ls -l | awk '{print NF}' prints the number of fields

grep

GREP stands for Global Regular Expression Print is utility that processes text line by line and prints any lines that match a specified pattern.

command description
grep keyword file search for a keyword from the file
grep -c keyword file count the occurences of the keyword
grep -i KeYwOrD file search for the keyword ignore case-sensitive
grep -n keyword file display the matched lines and their numbers
grep -v keyword file display everything but the keyword
ls -l | grep Desktop search for “Desktop” from the piped output
egrep -i "keyword1|keyword2" file search for 2 keywords

sort and uniq

sort is a utility to sort text in alphabetical order
uniq command filters out the repeated or duplicate lines

command description
sort file sort the file in alphabetical order
sort -r file sort the fiel in reverse alphabetical order
sort -k2 file sort the file in alphabetical order with second word as reference
uniq file removes duplicates
sort file | uniq -c counts the total unique values
sort file | uniq -d counts the duplicate values

always sort the data before using the uniq command.

wc

wc is used to count the total words in the file or output

Compare files

  • diff - compares the file line by line
  • cmp - compares the file byte by byte

Truncate

truncate is used to shrink or extend the size of the file to the specified size eg truncate -s 10 filename data will be lost when you shrink the file.

Combine and Split

Multiple files can be combined into one and one file can be split into multiple files eg cat file1 file2 file3 file4 > file 5 to combine files and split -l 20 filename to split the file into diff files of 20 lines each.