0% found this document useful (0 votes)
13 views

linux commands full

This document provides a comprehensive overview of basic Linux commands and concepts, including the shell, file navigation, and file manipulation commands such as ls, cp, mv, and rm. It also covers how to create directories, view file contents, and utilize built-in help tools like man and whatis. The document emphasizes the importance of understanding paths, command flags, and the use of aliases for efficiency in the Linux environment.

Uploaded by

hadej11072
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

linux commands full

This document provides a comprehensive overview of basic Linux commands and concepts, including the shell, file navigation, and file manipulation commands such as ls, cp, mv, and rm. It also covers how to create directories, view file contents, and utilize built-in help tools like man and whatis. The document emphasizes the importance of understanding paths, command flags, and the use of aliases for efficiency in the Linux environment.

Uploaded by

hadej11072
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Basics of Linux

1. The Shell
What is the shell? The shell is basically a program that takes your commands from the keyboard
and sends them to the operating system to perform. If you’ve ever used a GUI, you’ve probably
seen programs such as “Terminal” or “Console” these are just programs that launch a shell for
you.
In this module we will use the shell program bash (Bourne Again shell), almost all Linux
distributions will default to the bash shell. There are other shells available such as ksh, zsh, tsch,
but we won’t get into any of those.

username@hostname:current_directory$
pete@icebox:/home/pete $

Notice the $ at the end of the prompt? Different shells will have different prompts, in our case the
$ is for a normal user using Bash, Bourne or Korn shell, you don't add the prompt symbol when
you type the command, just know that it's there.
Let’s start with a simple command, echo. The echo command just prints out the text arguments
to the display.

$ echo Hello World

Try some other Linux commands and see what they output:

1. $ date
2. $ whoami

2. pwd (Print Working Directory)


Every file is organized in a hierarchical directory tree. The first directory in the filesystem is aptly
named the root directory. The root directory has many folders and files which you can store more
folders and files, etc. Here is an example of what the directory tree looks like:

|-- bin

| |-- file1

| |-- file2

|-- etc

| |-- file3

| `-- directory1
| |-- file4

| `-- file5

|-- home

|-- var

The location of these files and directories are referred to as paths. If you had a folder named
home with a folder inside of it named pete and another folder in that folder called Movies, that
path would look like this: /home/pete/Movies
Navigation of the filesystem, much like real life is helpful if you know where you are and where
you are going. To see where you are, you can use the pwd command, this command means
“print working directory” and it just shows you which directory you are in, note the path stems
from the root directory.

$ pwd

3. cd (Change Directory)
There are two different ways to specify a path, with absolute and relative paths.

 Absolute path: This is the path from the root directory. The root is the head honcho. The
root directory is commonly shown as a slash. Every time your path starts with / it means
you are starting from the root directory. For example, /home/pete/Desktop.
 Relative path: This is the path from where you are currently in filesystem. If I was in
location /home/pete/Documents and wanted to get to a directory inside Documents called
taxes, I don’t have to specify the whole path from root like /home/pete/Documents/taxes,
I can just go to taxes/ instead.

Now that you know how paths work, we just need something to help us change to the directory
we want to. Luckily, we have cd or “change directory” to do that.

$ cd /home/pete/Pictures

So now I've changed my directory location to /home/pete/Pictures.


Now from this directory I have a folder inside called Hawaii, I can navigate to that folder with:

$ cd Hawaii

Notice how I just used the name of the folder? It’s because I was already in /home/pete/Pictures.
It can get pretty tiring navigating with absolute and relative paths all the time, luckily there are
some shortcuts to help you out.
 . (current directory). This is the directory you are currently in.
 .. (parent directory). Takes you to the directory above your current.
 ~ (home directory). This directory defaults to your “home directory”. Such as /home/pete.
 - (previous directory). This will take you to the previous directory you were just at.

$ cd .

$ cd ..

$ cd ~

$ cd -

4. ls (List Directories)
Now that we know how to move around the system, how do we figure out what is available to
us?. The ls command will list directories and files in the current directory by default, however you
can specify which path you want to list the directories of.

$ ls

$ ls /home/pete

ls is a quite useful tool, it also shows you detailed information about the files and directories you
are looking at.
Also note that not all files in a directory will be visible. Filenames that start with . are hidden, you
can view them however with the ls command and pass the -a flag to it (a for all).

$ ls -a

