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

Linux Main

This document discusses basic Linux commands. It describes commands like ls, pwd, cd, mkdir which are used to list, change directories, create directories. It also covers commands like cat, vim, grep, sort, tail, chmod, chown which can view files, search files, sort output and modify permissions. The document provides examples of commonly used commands and their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Linux Main

This document discusses basic Linux commands. It describes commands like ls, pwd, cd, mkdir which are used to list, change directories, create directories. It also covers commands like cat, vim, grep, sort, tail, chmod, chown which can view files, search files, sort output and modify permissions. The document provides examples of commonly used commands and their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Linux

#Basic_Linux_commands:
Listing commands
ls option flag arguments --> list the sub directories and files available in the present directory
hashtag#Examples:
ls -l--> list the files and directories in long list format with extra information.
ls -a --> list all including hidden files and directory.
ls *.sh --> list all the files having .sh extension.
ls -i --> list the files and directories with index numbers inodes.
ls -d */ --> list only directories. (we can also specify a pattern).

hashtag#Directory_commands
pwd --> print work directory. Gives the present working directory.
cd path_to_directory --> change directory to the provided path
cd ~ or just cd --> change directory to the home directory
cd - --> Go to the last working directory.
cd .. --> change directory to one step back.
cd ../.. --> Change directory to 2 levels back.
mkdir directoryName --> to make a directory in a specific location.
Examples:
1. mkdir newFolder # make a new folder 'newFolder'

2. mkdir .NewFolder # make a hidden directory

3. mkdir A B C D # make multiple directories at the same time.

4. mkdir /home/user/Mydirectory # make a new folder in a specific location.

5. mkdir -p A/B/C/D # make a nested directory.

Basic Linux commands


Listing commands
ls option flag arguments --> list the sub directories and files available in the present directory
Examples:

 ls -l--> list the files and directories in long list format with extra information.
 ls -a --> list all including hidden files and directory.
 ls *.sh --> list all the files having .sh extension.
 ls -i --> list the files and directories with index numbers inodes.
 ls -d */ --> list only directories. (we can also specify a pattern).
Directory commands
 pwd --> print work directory. Gives the present working directory.
 cd path_to_directory --> change directory to the provided path
 cd ~ or just cd --> change directory to the home directory
 cd - --> Go to the last working directory.
 cd .. --> change directory to one step back.
 cd ../.. --> Change directory to 2 levels back.
 mkdir directoryName --> to make a directory in a specific location.
Examples:

1. mkdir newFolder # make a new folder 'newFolder'

2. mkdir .NewFolder # make a hidden directory (also . before a file to make it


hidden)

3. mkdir A B C D # make multiple directories at the same time.

4. mkdir /home/user/Mydirectory # make a new folder in a specific location.

5. mkdir -p A/B/C/D # make a nested directorS

Most used commands in Linux:


1. ls - The most frequently used command in Linux to list directories.
2. pwd - Print working directory command in Linux.
3. cd - Linux command to navigate through directories.
4. mkdir - Command used to create directories in Linux.
5. mv - Move or rename files in Linux.
6. cp - Similar usage as mv but for copying files in Linux.
7. rm - Delete files or directories.
8. touch - Create blank/empty files.
9. ln - Create symbolic links (shortcuts) to other files.
10. cat - Display file contents on the terminal.
11. clear - Clear the terminal display.
12. echo - Print any text that follows the command.
13. less - Linux command to display paged outputs in the terminal.
14. man - Access manual pages for all Linux commands.
15. uname - Linux command to get basic information about the OS.
16. whoami - Get the active username.
17. tar - Command to extract and compress files in Linux.
18. grep - Search for a string within an output.
19. head - Return the specified number of lines from the top.
20. tail - Return the specified number of lines from the bottom.
21. diff - Find the difference between two files.
22. cmp - Allows you to check if two files are identical.
23. comm - Combines the functionality of diff and cmp.
24. sort - Linux command to sort the content of a file while outputting.
25. export - Export environment variables in Linux.
26. zip - Zip files in Linux.
27. unzip - Unzip files in Linux.
28. ssh - Secure Shell command in Linux
29. service - Linux command to start and stop services.
30. ps - Display active processes.
31. kill and killall - Kill active processes by process ID or name.
32. df - Display disk filesystem information.
33. mount - Mount file systems in Linux.
34. chmod - Command to change file permissions.
35. chown - Command for granting ownership of files or folders.
36. ifconfig - Display network interfaces and IP addresses.
37. traceroute - Trace all the network hops to reach the destination.
38. wget - Direct download files from the internet.
39. ufw - Firewall command.
40. iptables - Base firewall for all other firewall utilities to interface with.
41. apt, pacman, yum, rpm - Package managers depending on the distro.
42. sudo - Command to escalate privileges in Linux.
43. cal - View a command-line calendar.
44. alias - Create custom shortcuts for your regularly used commands.
45. dd - Majorly used for creating bootable USB sticks.
46. whereis - Locate the binary, source, and manual pages for a command.
47. whatis - Find what a command is used for.
48. top - View active processes live with their system usage.
49. useradd and usermod - Add new user or change existing users data.
50. passwd - Create or update passwords for existing users.

