Linux Commands
Linux Commands
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:
Command: ls
This will list the files and directories in the current directory.
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.
Command: ls -r
This will list the files and directories in the current directory in reverse order.
Command: ls -S
This will list the files and directories in the current directory sorted by size, with the
largest files first.
Command: ls -R
This will list the files and directories in the current directory and all subdirectories
recursively.
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).
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:
The rm command is used to remove files or directories in Linux. Here are some
examples:
1)Remove a file:
This command will delete the file filename.txt in the current directory.
This command will delete the files file1.txt, file2.txt, and file3.txt in the current directory.
Command : rm -r directory_name
The -r flag stands for "recursive" and tells rm to delete the directory and its contents
recursively.
The -f flag stands for "force" and tells rm to remove the directory and its contents
without
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.
The -r flag stands for "recursive" and tells cp to copy the directory and its contents
recursively to a
The -p flag stands for "preserve" and tells cp to preserve the original file attributes (such
as
The mv command in Linux is used to move files or directories from one location to
another, and also
This command will move the file file1.txt to the directory specified by
/path/to/new/location/.
2)Rename a file:
/path/to/new/location/.
4)Rename a directory:
The cat command in Linux is used to concatenate files and display the output on the
terminal.
This command will display the contents of the file filename.txt on the terminal.
This command will concatenate the contents of file1.txt and file2.txt and display the
output on the
terminal.
This command will concatenate the contents of file1.txt and file2.txt and display the
output on the
terminal.
This command will append text to the end of the file existingfile.txt. Once you have
entered the text,
expression in a file or a set of files. The tool is primarily used for searching text
Sample.txt
Hello, world!
To search for the pattern "sample" in the file, we can use the following
command:
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
Sure, here are some examples of using the grep command to search for a pattern in a
file:
2)Ignore case: search for a pattern "hello" in a file named "file.txt" ignoring the case:
3)Search recursively: search for a pattern "hello" in all files in a directory and its
subdirectories:
4)Search for whole words: search for a whole word "hello" in a file named "file.txt":
6)Search for multiple patterns: search for multiple patterns "hello" and "world" in a file
named
"file.txt":
7)Invert match: search for lines that do not match a pattern "hello" in a file named
"file.txt"
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
1) Grant read, write, and execute permissions to the owner, and read and execute
permissions to
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 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
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
2) Grant read, write, and execute permissions to the owner, and read and execute
permissions to
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:
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
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
4) Grant read and write permissions to the owner and group, and read-only permissions
to others:
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)
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
chmod -R +x directory
The -R option stands for "recursive", which applies the permission change to the
directory and its
subdirectories.
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
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:
Therefore, the command chmod 777 sets the file permissions to rwxrwxrwx, or read,
write, and
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
VI EDITOR COMMANDS
1) vi filename: Opens the specified file in Vi editor.