There is also one more useful ls flag, -l for long, this shows a detailed list of files in a long format.
This will show you detailed information, starting from the left: file permissions, number of links,
owner name, owner group, file size, timestamp of last modification, and file/directory name.

$ ls -l
pete@icebox:~$ ls -l

total 80

drwxr-x--- 7 pete penguingroup 4096 Nov 20 16:37 Desktop

drwxr-x--- 2 pete penguingroup 4096 Oct 19 10:46 Documents

drwxr-x--- 4 pete penguingroup 4096 Nov 20 09:30 Downloads

drwxr-x--- 2 pete penguingroup 4096 Oct 7 13:13 Music


drwxr-x--- 2 pete penguingroup 4096 Sep 21 14:02 Pictures
drwxr-x--- 2 pete penguingroup 4096 Jul 27 12:41 Public

drwxr-x--- 2 pete penguingroup 4096 Jul 27 12:41 Templates

drwxr-x--- 2 pete penguingroup 4096 Jul 27 12:41 Videos

Commands have things called flags (or arguments or options, whatever you want to call it) to add
more functionality. See how we added -a and -l, well you can add them both together with -la.
The order of the flags determines which order it goes in, most of the time this doesn’t really
matter so you can also do ls -al and it would still work.

$ ls -la

Run ls with different flags and see the output you receive.

 ls -R: recursively list directory contents


 ls -r: reverse order while sorting
 ls -t: sort by modification time, newest first

5. touch
Let’s learn how to make some files. A very simple way is to use the touch command. Touch
allows you to the create new empty files.

$ touch mysuperduperfile

Touch is also used to change timestamps on existing files and directories. Give it a try, do an ls -l
on a file and note the timestamp, then touch that file and it will update the timestamp.
There are many other ways to create files that involve other things like redirection and text
editors.

6. file
In the previous lesson we learned about touch. Did you notice that the filename didn’t conform to
standard naming like you’ve probably seen with other operating systems like Windows? Normally
you would expect a file called banana.jpeg and expect a JPEG picture file.
In Linux, filenames aren’t required to represent the contents of the file. You can create a file
called funny.gif that isn’t actually a GIF.
To find out what kind of file a file is, you can use the file command. It will show you a description
of the file’s contents.

$ file banana.jpg

7. cat
We’re almost done navigating files, but first let’s learn how to read a file. A simple command to
use is the cat command, short for concatenate, it not only displays file contents but it can
combine multiple files and show you the output of them.
$ cat file1
$ cat dogfile birdfile

It’s not great for viewing large files and it’s only meant for short content.

8. less
If you are viewing text files larger than a simple output, less is more. The text is displayed in a
paged manner, so you can navigate through a text file page by page.
Go ahead and look at the contents of a file with less. Once you’re in the less command, you can
actually use other keyboard commands to navigate in the file.

$ less /home/pete/Documents/text1

Use the following command to navigate through less:

 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.

9. history
In your shell, there is a history of the commands that you previously entered, you can actually
look through these commands. This is quite useful when you want to find and run a command
you used previously without actually typing it again.

$ history

Want to run the same command you did before, just hit the up arrow.
Want to run the previous command without typing it again? Use !!. If you typed cat file1 and want
to run it again, you can actually just go !! and it will run the last command you ran.
Another history shortcut is ctrl-R, this is the reverse search command, if you hit ctrl-R and you
start typing parts of the command you want it will show you matches and you can just navigate
through them by hitting the ctrl-R key again. Once you found the command you want to use
again, just hit the Enter key.
Our terminal is getting a little cluttered no? Let’s do a little cleanup, use the clear command to
clear up your display.

$ clear

10. cp (Copy)
Let’s start making some copies of these files. Much like copy and pasting files in other operating
systems, the shell gives us an even simpler way of doing that.
$ cp mycoolfile /home/pete/Documents/cooldocs

mycoolfile is the file you want to copy and /home/pete/Documents/cooldocs is where you are
copying the file to.
You can copy multiple files and directories as well as use wildcards. A wildcard is a character
that can be substituted for a pattern based selection, giving you more flexibility with searches.
You can use wildcards in every command for more flexibility.

 * the wildcard of wildcards, it's used to represent all single characters or any string.
 ? used to represent one character
 [ ] used to represent any character within the brackets

$ cp *.jpg /home/pete/Pictures

