0% found this document useful (0 votes)
8 views10 pages

Linux Commands

The document provides a comprehensive overview of essential Linux commands including 'ls', 'mkdir', 'rm', 'cp', 'mv', 'cat', 'grep', 'chmod', and basic 'vi' editor commands. Each command is explained with examples demonstrating its usage for listing files, creating and removing directories, copying and moving files, searching text, changing file permissions, and editing files. The document serves as a practical guide for users to navigate and manipulate files and directories in a Linux environment.
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
0% found this document useful (0 votes)
8 views10 pages

Linux Commands

The document provides a comprehensive overview of essential Linux commands including 'ls', 'mkdir', 'rm', 'cp', 'mv', 'cat', 'grep', 'chmod', and basic 'vi' editor commands. Each command is explained with examples demonstrating its usage for listing files, creating and removing directories, copying and moving files, searching text, changing file permissions, and editing files. The document serves as a practical guide for users to navigate and manipulate files and directories in a Linux environment.
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/ 10

1) ls Command:

The ls command in Linux is used to list the contents of a directory. Here are some
examples of how to use the ls command:

2) To list the contents of the current directory:

Command: ls

This will list the files and directories in the current directory.

3) To list the contents of a directory with additional information:

Command: ls -l

This will list the files and directories in the current directory with additional information
such as file permissions, owner, size, and modification date.

4) To list the contents of a directory in reverse order:

Command: ls -r

This will list the files and directories in the current directory in reverse order.

5) To list the contents of a directory sorted by size:

Command: ls -S

This will list the files and directories in the current directory sorted by size, with the
largest files first.

6) To list the contents of a directory recursively:

Command: ls -R

This will list the files and directories in the current directory and all subdirectories
recursively.

7) To list the contents of a directory with hidden files:

Command: ls -a

This will list the files and directories in the current directory, including hidden files. (files
and directories whose names start with a dot).

MKDIR COMMAND AND ITS FLAVOURS

The mkdir command is used to create a new directory. To use mkdir, simply open a
terminal and type
mkdir followed by the name of the directory you want to create. Here are some
examples:

1) Create a new directory in the current directory:

Command :mkdir new_directory

2)Create a new directory in a specific location:

Command : mkdir /path/to/new_directory

3)Create multiple directories at once:

Command : mkdir directory1 directory2 directory3

Remove Directory Command and Its Flavours

The rm command is used to remove files or directories in Linux. Here are some
examples:

1)Remove a file:

Command :rm filename.txt

This command will delete the file filename.txt in the current directory.

2)Remove multiple files:

Command :rm file1.txt file2.txt file3.txt

This command will delete the files file1.txt, file2.txt, and file3.txt in the current directory.

3)Remove a directory and its contents:

Command : rm -r directory_name

The -r flag stands for "recursive" and tells rm to delete the directory and its contents
recursively.

4)remove a directory without prompting

Command :rm -r -f directory_name

The -f flag stands for "force" and tells rm to remove the directory and its contents
without

prompting for confirmation.

Copy Command and its Flavours

The cp command is used to copy files or directories in Linux. Here are some examples:

1)Copy a file:
Command :cp file1.txt file2.txt

This command will copy file1.txt to a new file named file2.txt in the same directory.

2)Copy a directory and its contents:

Command :cp -r directory1 directory2

The -r flag stands for "recursive" and tells cp to copy the directory and its contents
recursively to a

new directory named directory2

3)Copy a file to a di\erent directory:

Command :cp file1.txt /path/to/directory

This command will copy file1.txt to the directory specified by /path/to/directory.

4)Preserve file attributes:

Command :cp -p file1.txt file2.txt

The -p flag stands for "preserve" and tells cp to preserve the original file attributes (such
as

permissions and timestamps) when copying the file.

Move Command and its Flavours

The mv command in Linux is used to move files or directories from one location to
another, and also

to rename files or directories. Here are some examples:

1)Move a file to a new location:

Command : mv file1.txt /path/to/new/location/

This command will move the file file1.txt to the directory specified by
/path/to/new/location/.

2)Rename a file:

Command : mv oldname.txt newname.txt

This command will rename the file oldname.txt to newname.txt.

3)Move a directory to a new location:

Command :mv directory1 /path/to/new/location/


This command will move the directory directory1 and all its contents to the directory
specified by

/path/to/new/location/.

4)Rename a directory:

Command : mv oldname newname

This command will rename the directory oldname to newname

CAT COMMAND AND ITS FLAVOURS

The cat command in Linux is used to concatenate files and display the output on the
terminal.

Here are some examples:

1)Display the contents of a file:

Command : cat filename.txt

This command will display the contents of the file filename.txt on the terminal.

2)Concatenate two or more files

command :cat file1.txt file2.txt

This command will concatenate the contents of file1.txt and file2.txt and display the
output on the

terminal.

3)Create a new file:

command : cat > newfile.txt

This command will concatenate the contents of file1.txt and file2.txt and display the
output on the

terminal.

4)Append to an existing file:

command :cat >> existingfile.txt

This command will append text to the end of the file existingfile.txt. Once you have
entered the text,

press Ctrl+D to save the changes.

GREP COMMAND and its Flavours


grep is a command-line tool used to search for a specified pattern or regular

expression in a file or a set of files. The tool is primarily used for searching text

files and has many options to perform complex searches.

Here's an example of how to use grep:

Suppose we have a file called sample.txt with the following contents:

Sample.txt

Hello, world!

This is a sample file.

It contains some text.

To search for the pattern "sample" in the file, we can use the following

command:

This will output:

This is a sample file.

grep can also search for patterns in multiple files at once. For example, to