Ls: This command is used to list the contents of a directory.


if you run ls -l, it will show the permissions, owner, size, and last modified date for each file
in the directory.
Sudo: This command executes only that command with superuser privileges. 2. Sudo:
This command executes only that command with superuser privileges.

Pwd: This command will print your directory location, where you currently working.

Cat: This is used to concatenate and display files on the terminal. It can also be used
to modify existing ones.

cat -b: This adds line numbers to non-blank lines.

cat -n: This adds line numbers to all lines.

cat -s: This squeezes blank lines into one line.

cat –E: This shows $ at the end of the line.

Vim: This is a text editor used in Linux. It stands for “Vi Improved”.
Mostly used modes in VIM:

Normal mode: This is the default mode in which vim starts. In normal mode, you can
use various commands to navigate and edit the text.

Insert mode: In insert mode, you can type text into the file. To enter insert mode,
press the "i" key. To exit insert mode and return to normal mode, press the "Esc" key.

Command mode: In command mode, you can enter commands to perform various
actions, such as saving the file or quitting vim. To enter command mode, press the ":"
key.

Grep: This command searches for a particular string/ word in a text file. This is similar to
“Ctrl+F” but executed via a CLI.

Sort: This command is used to sort the results of search either alphabetically or numerically.
It also sorts files and directories.
sort -r: the flag returns the results in reverse order.
sort -f: the flag does case-insensitive sorting.
sort -n: the flag returns the results as per numerical order.
Tail: This command prints the last N number of data of the given input. By default, it prints
10 lines.
We can specify the number of lines, that we want to display.

Chmod: This command is used to change the access permissions of files and directories.
For example: Following “chmod” command will give the user permission to read, write and
execute a file.
Chown: This command is used to change the file Owner or group.

Here, below the ownership of “Chetan.txt” file got changed to root.


Ping – This command will ping a host and check if it is responding.
Lsof: It is used to display a list of all the open files on a Linux system.

ID: This is used to find out user and group names and numeric ID’s (UID or group ID)
of the current user or any other user in the server.

Syntax: id <option> <user_name>

Find: This is used to find files and directories and perform subsequent operations on
them.

In the below command, It will search in the present working directory and its
subdirectories, and print the name of the file that have “.txt” file extension.
File Permissions in Linux
Q1. How to create file permissions?
Q2. How to Check the Permission of Files in Linux
Q3. What are the three permission groups in Linux?
Q4. What are the three kinds of file permissions in Linux?
Q5. Reading the Security Permissions in Linux
Q6. How to Change Permissions in Linux
Let’s dive deeper one by one,
Q1. How to create file permissions?
It will set the boundaries for User, Group, Other User by setting up the only required
permission to them so that the any file or any directories will not be affected.

r (Read) w (Write) x (Execute) Permissions


0 0 0 0 ---
1 0 0 1 --x
2 0 1 0 -w-
3 0 1 1 -wx
4 1 0 0 r--
5 1 0 1 r-x
6 1 1 0 rw-
7 1 1 1 rwx

Q2. How to Check the Permission of Files in Linux


“ ls – l “

What is the meaning of this following line.