This will copy all files with the .jpg extension in your current directory to the Pictures directory.
A useful command is to use the -r flag, this will recursively copy the files and directories within a
directory.
Try to do a cp on a directory that contains a couple of files to your Documents directory. Didn’t
work did it? Well that’s because you’ll need to copy over the files and directories inside as well
with -r command.

$ cp -r Pumpkin/ /home/pete/Documents

One thing to note, if you copy a file over to a directory that has the same filename, the file will be
overwritten with whatever you are copying over. You can use the -i flag (interactive) to prompt
you before overwriting a file.

$ cp -i mycoolfile /home/pete/Pictures

11. mv (Move)
Used for moving files and also renaming them. Quite similar to the cp command in terms of flags
and functionality.
You can rename files like this:

$ mv oldfile newfile

Or you can actually move a file to a different directory:

$ mv file2 /home/pete/Documents

And move more than one file:

$ mv file_1 file_2 /somedirectory

You can rename directories as well:

$ mv directory1 directory2
Like cp, if you mv a file or directory it will overwrite anything in the same directory. So you can
use the -i flag to prompt you before overwriting anything.

mv -i directory1 directory2

Let’s say you did want to mv a file to overwrite the previous one. You can also make a backup of
that file and it will just rename the old version with a ~.

$ mv -b directory1 directory2

12. mkdir (Make Directory)


We’re gonna need some directories to store all these files we’ve been working on. The mkdir
command (Make Directory) is useful for that, it will create a directory if it doesn’t already exist.
You can even make multiple directories at the same time.

$ mkdir books paintings

You can also create subdirectories at the same time with the -p (parent flag).

$ mkdir -p books/hemmingway/favorites

13. rm (Remove)
To remove files you can use the rm command. The rm (remove) command is used to delete files
and directories.

$ rm file1

Take caution when using rm, there is no magical trash can that you can fish out removed files.
Once they are gone, they are gone for good, so be careful.
Write-protected files will prompt you for confirmation before deleting them. If a directory is write-
protected it will also not be easily removed.
Now if you don’t care about any of that, you can absolutely remove a bunch of files.

$ rm -f file1

-f or force option tells rm to remove all files, whether they are write protected or not, without
prompting the user (as long as you have the appropriate permissions).

$ rm -i file

Adding the -i flag like many of the other commands, will give you a prompt on whether you want
to actually remove the files or directories.

$ rm -r directory

You can’t just rm a directory by default, you’ll need to add the -r flag (recursive) to remove all the
files and any subdirectories it may have.
You can remove a directory with the rmdir command.
$ rmdir directory

14. find
With all these files we have on the system it can get a little hectic trying to find a specific one.
Well there’s a command we can use for that, find!

$ find /home -name puppies.jpg

With find you’ll have to specify the directory you’ll be searching it, what you’re searching for, in
this case we are trying to find a file by the name of puppies.jpg.
You can specify what type of file you are trying to find.

$ find /home -type d -name MyFolder

You can see that I set the type of file I’m trying to find as (d) for directory and I’m still searching
by the name of MyFolder.
One cool thing to note is that find doesn’t stop at the directory you are searching, it will look
inside any subdirectories that directory may have as well.

15. help
Linux has some great built-in tools to help you how to use a command or check what flags are
available for a command. One tool, help, is a built-in bash command that provides help for other
bash commands (echo, logout, pwd, etc).

$ help echo

This will give you a description and the options you can use when you want to run echo. For
other executable programs, it’s convention to have an option called --help or something similar.

$ echo --help

Not all developers who ship out executables will conform to this standard, but it’s probably your
best shot to find some help on a program.

16. man
You can see the manuals for a command with the man command.

$ man ls

Man pages are manuals that are by default built into most Linux operating systems. They provide
documentation about commands and other aspects of the system.
Try it out on a few commands to get more information about them.
17. whatis
If you are ever feeling doubtful about what a command does, you can use the whatis command.
The whatis command provides a brief description of command line programs.

$ whatis cat

The description gets sourced from the manual page of each command. If you ran whatis cat,
you’d see there is a small blurb with a short description.

18. alias
Sometimes typing commands can get really repetitive, or if you need to type a long command
many times, it’s best to have an alias you can use for that. To create an alias for a command you
simply specify an alias name and set it to the command.

$ alias foobar='ls -la'

Now instead of typing ls -la, you can type foobar and it will execute that command, pretty neat
stuff. Keep in mind that this command won't save your alias after reboot, so you'll need to add a
permanent alias in:

