× Main Home Linux File Directory Permission Environment Pipes and Filters User - sudo
☰ LINUX

LINUX

Linux is a family of open source Unix-like operating systems based on the Linux kernel, an operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically packaged in a Linux distribution.

The computer programs that allocate the system resources and coordinate all the details of the computer's internals is called the operating system or the kernel.
Users communicate with the kernel through a program known as the shell. The shell is a command line interpreter; it translates commands entered by the user and converts them into a language that is understood by the kernel.
>> Unix was originally developed in 1969 by a group of AT&T employees Ken Thompson, Dennis Ritchie, Douglas McIlroy, and Joe Ossanna at Bell Labs.
>> There are various Unix variants available in the market. Solaris Unix, AIX, HP Unix and BSD are a few examples. Linux is also a flavor of Unix which is freely available.
>> Several people can use a Unix computer at the same time; hence Unix is called a multiuser system.
>> A user can also run multiple programs at the same time; hence Unix is a multitasking environment.

sudo login
(to login)

logout
( logout after login)

cal
(to show current calender)

ls
(listing directories)

whoami
(who is currently logged in)

users
(who is currently logged in)

who
(information about users who are currently login in)

w
( quick summary of every user logged into a computer)
passwd
(change password)

halt
(Brings the system down immediately)

init 0
(Powers off the system using predefined scripts to synchronize and clean up the system prior to shutting down)

init 6
(Reboots)

poweroff
(Shuts down the system by powering off)

reboot
(Reboots the system)

shutdown
(Shuts down the system)
FILE MANAGEMENT
ls -l

drwxrwxr-x 2 amrood amrood 4096 Dec 25 09:59 Desktop

First Column − Represents the file type and the permission given on the file. Below is the description of all type of files.
Second Column − Represents the number of memory blocks taken by the file or directory.
Third Column − Represents the owner of the file. This is the Unix user who created this file.
Fourth Column − Represents the group of the owner. Every Unix user will have an associated group.
Fifth Column − Represents the file size in bytes.
Sixth Column − Represents the date and the time when this file was created or modified for the last time.
Seventh Column − Represents the file or the directory name.
Prefix & Description
- − Regular file, such as an ASCII text file, binary executable, or hard link.
b − Block special file. Block input/output device file such as a physical hard drive.
c − Character special file. Raw input/output device file such as a physical hard drive.
d − Directory file that contains a listing of other files and directories.
l − Symbolic link file. Links on any regular file..
p − Named pipe. A mechanism for interprocess communications.
s − Socket used for interprocess communication.
Metacharacters
ls ch*.doc − Displays all the files, the names of which start with ch and end with .doc
ls *.doc − Display all the files ending with just .doc
Hidden Files
An invisible file is one, the first character of which is the dot or the period character (.). Unix programs (including the shell) use most of these files to store configuration information.
Some common examples of the hidden files include the files −
.profile − The Bourne shell ( sh) initialization script
.kshrc − The Korn shell ( ksh) initialization script
.cshrc − The C shell ( csh) initialization script
.rhosts − The remote shell configuration file
ls -a − list the invisible files
ls --all − list the invisible files
Single dot (.) − This represents the current directory.
Double dot (..) − This represents the parent directory.
vi filename
(vi editor to create ordinary files)

i
(for insert)

Esc
(come out of the edit mode)

:wq
(write and quite)

:q!
(just quite)

touch sample
( create sample file)

cat> sample
(write to sample)

cat sample
(see content)

cd
(change directory)

exit
(close terminal)
cat -b filename
( display the line numbers)

wc filename
(Counting Words in a File)

2 19 103 filename

First Column − total number of lines in the file.
Second Column − total number of words in the file.
Third Column − number of bytes in the file.
Fourth Column − Represents the file name.
wc filename1 filename2
(information about multiple files )

cp source_file destination_file
( copying)

mv old_file new_file
(rename)

rm filename
(delete file)

rm filename1 filename2 filename3
(delete files)

rm -i filename
(-i for confirmation)


Unix / Linux - Directory Management
cd ~
(home directory)

