Day-3 Basic Linux Commands
To View what's written in the file.
You can use the
cat
command to view the contents of a file in the terminal. The basic syntax is:
-
To change the access permission.
You can use the
chmod
command to change the access permissions of files. Thechmod
command allows you to modify the read, write, and execute permissions for the owner, group, and others. Here's the basic syntax:
In Linux, file permissions control the extent of a file's accessibility. Permissions can be classified into three categories: read, write, and execute. The owner of the file, the group to which it belongs, and all other users are the three groups to which these rights are set.
Read (r): A user can access a file's contents if they have read privilege.
Write (w): A user with write permission can edit a file's contents. They have the ability to edit, remove, or add content.
Execute (x): Program or script files are those with the executable permission. A user can run a file as a program or script if they have the execute permission for it.
Let's now discuss the three groups:Owner: The individual who originated the file is this. They can modify the file's permissions and have complete control over it.
Group: Every file is a part of a specific group. The permissions for that file are shared by all members of that group. All members of the "staff" group, for instance, can read and write to a file if it is owned by the group and has read and write permissions.
Others: This describes everyone else who isn't the file's owner or a part of the group to which it belongs. Usually, these are individuals with no special relationship to the file.
Permissions are frequently displayed as a string of characters or digits, such as "rwxr-x---". The permissions of the owner are represented by the first three characters, those of the group by the next three, and those of others by the final three.
For instance, "rwxr-x---" denotes:
The file can be executed, read, and written by the owner.
The file can be read and used by the group.
Others don't even have authorization.
Controlling who can access and alter your files as well as their security can be achieved by managing their permissions. It's essential to preserving your data's integrity and privacy on a Linux system.
To check which command you have run till now.
The
history
command is used to display a list of previously executed commands in a terminal session. It's a handy tool for quickly recalling and reusing commands you've used in the past without having to retype them. This can be especially useful when you're working on the command line and need to refer back to commands you've used before.To remove a directory or a folder.
To remove a directory (folder) in Linux, you can use the
rmdir
orrm
command, depending on whether the directory is empty or contains files.
To create a fruit.txt file and to view the content inside it.
Creating the
fruits.txt
fileThe .touch command can be used to create a new file, which will be an empty file with the given name:
To add contents on fruits.txt.
The touch command can be used to start a new file, which will be an empty file with the given name:
The provided content is printed using the echo command in this command. The -e option allows backslash escapes (\n for a new line, for example) to be interpreted. By redirecting the output to the devops.txt file using the > operator, the file is essentially created and its contents are added.
Alternatively, we may use the following instructions to add the desired content, one item on a separate line, to a file called devops.txt:
To create the file.
If the
devops.txt
file doesn't exist, you can create it using thetouch
command-
Add the following lines in the text editor:
Press
Ctrl
+O
to save the changes andCtrl
+X
to exitnano
To display just the file's top three fruits.
We may use the head command to show only the top three fruits from the devops.txt file. To view a file's top or first few lines, use the head command.
-
This command will display the first three lines of the devops.txt
file, which correspond to the top three fruits.
The -n
option specifies the number of lines to display, and 3
is the number of lines you want to show.
This will give you the desired output of showing only the top three fruits from the devops.txt
file.
\> To show only the bottom three words.
Use the tail command to see just the first three fruits from the devops.txt file. To see a file's last few lines, or bottom lines, use the tail command.
\> This command will display the last three lines of the devops.txt
file, which corresponds to the bottom three fruits:
Thank You for reading this Article.
Subscribe to my newsletter
Read articles from Jatin Virmani directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by