~/.bashrc

or similar files if you want to have it persist after reboot.


You can remove aliases with the unalias command:

$ unalias foobar

19. exit
To exit from the shell, you can use the exit command

$ exit

Or the logout command:

$ logout

Or if you are working out of a terminal GUI, you can just close the terminal.

20. stdout (Standard Out)


By now, we've become familiar with many commands and their output and that brings us to our
next subject I/O (input/output) streams. Let's run the following command and we'll discuss how
this works.
$ echo Hello World > peanuts.txt

What just happened? Well check the directory where you ran that command and lo and behold
you should see a file called peanuts.txt, look inside that file and you should see the text Hello
World. Lots of things just happened in one command so let's break it down.
First let's break down the first part:

$ echo Hello World

We know this prints out Hello World to the screen, but how? Processes use I/O streams to
receive input and return output. By default the echo command takes the input (standard input or
stdin) from the keyboard and returns the output (standard output or stdout) to the screen. So
that's why when you type echo Hello World in your shell, you get Hello World on the screen.
However, I/O redirection allows us to change this default behavior giving us greater file flexibility.
Let's proceed to the next part of the command:

>

The > is a redirection operator that allows us the change where standard output goes. It allows
us to send the output of echo Hello World to a file instead of the screen. If the file does not
already exist it will create it for us. However, if it does exist it will overwrite it (you can add a shell
flag to prevent this depending on what shell you are using).
And that's basically how stdout redirection works!
Well let's say I didn't want to overwrite my peanuts.txt, luckily there is a redirection operator for
that as well, >>:

$ echo Hello World >> peanuts.txt

This will append Hello World to the end of the peanuts.txt file, if the file doesn't already exist it will
create it for us like it did with the > redirector!

21. stdin (Standard In)


In our previous lesson we learned that we have different stdout streams we can use, such as a
file or the screen. Well there are also different standard input (stdin) streams we can use as well.
We know that we have stdin from devices like the keyboard, but we can use files, output from
other processes and the terminal as well, let's see an example.
Let's use the peanuts.txt file in the previous lesson for this example, remember it had the text
Hello World in it.

$ cat < peanuts.txt > banana.txt

Just like we had > for stdout redirection, we can use < for stdin redirection.
Normally in the cat command, you send a file to it and that file becomes the stdin, in this case,
we redirected peanuts.txt to be our stdin. Then the output of cat peanuts.txt which would be Hello
World gets redirected to another file called banana.txt.

22. pipe and tee


Let's get into some plumbing now, not really but kinda. Let's try a command:
$ ls -la /etc

You should see a very long list of items, it's a little hard to read actually. Instead of redirecting
this output to a file, wouldn't it be nice if we could just see the output in another command like
less? Well we can!

$ ls -la /etc | less

The pipe operator |, represented by a vertical bar, allows us to get the stdout of a command and
make that the stdin to another process. In this case, we took the stdout of ls -la /etc and
then piped it to the less command. The pipe command is extremely useful and we will continue to
use it for all eternity.
Well what if I wanted to write the output of my command to two different streams? That's possible
with the tee command:

$ ls | tee peanuts.txt

You should see the output of ls on your screen and if you open up the peanuts.txt file you should
see the same information!

23. sort
The sort command is useful for sorting lines.

file1.txt

dog

cow

cat

elephant

bird

$ sort file1.txt

bird

cat

cow

dog
elephant

You can also do a reverse sort:

$ sort -r file1.txt

elephant

dog

cow

cat

bird

And also sort via numerical value:

$ sort -n file1.txt

bird

cat

cow

elephant

dog

23. uniq (Unique)


The uniq (unique) command is another useful tool for parsing text.
Let's say you had a file with lots of duplicates:

reading.txt

book
book

paper

paper

article

article

magazine

And you wanted to remove the duplicates, well you can use the uniq command:

$ uniq reading.txt

book

paper

article

magazine

Let's get the count of how many occurrences of a line:

$ uniq -c reading.txt

2 book

2 paper

2 article

1 magazine

Let's just get unique values:


$ uniq -u reading.txt

magazine

Let's just get duplicate values:

$ uniq -d reading.txt

book

paper

article

Note : uniq does not detect duplicate lines unless they are adjacent. For eg:
Let's say you had a file with duplicates which are not adjacent:

reading.txt

book

paper

book

paper

article