search for the pattern "sample" in all files with a .txt extension in the current

directory and its subdirectories, we can use the following command:

grep "sample" *.txt

Sure, here are some examples of using the grep command to search for a pattern in a
file:

1)Basic search: search for a pattern "hello" in a file named "file.txt":

Command: grep "hello" file.txt

2)Ignore case: search for a pattern "hello" in a file named "file.txt" ignoring the case:

Command :grep -i "hello" file.txt

3)Search recursively: search for a pattern "hello" in all files in a directory and its
subdirectories:

Command :grep -r "hello" /path/to/directory

4)Search for whole words: search for a whole word "hello" in a file named "file.txt":

Command : grep -w "hello" file.txt


5)Count matches: count the number of matches of a pattern "hello" in a file named
"file.txt":

Command : grep -c "hello" file.txt

6)Search for multiple patterns: search for multiple patterns "hello" and "world" in a file
named

"file.txt":

Command : grep "hello\|world" file.txt

7)Invert match: search for lines that do not match a pattern "hello" in a file named
"file.txt"

Command : grep -v "hello" file.txt

CHMOD COMMAND AND ITS FLAVOURS

chmod is a Linux/Unix command used to change the access permissions of files and
directories. It

can be used to grant or restrict read, write, and execute permissions to the owner of the
file, the

group of the file, and other users.

The basic syntax of the chmod command is:

chmod [permissions] [file/directory]

1) Grant read, write, and execute permissions to the owner, and read and execute
permissions to

the group and others:

Command : chmod 755 filename.txt

In the command chmod 755 filename.txt, the numbers "755" represent the file
permissions.

The first digit, "7", represents the permissions for the owner of the file. In this case, it
indicates that

the owner has read, write, and execute permissions (4 + 2 + 1 = 7).

The second digit, "5", represents the permissions for the group that the file belongs to. In
this case, it

indicates that the group has read and execute permissions (4 + 1 = 5).
The third digit, "5", represents the permissions for all other users. In this case, it
indicates that all

other users have read and execute permissions (4 + 1 = 5).

So, in summary, chmod 755 filename.txt sets the permissions for the file "filename.txt"
to allow the

owner to read, write, and execute the file, and allow the group and all other users to
read and

execute the file.

2) Grant read, write, and execute permissions to the owner, and read and execute
permissions to

the group, and no permissions to others:

Command : chmod 750 filename.txt

The command "chmod 750 filename.txt" sets the file permissions for the file
"filename.txt" to "rwxr-

x---".

The first digit, "7", sets the file permission for the owner of the file to "rwx" (read, write,
execute).

The second digit, "5", sets the file permission for the group owner of the file to "r-x"
(read, execute

only).

The third digit, "0", sets the file permission for all other users to no permission ("-").

3) Grant read and write permissions to the owner, and no permissions to the group and
others:

Command : chmod 600 filename.txt

In chmod 600 filename.txt, the number 600 represents the permissions being set for the
file

filename.txt in octal notation. In octal notation, each digit represents a permission level:
the first

digit represents permissions for the owner of the file, the second digit represents
permissions for the

group that the file belongs to, and the third digit represents permissions for everyone
else.
Here is the breakdown of chmod 600 filename.txt:

The first digit 6 means that the owner of the file has read and write permissions (4 for
read, 2 for

write, and 4+2=6).

The second and third digits, 0, mean that the group and everyone else have no
permissions (0 for no

permissions).

So chmod 600 filename.txt sets the file permissions to be read and write only for the
owner of the

file, with no permissions for anyone else.

4) Grant read and write permissions to the owner and group, and read-only permissions
to others:

Command : chmod 640 filename.txt

chmod 640 filename.txt means that the owner of the file has read and write permissions
(6), the

group to which the file belongs has read permission (4), and all other users have no
permissions (0)

to the file. In summary:

6: Read and write permission for the owner

4: Read permission for the group

0: No permission for others (all users who are not the owner or in the group)

This setting ensures that only the owner can modify the file, the group can read it, and
others have

no access to the file.

5) Grant execute permission to a directory and its subdirectories:

chmod -R +x directory

The -R option stands for "recursive", which applies the permission change to the
directory and its

subdirectories.

6) Revoke execute permission from a file:


chmod -x filename.txt

The -x option removes the execute permission from the file.

7)CHMOD 777

chmod 777 is a command used to change the file permissions of a file or directory.
Specifically, it sets

the file permissions to read, write, and execute for the owner, group, and all others.

The number 777 is a shorthand way of representing the file permissions. Each digit
corresponds to a

di\erent set of permissions:

The first digit (7) represents the file permissions for the owner of the file or directory

The second digit (7) represents the file permissions for the group associated with the file
or directory

The third digit (7) represents the file permissions for all other users who are not the
owner or in the

group.

Each digit can take on a value between 0 and 7, where 0 represents no permissions and
7 represents

full permissions. The values for each digit represent the sum of the permissions, where:

4 represents read permissions

2 represents write permissions

1 represents execute permissions

Therefore, the command chmod 777 sets the file permissions to rwxrwxrwx, or read,
write, and

execute for the owner, group, and all others.

It is generally not recommended to use chmod 777 as it gives all users complete control
over the file

or directory, which can pose a security risk. It is better to use more restrictive
permissions and only

give permissions to the users who need them.

VI EDITOR COMMANDS
1) vi filename: Opens the specified file in Vi editor.

2) i: Switches to insert mode, allowing text to be entered into the document.

3) Esc: Exits insert mode and returns to command mode.

4) :w: Saves the file without quitting Vi.

5) :q: Quits Vi without saving any changes.

6) :wq: Saves the file and quits Vi.

You might also like