Linux Basic Commands.pdf
Linux Basic Commands.pdf
• Introduction to Linux
• History of Linux
BASH-Bash, or the Bourne-Again Shell, is by far the most popularly used shell
and it comes installed as the default shell in the most popular Linux distributions.
Bash is the default login shell for most Linux distributions. It is also accessible for
Windows and it is the default user shell in Ubuntu, Linux Mint, Solaris 11, Pop
OS, etc.
ls
ls -a
In Linux, hidden files start with . (dot) symbol and they are not visible in the regular directory. The (ls -a) command will enlist the whole
list of the current directory including the hidden files.
ls -l
ls -lh
This command will show you the file sizes in human readable format. Size of the file is very difficult to read when displayed in terms of
byte. The (ls -lh)command will give you the data in terms of Mb, Gb, Tb, etc.
ls -lhS
If you want to display your files in descending order (highest at the top) according to their size, then you can use (ls -lhS) command.
ls -l - -block-size=[SIZE]
It is used to display the files in a specific size format. Here, in [SIZE] you can assign size according to your requirement.
ls -d */
ls -g or ls -lG
With this you can exclude column of group information and owner.
ls
ls -n
ls --color=[VALUE]
ls -li
This command prints the index number if file is in the first column.
ls -p
It is used to identify the directory easily by marking the directories with a slash (/) line sign.
ls -r
ls -R
ls -lX
It will group the files with same extensions together in the list.
ls -lt
ls ~
ls ../
cd- cd command in linux known as change directory command. It is used to change current working
directory.
cd directory_name
cd
cd /
mkdir- mkdir command in Linux allows the user to create directories (also referred to as folders in some
operating systems ). This command can create multiple directories at once as well as set the permissions
for the directories.
mkdir --version
mkdir -p first/second/third
mkdir directory_name
RM
rm stands for remove here. rm command is used to remove objects such as files, directories, symbolic
links and so on from the file system like UNIX. To be more precise, rm removes references to objects from
the filesystem, where those objects might have had multiple references (for example, a file with two
different names). By default, it does not remove directories.
$ ls
b.txt c.txt d.txt e.txt
$ rm -i d.txt
rm: remove regular empty file
'd.txt'? y
Cat,VI, NANO
vi/nano-VI/NANO are text editors for linux that can edit any file regardless
of its extension.
curl, wget
wget http://example.com/sample.php
wget -b http://www.example.com/samplepage.php
wget -b http://www.example.com/samplepage.php
curl is a command-line tool to transfer data to or from a server, using any of the supported protocols
(HTTP, FTP, IMAP, POP3, SCP, SFTP, SMTP, TFTP, TELNET, LDAP, or FILE). curl is powered by
Libcurl. This tool is preferred for automation since it is designed to work without user interaction. curl
can transfer multiple files at once.
curl -# -O ftp://ftp.example.com/file.zip
curl --silent ftp://ftp.example.com/file.zip
systemctl
The systemctl command is a utility which is responsible for examining and
controlling the systemd system and service manager. It is a collection of system
management libraries, utilities and daemons which function as a successor to the
System V init daemon. The new systemctl commands have proven quite useful in
managing a servers services. It provides detailed information about specific
systemd services, and others that have server-wide utilization.
kill
kill command in Linux (located in /bin/kill), is a built-in command which is used to terminate processes manually.
kill command sends a signal to a process which terminates the process. If the user doesn’t specify any signal
which is to be sent along with kill command then default TERM signal is sent that terminates the process.
kill -l :To display all the available signals you can use below command option:
$kill pid
a.txt
$ cp a.txt b.txt
$ ls
a.txt b.txt
mv
mv [Option] source destination
mv stands for move. mv is used to move one or more files or directories from one
place to another in a file system like UNIX. It has two distinct functions:
$ ls
a.txt b.txt c.txt d.txt
$ mv a.txt geek.txt
mv example.txt ~/Documents
$ ls
b.txt c.txt d.txt
geek.txt
Manage access to the root account
Sudo, the one command to rule them all. It stands for “super user do!” and is pronounced like “sue dough”.
If you prefix “sudo” with any Linux command, it will run that command with elevated privileges. Elevated privileges are
required to perform certain administrative tasks.
A user- group can contain multiple users. All users belonging to a group will have the same
Linux group permissions access to the file. Suppose you have a project where a number of
people require access to a file.
Any other user who has access to a file. This person has neither created the file, nor he
belongs to a usergroup who could own the file. Practically, it means everybody else.
Linux Permissions
Every file and directory in your UNIX/Linux system has following 3 permissions defined for all
the 3 owners discussed above.
● Read: This permission give you the authority to open and read a file. Read permission on a
directory gives you the ability to lists its content.
● Write: The write permission gives you the authority to modify the contents of a file. The
write permission on a directory gives you the authority to add, remove and rename files
stored in the directory. Consider a scenario where you have to write permission on file but
do not have write permission on the directory where the file is stored. You will be able to
modify the file contents. But you will not be able to rename, move or remove the file from
the directory.
● Execute: In Windows, an executable program usually has an extension “.exe” and which
you can easily run. In Unix/Linux, you cannot run a program unless the execute permission
is set. If the execute permission is not set, you might still be able to see/modify the program
code(provided read & write permissions are set), but not run it.
rw- only read and write no execute USER
r = read permission
w = write permission
x = execute permission
– = no permission
chmod
In this mode, file permissions are not represented as characters but a three-digit octal number.
The table below gives numbers for all for permissions types.
0 No Permission —
1 Execute –x
2 Write -w-
4 Read r–
Redirecting Output
The > symbol is used to redirect output by taking the output from the command on the left and passing as input to the file on the right.
Listing Archive
Extracting Archive
Create a file named file1, another file named file2 create a tar archive from files 1
and file 2. Change the permission of the file so that user can (read,write,execute)
and groups can (read) while everyone else has no permissions. Rename the file1 to
sample and copy the contents of sample to another file called sample2 using cat.
List and check the file permissions given to sample, sample2 and the archive.