magazine

article

$ uniq reading.txt

reading.txt

book

paper
book

paper

article

magazine

article

The result returned by uniq will contain all the entries unlike the very first
example.

To overcome this limitation of uniq we can use sort in combination with uniq:

$ sort reading.txt | uniq

article

book

magazine

paper

24. wc and nl
The wc (word count) command shows the total count of words in a file.

$ wc /etc/passwd

96 265 5925 /etc/passwd

It display the number of lines, number of words and number of bytes, respectively.
To just see just the count of a certain field, use the -l, -w, or -c respectively.

$ wc -l /etc/passwd

96

Another command you can use to check the count of lines on a file is the nl (number lines)
command.
file1.txt

like

turtles

$ nl file1.txt

1. i
2. like
3. turtles

25. grep
The grep command is quite possibly the most common text processing command you will use. It
allows you to search files for characters that match a certain pattern. What if you wanted to know
if a file existed in a certain directory or if you wanted to see if a string was found in a file? You
certainly wouldn't dig through every line of text, you would use grep!
Let's use our sample.txt file as an example:

$ grep fox sample.txt

You should see that grep found fox in the sample.txt file.
You can also grep patterns that are case insensitive with the -i flag:

$ grep -i somepattern somefile

To get even more flexible with grep you can combine it with other commands with |.

$ env | grep -i User

As you can see grep is pretty versatile. You can even use regular expressions in your pattern:

$ ls /somedir | grep '.txt$'

Should return all files ending with .txt in somedir.


USER MANAGEMENT
1. Users and Groups
In any traditional operating system, there are users and groups. They exist solely for access and
permissions. When running a process, it will run as the owner of that process whether that is
Jane or Bob. File access and ownership is also permission dependent. You wouldn't want Jane
to see Bob's documents and vice versa.
Each user has their own home directory where their user specific files get stored, this is usually
located in /home/username, but can vary in different distributions.
The system uses user ids (UID) to manage users, usernames are the friendly way to associate
users with identification, but the system identifies users by their UID. The system also uses
groups to manage permissions, groups are just sets of users with permission set by that group,
they are identified by the system with their group ID (GID).
In Linux, you'll have users in addition to the normal humans that use the system. Sometimes
these users are system daemons that continuously run processes to keep the system
functioning. One of the most important users is root or superuser, root is the most powerful user
on the system, root can access any file and start and terminate any process. For that reason, it
can be dangerous to operate as root all the time, you could potentially remove system critical
files. Luckily, if root access is needed and a user has root access, they can run a command as
root instead with the sudo command. The sudo command (superuser do) is used to run a
command with root access, we'll go more in depth on how a user receives root access in a later
lesson.
Go ahead and try to view a protected file like /etc/shadow:

$ cat /etc/shadow

Notice how you get a permission denied error, look at the permissions with:

$ ls -la /etc/shadow

-rw-r----- 1 root shadow 1134 Dec 1 11:45 /etc/shadow

We haven't gone through permissions yet, but what's happening here is that root is the owner of
the file and you'll need root access or be part of the shadow group to read the contents. Now run
the command with sudo:

$ sudo cat /etc/shadow

Now you'll be able to see the contents of the file!

2. root
We've looked at one way to get superuser access using the sudo command. You can also run
commands as the superuser with the su command. This command will "substitute users" and
open a root shell if no username is specified. You can use this command to substitute to any
user as long as you know the password.
$ su

There are some downsides to using this method: it's much easier to make a critical mistake
running everything in root, you won't have records of the commands you use to change system
configurations, etc. Basically, if you need to run commands as the superuser, just stick to sudo.
Now that you know what commands to run as the superuser, the question is how do you know
who has access to do that? The system doesn't let every single Joe Schmoe run commands as
the superuser, so how does it know? There is a file called the /etc/sudoers file, this file lists users
who can run sudo. You can edit this file with the visudo command.

3. /etc/passwd
Remember that usernames aren't really identifications for users. The system uses a user ID
(UID) to identify a user. To find out what users are mapped to what ID, look at the /etc/passwd
file.

$ cat /etc/passwd

This file shows you a list of users and detailed information about them. For example, the first line
in this file most likely looks like this:

root:x:0:0:root:/root:/bin/bash

Each line displays user information for one user, most commonly you'll see the root user as the
first line. There are many fields separated by colons that tell you additional information about the
user, let's look at them all:

1. Username
2. User's password - the password is not really stored in this file, it's usually stored in the
/etc/shadow file. We'll discuss more in the next lesson about /etc/shadow, but for now,
know that it contains encrypted user passwords. You can see many different symbols
that are in this field, if you see an "x" that means the password is stored in the
/etc/shadow file, a "*" means the user doesn't have login access and if there is a blank
field that means the user doesn't have a password.
3. The user ID - as you can see root has the UID of 0
4. The group ID
5. GECOS field - This is used to generally leave comments about the user or account such
as their real name or phone number, it is comma delimited.
6. User's home directory
7. User's shell - you'll probably see a lot of user's defaulting to bash for their shell

Normally in a user's setting page, you would expect you see just human users. However, you'll
notice /etc/passwd contains other users. Remember that users are really only on the system to
run processes with different permissions. Sometimes we want to run processes with pre-
determined permissions. For example, the daemon user is used for daemon processes.

4. /etc/shadow
The /etc/shadow file is used to store information about user authentication. It requires superuser
read permissions.

$ sudo cat /etc/shadow


root:MyEPTEa$6Nonsense:15000:0:99999:7:::

You'll notice that it looks very similar to the contents of /etc/passwd, however in the password
field you'll see an encrypted password. The fields are separated by colons as followed:

1. Username
2. Encrypted password
3. Date of last password changed - expressed as the number of days since Jan 1, 1970. If
there is a 0 that means the user should change their password the next time they login
4. Minimum password age - Days that a user will have to wait before being able to change
their password again
5. Maximum password age - Maximum number of days before a user has to change their
password
6. Password warning period - Number of days before a password is going to expire
7. Password inactivity period - Number of days after a password has expired to allow login
with their password
8. Account expiration date - date that user will not be able to login
9. Reserved field for future use

In most distributions today, user authentication doesn't rely on just the /etc/shadow file, there are
other mechanisms in place such as PAM (Pluggable Authentication Modules) that replace
authentication.

5. /etc/group
Another file that is used in user management is the /etc/group file. This file allows for different
groups with different permissions.

$ cat /etc/group

root:*:0:pete

Very similar to the /etc/password field, the /etc/group fields are as follows:

1. Group name
2. Group password - there isn't a need to set a group password, using an elevated privilege
like sudo is standard. A "*" will be put in place as the default value.
3. Group ID (GID)
4. List of users - you can manually specify users you want in a specific group

6. User Management Tools


Most enterprise environments are using management systems to manage users, accounts and
passwords. However, on a single machine computer there are useful commands to run to
manage users.
Adding Users
You can use the adduser or the useradd command. The adduser command contains more
helpful features such as making a home directory and more. There are configuration files for
adding new users that can be customized depending on what you want to allocate to a default
user.

$ sudo useradd bob

You'll see that the above command creates an entry in /etc/passwd for bob, sets up default
groups and adds an entry to the /etc/shadow file.
Removing Users
To remove a user, you can use the userdel command.

$ sudo userdel bob

This basically does its best to undo the file changes by useradd.
Changing Passwords

$ passwd bob

This will allow you to change the password of yourself or another user (if you are root).

PERMISSIONS
1. File Permissions
As we learned previously, files have different permissions or file modes. Let's look at an
example:

$ ls -l Desktop/

drwxr-xr-x 2 pete penguins 4096 Dec 1 11:45 .

There are four parts to a file's permissions. The first part is the filetype, which is denoted by the
first character in the permissions, in our case since we are looking at a directory it shows d for
the filetype. Most commonly you will see a - for a regular file.
The next three parts of the file mode are the actual permissions. The permissions are grouped
into 3 bits each. The first 3 bits are user permissions, then group permissions and then other
permissions. I've added the pipe to make it easier to differentiate.

d | rwx | r-x | r-x

Each character represent a different permission:

 r: readable
 w: writable
 x: executable (basically an executable program)
 -: empty

So in the above example, we see that the user pete has read, write and execute permissions on
the file. The group penguins has read and execute permissions. And finally, the other users
(everyone else) has read and execute permissions.

2. Modifying Permissions
Changing permissions can easily be done with the chmod command.
First, pick which permission set you want to change, user, group or other. You can add or
remove permissions with a + or -, let's look at some examples.
Adding permission bit on a file

$ chmod u+x myfile

The above command reads like this: change permission on myfile by adding executable
permission bit on the user set. So now the user has executable permission on this file!
Removing permission bit on a file