1. First character ‘-‘, which means it’s a file ‘d’, which means it’s a directory.
2. The next nine characters (drwxr-xr-x) show the security.
3. The next column shows the owner of the file.
4. The next column shows the group owner of the file.
5. The next column shows the size of the file in bytes.
6. The next column shows the date and time the file was last modified.
7. Last Column = File_name or Directory_name. (For example, here are: prac, snap, test,
example)
Q3. What are the three permission groups in Linux?
Look at those nine characters as three sets of three characters. Each of the three “rwx”
characters refers to a different operation you can perform on the file.
Owners: These permissions apply exclusively to the individuals who own the files or
directories.
Groups: Permissions can be assigned to a specific group of users, impacting only those within
that particular group.
All Users: These permissions apply universally to all users on the system, presenting the
highest security risk. Assigning permissions to all users should be done cautiously to prevent
potential security vulnerabilities.

rwx rwx rwx


user group other

Q4. What are the three kinds of file permissions in Linux?


Three kinds of file permissions in Linux Read, write, and execute.

Letter Definition
r “read” the file’s contents.

w “write”, or modify, the file’s contents.


x “execute” the file. This permission is given only if the file is a program.

Symbols: `+`, `-` and `=`Option in Linux File Permission


Operators Definition
`+` Add permissions
`-` Remove permissions
`=` Set the permissions to the specified values

Q5. Reading the Security Permissions in Linux


For example: “drwxr-xr-x
“rwx“: the first three characters `rwx`. This means that the owner of the file can “read” it
(look at its contents) and “write” it (modify its contents) and execute it
“r-x” : the second set of three characters “r-x”. This means that the members of the group can
only read and execute the files.
“r–x“ : The final three characters “r-x” show the permissions allowed to other users who have
a UserID on this Linux system. This means anyone in our Linux world can execute the files’
contents.

Q6. How to Change Permissions in Linux?


The command you use to change the security permissions on files is called “chmod“, which
stands for “change mode” because the nine security characters are collectively called the
security “mode” of the file.

How do I change file permissions in Linux using the command line?


To change file permissions in Linux, you can use the `chmod` command followed by the
desired permission settings.
For example: If we want to grants read, write, and execute permissions to the owner, and read
and execute permissions to the group and others.
“chmod 755 filename”

Advanced Shell scripting


Q. Write a bash script create_directories.sh that when the script is executed with three
given arguments (one is the directory name and second is start number of directories
and third is the end number of directories) it creates a specified number of
directories with a dynamic directory name.
Solution:
Here's a script named create_directories.sh that creates a specified number of directories
with dynamic directory names using loops:
#!/bin/bash
# Check if the number of arguments is not equal to 3
if [ "$#" -ne 3 ];
then echo "Usage: $0 <directory_prefix> <start_index> <num_directories>" exit 1
fi
# Extracting arguments
directory_prefix="$1"
start_index="$2"
num_directories="$3"
# Loop to create directories
for (( i=start_index; i<start_index+num_directories; i++ ));
do # Generating dynamic directory name
directory_name="${directory_prefix}_${i}"
# Check if the directory already exists
if [ -d "$directory_name" ];
then echo "Directory '$directory_name' already exists. Skipping."
else # Create the directory mkdir "$directory_name"
echo "Created directory: $directory_name"
fi
done
Save this script in a file named create_directories.sh, and then make it executable using the
following command:
chmod +x create_directories.sh
Now, you can execute the script with three arguments: the directory prefix, the start index,
and the number of directories you want to create. For example:
./create_directories.sh movie 1 10
This command will create 10 directories with names movie 1 to movie 10. Adjust the
arguments as needed.
Similarly you can do ./create_directories.sh day 1 90

Q. write a Script to backup your file.

Below is a simple script named backup.sh that can be used to backup your files. It will create
a timestamped directory and copy all files in the current directory to that backup directory.

#!/bin/bash

src_dir="/c/Users/csuryawa/chandu"

trg_dir="/c/Users/csuryawa/Backupfile"
backup_filename="backup_$(date +"%Y-%m-%d_%H-%M-%S").tar.gz"

echo "Backup started"

echo "backing upto $backup_filename ..."

tar -czvf "${tgt_dir}/${backup_filename}" "$src_dir"

echo "Backup completed"

Now, you can run this script whenever you want to back up your work.

Counting characters, words & lines in the file:


Take three variables, one for counting characters, words, and lines respectively. We use ‘wc’
command, which stands for word count and counts the number of characters and words as
well. For counting lines, we pass ‘grep ‘which keeps count of the lines that match a pattern.
Then we print out each variable.

You might also like