KEMBAR78
Command Es Linux | PDF | Directory (Computing) | Superuser
100% found this document useful (1 vote)
20 views21 pages

Command Es Linux

Uploaded by

hamidfaouzi70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
20 views21 pages

Command Es Linux

Uploaded by

hamidfaouzi70
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Commandes Linux

The Shell
The shell is basically a program that takes your commands from the keyboard and sends them to the operating
system to perform.
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.

Notice the $ at the end of the prompt? Different shells will have different prompts, in our case the $ is for a
normal user.
Let’s start with simple commands:

pwd (Print Working Directory)


Everything in Linux is a file. 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.
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.
The command pwd shows you which directory you are in, note the path stems from the root directory.

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 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/soufiane/Desktop.
 Relative path: This is the path from where you are currently in filesystem. If I was in location
/home/soufiane/Documents and wanted to get to a directory inside Documents called school, I don’t have
to specify the whole path from root like /home/soufiane/Documents/, I can just go to school/ instead.

To change directory, we use cd

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


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

If the directory you want to navigate contain spaces you must surround the name with quotes in order for the
Shell to read it correctly (both single quotes and double quotes works fine):

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/soufiane.
 - (previous directory). This will take you to the previous directory you were just at.

Using cd .

Using cd ..

Using cd ~

Using cd -
Using cd command without any flags

ls (List Directories)
we can use the wonderful ls command to list directory contents. 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.

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

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.

Commands have things called flags (or arguments or options) 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.
Using ls -R : recursively list directory contents

Using ls -r : reverse order while sorting

Using ls -t : sort by modification time, newest first


touch
touch allows you to create new empty files.

touch is also used to change timestamps on existing files and directories. Do an ls -l on a file and note the
timestamp, then touch that file and it will update the timestamp.

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.

cat
let’s learn how to read a file, short for concatenate, it not only displays file contents but it can combine
multiple files and show you the output of them.

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

less
If you are viewing text files larger than a simple output, less is more. (There is actually a command called
more that does something similar) 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.

Result:

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.

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.

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 !!
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.
Use the clear or Ctrl+l command to clear up your display.
$ clear

If you start typing the beginning of a command, file, directory, etc and hit the Tab key, it will autocomplete
based on what it finds in the directory you are searching as long as you don’t have any other files that start
with those letters.

cp (Copy)
Let’s start making some copies of these files.

mySuperFile is the file you want to copy and /home/soufiane/Documents/ 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/soufiane/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.
Using the command cp alone will not copy a directory (that contains files or does not), you’ll need to use -r
command with cp.

$ cp -r masters/ /home/soufiane/Documents

OR
$ cp -r masters /home/soufiane/Documents

They both work fine.


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.

I was trying to copy the file python(text file) from the tutorials directory to abbad directory but there is
another file inside abbad that has the name python(text file). So the shell asked me if I want to overwrite the
file.

mv (Move)
Used for moving files and also renaming them.
 You can rename files like this:

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

 And move more than one file:

 You can rename directories as well:

$ mv directory1 directory2

 Like cp, if you mv a file or directory (meaning that we want to move it) 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 folder to overwrite the previous one. You can also make a backup of that
folder and it will just rename the old version with a folder1~.(it works on folders only)

$ mv -b folder1 folder2

mkdir (Make Directory)


The mkdir command will create a directory if it doesn’t already exist. You can even make multiple directories
at the same time.

$ mkdir books videos

We are creating two directories books and videos.


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

$ mkdir -p books/hacking/basics

Here we are creating a folder books that contains the folder hacking which contains the folder basics.

rm (remove)
The rm (remove) command is used to delete files.
Using rm like this will only delete a file but it won’t delete a directory: rm index.html
Since 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.
$ rm -r directory

Take caution when using rm, once they are gone, they are gone for good.
Fortunately there are some safety measures put into place. Write-protected files will prompt you for
confirmation before deleting them. If a directory is write-protected it will also not be easily removed.

$ 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.
Or you can remove a directory with the rmdir command.

$ rmdir directory

find
find command is used to find a specific file on the system.

$ 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 search in the same directory that you are in:

$ find -name 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 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.
There also the command locate
locate rockyou.txt
But find can be faster since you can specify a directory.

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

man
Man pages are manuals that provide documentation about commands and other aspects of the system.

$ man ls

whatis
The whatis command provides a brief description of command line programs.

The description gets sourced from the manual page of each command.

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.
Using double quotes will not cause a problem.
You can remove aliases with the unalias command:

$ unalias foobar

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

$ exit
Text-Fu

stdout (Standard Out)


$ echo Hello World > peanuts.txt

Check the directory where you ran that command and you should see a file called peanuts.txt, look inside that
file and you should see the text Hello World.
The > is a redirection operator that allows us to 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.
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!
Other examples:

$ ls -l /var/log > myoutput.txt

$ echo Hello World > rm

$ > somefile.txt

The first one will output the result to myoutput.txt


The second one will create a text file called rm with the text “hello world”
The third one will create an empty file

stdin(Standard In)

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

Normally in the cat command, you send a file to it and that file becomes the stdin, 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.
pipe and tee
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.

$ 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

In this case, we took the stdout of ls -la /etc and then piped it to the less command.
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!

cut
$ echo 'The quick brown; fox jumps over the lazy dog' > sample.txt

To extract contents by a list of characters:

$ cut -c 5 sample.txt

This outputs the 5th character in each line of the file. In this case it is "q".

head
by default the head command will show you the first 10 lines in a file.

$ head /var/log/syslog

You can also modify the line count to whatever you choose, let's say I wanted to see the first 15 lines instead.

$ head -n 15 /var/log/syslog

The -n flag stands for number of lines.

tail
Similar to the head command, the tail command lets you see the last 10 lines of a file by default.

$ tail /var/log/syslog

Change the number of lines you want to see.


$ tail -n 10 /var/log/syslog

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

There –u means unique, we use it in case of repeated elements.

wc and nl
The wc (word count) command shows the total count of words in a file.
We have a file named peanuts.txt that contains “hello world”

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

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

grep
It allows you to search files for characters that match a certain pattern

You can see that grep found hello in the peanuts.txt file.
You should use quotes if you want to use multiple words like this: grep ‘hello w’ peanuts.txt
You can also grep patterns that are case insensitive with the -i flag:

$ grep -i somepattern somefile

Vi Search Patterns
To search for an expression just type the / key and then type what you are looking for. Once you hit enter, you
can press "n" to go forward or "N" to go backward in your search results.

Vi Navigation
Now you may notice, the mouse is nowhere is use here. To navigate a text document in vim, use the following
keys:
the left arrow - the up arrow - the down arrow - the right arrow

Vi Appending Text
When you try to type something you wouldn't be able to. That's because you are in command mode. To inse rt
text you'll need to enter insert mode first.
 i - insert text before the cursor
 O - insert text on the previous line
 o - insert text on the next line
 a - append text after the cursor
 A - append text at the end of the line
To exit insert mode and go back to command mode, just hit the Esc key.

Vi editing
 x - used to cut the selected text also used for deleting characters.
 dd - used to delete the current line.
 y - copy whatever is selected.
 yy - copy the current line.
 p - paste the copied text before the cursor.

Vi saving and exiting


 :w - writes or saves the file
 :q - quit out of vim
 :wq - write and then quit
 :q! - quit out of vim without saving the file
 ZZ - equivalent of :wq, but one character faster

 u - undo your last action


 Ctrl-r - redo your last action

User Management
Users and Groups
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 identif ied by the system
with their group ID (GID).
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.

Root
To get root access, run the command with sudo:

$ sudo cat /etc/shadow

There is a file called the /etc/sudoers file, this file lists users who can run sudo.

/etc/passwd
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
soufiane:x:1000:1000:Soufiane Abbad,,,:/home/soufiane:/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. 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

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

$ sudo cat /etc/shadow

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

/etc/group
$ 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

User Management Tools


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

$ sudo useradd soufiane

 Removing users
To remove a user, you can use the userdel command.

$ sudo userdel soufiane

 Changing passwords

$ passwd soufiane

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

Permissions
File Permissions
Files have different permissions or file modes.
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.

d | rwx | r-x | r-x

Each character represent a different permission:


o r: readable
o w: writable
o x: executable (basically an executable program)
o -: empty
So in the above example, we see that the user soufiane has read, write and execute permissions on the file. The
group soufiane has read and execute permissions. And finally, the other users (everyone else) has read and
execute permissions.

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

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 us er with u.
The numerical representations are seen below:
o 4: read permission
o 2: write permission
o 1: execute permission
Let's look at an example:
$ chmod 755 myfile

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

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

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

curl
wget
ps
Report a snapshot of the current processes.
To see every process on the system: ps aux

top
The top command provides a dynamic real-time view of a running system.

df
Report file system disk space usage: df –h

You might also like