cd ~username
(go in any other user's home directory)

cd -
(go in your last directory)

cd /home
(pathname is absolute , relation to root)

cd Desktop
(pathname is relative)

pwd
(current working directory)

mkdir dirname
(create directories)

mkdir -p /tmp/amrood/test
(creates all the required parent directories)

rmdir dirname
(To remove a directory)

cd dirname
(Changing Directories)

cd ../
(backward directory)

mv mydir yourdir
(Renaming Directories)

cd ./Desktop
(. represents current directory)


Unix / Linux - File Permission / Access Modes
  • Owner permissions
  • Group permissions
  • Other (world) permissions
    -rwxr-xr-- 1 amrood users 1024 Nov 2 00:10 myfile

  • 1 file / Directory
  • 2,3,4 Owner permissions
  • 5,6,7 Group permissions
  • 8,9,10 Other (world) permissions

File Access Modes
  • r - Read
  • w - Write
  • x - Executable

Changing Permissions
    chmod
  • + - Adds the designated permission(s)
  • - - Removes the designated permission(s)
  • = - Sets the designated permission(s)
    eg : chmod u+wx filename
    eg : chmod o+wx,u-x,g = rx testfile

  • o - Other
  • g - Group
  • u - Owner
Using chmod with Absolute Permissions

Number Octal permission representation Ref
0 No permission ---
1 Execute permission --x
2 Write permission -w-
3 Execute and write permission -wx
4 Read permission r--
5 Read and execute permission r-x
6 Read and write permission rw-
7 All permissions rwx
eg : chmod 755 testfile

Changing Owners and Groups

While creating an account on Unix, it assigns a owner ID and a group ID to each user

chown user filelist
"change owner"

chgrp group filelist
"change group"

NOTE − The super user, root, has the unrestricted capability to change the ownership of any file but normal users can change the ownership of only those files that they own.

SUID and SGID File Permission

Often when a command is executed, it will have to be executed with special privileges in order to accomplish its task.
As an example, when you change your password with the passwd command, your new password is stored in the file /etc/shadow.
As a regular user, you do not have read or write access to this file for security reasons, but when you change your password, you need to have the write permission to this file. This means that the passwd program has to give you additional permissions so that you can write to the file /etc/shadow.
Additional permissions are given to programs via a mechanism known as the Set User ID (SUID) and Set Group ID (SGID) bits.
When you execute a program that has the SUID bit enabled, you inherit the permissions of that program's owner. Programs that do not have the SUID bit set are run with the permissions of the user who started the program.
This is the case with SGID as well. Normally, programs execute with your group permissions, but instead your group will be changed just for this program to the group owner of the program.
The SUID and SGID bits will appear as the letter "s" if the permission is available. The SUID "s" bit will be located in the permission bits where the owners’ execute permission normally resides.
$ ls -l /usr/bin/passwd
-r-sr-xr-x 1 root bin 19031 Feb 7 13:47 /usr/bin/passwd*

Shows that the SUID bit is set and that the command is owned by the root. A capital letter S in the execute position instead of a lowercase s indicates that the execute bit is not set.
If the sticky bit is enabled on the directory, files can only be removed if you are one of the following users −
1. The owner of the sticky directory
2. The owner of the file being removed
3. The super user, root


chmod ug+s dirname
(To set the SUID and SGID bits)

Unix / Linux - Environment
$TEST="Unix Programming"
$echo $TEST

The .profile File

The file /etc/profile is maintained by the system administrator of your Unix machine and contains shell initialization information required by all users on a system.
The file .profile is under your control. You can add as much shell customization information as you want to this file. The minimum set of information that you need to configure includes −

  • The type of terminal you are using.
  • A list of directories in which to locate the commands.
  • A list of variables affecting the look and feel of your terminal.

You can check your .profile available in your home directory. Open it using the vi editor and check all the variables set for your environment.


Setting the Terminal Type

Usually, the type of terminal you are using is automatically configured by either the login or getty programs. Sometimes, the auto configuration process guesses your terminal incorrectly. If your terminal is set incorrectly, the output of the commands might look strange, or you might not be able to interact with the shell properly. To make sure that this is not the case, most users set their terminal to the lowest common denominator in the following way

TERM=vt100
Setting the PATH

When you type any command on the command prompt, the shell has to locate the command before it can be executed. The PATH variable specifies the locations in which the shell should look for commands. Usually the Path variable is set as follows

PATH=/bin:/usr/bin

Here, each of the individual entries separated by the colon character (:) are directories. If you request the shell to execute a command and it cannot find it in any of the directories given in the PATH variable, a message similar to the following appears −

$hello
hello: not found
$

PS1 and PS2 Variables

The characters that the shell displays as your command prompt are stored in the variable PS1. You can change this variable to be anything you want. As soon as you change it, it'll be used by the shell from that point on. For example, if you issued the command

$PS1='=>'
=>
=>
=>

=>PS1="[\u@\h \w]\$"
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$
[root@ip-72-167-112-17 /var/www/tutorialspoint/unix]$

Escape Sequence Description
\t Current time, expressed as HH:MM:SS
\d Current date, expressed as Weekday Month Date
\n Newline
\s Current shell environment
\W Working directory
\w Full path of the working directory
\u Current user’s username
\h Hostname of the current machine
\# Command number of the current command. Increases when a new command is entered
\$ If the effective UID is 0 (that is, if you are logged in as root), end the prompt with the # character; otherwise, use the $ sign

You can make the change yourself every time you log in, or you can have the change made automatically in PS1 by adding it to your .profile file.
When you issue a command that is incomplete, the shell will display a secondary prompt and wait for you to complete the command and hit Enter again.
The default secondary prompt is > (the greater than sign), but can be changed by re-defining the PS2 shell variable −
Following is the example which uses the default secondary prompt

$ echo "this is a
> test"
this is a
test
$

The example given below re-defines PS2 with a customized prompt −

$ PS2="secondary prompt->"
$ echo "this is a
secondary prompt->test"
this is a
test
$

Environment Variables

Variable Description
DISPLAY Contains the identifier for the display that X11 programs should use by default.
HOME Indicates the home directory of the current user: the default argument for the cd built-in command.
IFS Indicates the Internal Field Separator that is used by the parser for word splitting after expansion.
LANG LANG expands to the default system locale; LC_ALL can be used to override this. For example, if its value is pt_BR, then the language is set to (Brazilian) Portuguese and the locale to Brazil.
LD_LIBRARY_PATH A Unix system with a dynamic linker, contains a colonseparated list of directories that the dynamic linker should search for shared objects when building a process image after exec, before searching in any other directories.
PATH Indicates the search path for commands. It is a colon-separated list of directories in which the shell looks for commands.
PWD Indicates the current working directory as set by the cd command.
RANDOM Generates a random integer between 0 and 32,767 each time it is referenced
SHLVL Increments by one each time an instance of bash is started. This variable is useful for determining whether the built-in exit command ends the current session.
TERM Refers to the display type.
TZ Refers to Time zone. It can take values like GMT, AST, etc.
UID Expands to the numeric user ID of the current user, initialized at the shell startup.

Following is the sample example showing few environment variables −

$ echo $HOME
/root
]$ echo $DISPLAY
$ echo $TERM
xterm
$ echo $PATH
/usr/local/bin:/bin:/usr/bin: /home/amrood/bin:/usr/local/bin


Unix / Linux - Pipes and Filters

You can connect two commands together so that the output from one program becomes the input of the next program. Two or more commands connected in this way form a pipe. To make a pipe, put a vertical bar (|) on the command line between two commands.

The grep command
grep pattern file(s)
(search certain pattern)

ls -l | grep "Aug"

Option Description
-v Prints all lines that do not match pattern.
-n Prints the matched line and its line number.
-l Prints only the names of files with matching lines (letter "l")
-c Prints only the count of matching lines.
-i Matches either upper or lowercase.

The sort Command
sort text
Option Description
-n Sorts numerically (example: 10 will sort after 2), ignores blanks and tabs.
-r Reverses the order of sort.
-f Sorts upper and lowercase together.
+x Ignores first x fields when sorting.

ls -l | grep "Aug" | sort +4n

The pg and more Commands
ls -l | more ls -l | grep "Aug" | sort +4n | more
User - Sudo
apt-get install sudo
(to install sudo)

adduser jobin
(to add new user)

sudo usermod -a -G sudo jobin
(add sudo permission to an user)

su jobin
(switch to another user)

sudo deluser jobin
(delete an user)