$ chmod u-x myfile

Adding multiple permission bits on a file

$ chmod ug+w

There is another way to change permissions using numerical format. This method allows you to
change permissions all at once. Instead of using r, w, or x to represent permissions, you'll use a
numerical representation for a single permission set. So no need to specify the group with g or
the user with u.
The numerical representations are seen below:

 4: read permission
 2: write permission
 1: execute permission

Let's look at an example:

$ chmod 755 myfile

Can you guess what permissions we are giving this file? Let's break this down, so now 755
covers the permissions for all sets. The first number (7) represents user permissions, the second
number (5) represents group permissions and the last 5 represents other permissions.
Wait a minute, 7 and 5 weren't listed above, where are we getting these numbers? Remember
we are combining all the permissions into one number now, so you'll have to get some math
involved.
7 = 4 + 2 + 1, so 7 is the user permissions and it has read, write and execute permissions
5 = 4 + 1, the group has read and execute permissions
5 = 4 +1, and all other users have read and execute permissions
One thing to note: it's not a great idea to be changing permissions nilly willy, you could potentially
expose a sensitive file for everyone to modify, however many times you legitimately want to
change permissions, just take precaution when using the chmod command.

3. Ownership Permissions
In addition to modifying permissions on files, you can also modify the group and user ownership
of the file as well.
Modify user ownership

$ sudo chown patty myfile

This command will set the owner of myfile to patty.


Modify group ownership

$ sudo chgrp whales myfile

This command will set the group of myfile to whales.


Modify both user and group ownership at the same time
If you add a colon and groupname after the user you can set both the user and group at the
same time.

$ sudo chown patty:whales myfile

4. Umask
Every file that gets created comes with a default set of permissions. If you ever wanted to change
that default set of permissions, you can do so with the umask command. This command takes
the 3 bit permission set we see in numerical permissions.
Instead of adding these permissions though, umask takes away these permissions.

$ umask 021

In the above example, we are stating that we want the default permissions of new files to allow
users access to everything, but for groups we want to take away their write permission and for
others we want to take away their executable permission. The default umask on most
distributions is 022, meaning all user access, but no write access for group and other users.
When you run the umask command it will give that default set of permissions on any new file you
make. However, if you want it to persist you'll have to modify your startup file (.profile), but we'll
discuss that in a later lesson.

Packages
gzip bzip2 & tar
Before we get into package installation and the different managers, we need to discuss archiving
and compressing files, because you will most likely encounter these when you hunt for software
on the internet.
You probably already know what a file archive is, you've most likely encountered file types such
as .rar and .zip. These are an archive of files, they contain many files inside of them, but they
come in this very neat single file known as an archive.
Compressing files with gzip
gzip is program used to compress files in Linux, they end in a .gz extension.
To compress a file down:

$ gzip mycoolfile

To decompress the file:

$ gunzip mycoolfile.gz

Note :Remember that the filename extension must be in .gz format

Compressing files with bzip2


Bzip2 is program used to compress files in Linux, they end in a .bz2 extension.
To compress a file down:

$ bzip2 mycoolfile

To decompress the file:


$ bzip2 –d mycoolfile.bz2
Creating archives with tar
Unfortunately, gzip can't add multiple files into one archive for us. Luckily we have the tar
program which does. When you create an archive using tar, it will have a .tar extension.

$ tar cvf mytarfile.tar mycoolfile1 mycoolfile2

 c - create
 v - tell the program to be verbose and let us see what it's doing
 f - the filename of the tar file has to come after this option, if you are creating a tar file
you'll have to come up with a name

Unpacking archives with tar


To extract the contents of a tar file, use:

$ tar xvf mytarfile.tar

 x - extract
 v - tell the program to be verbose and let us see what it's doing
 f - the file you want to extract

Compressing/uncompressing archives with tar and gzip


Many times you'll see a tar file that has been compressed such as: mycompressedarchive.tar.gz,
all you need to do is work outside in, so first remove the compression with gunzip and then you
can unpack the tar file. Or you can alternatively use the z option with tar, which just tells it to use
the gzip or gunzip utility.
Create a compressed tar file:

$ tar czf myfile.tar.gz

Uncompress and unpack:

$ tar xzf file.tar

Compressing/uncompressing archives with zip


$ zip <files> file.zip
$ unzip file.zip

You might also like