Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62
Subscribe to my email list now at http://jauyeung.net/subscribe/
Linux is an operating system that many developers will use.
Therefore, it’s a good idea to learn some Linux commands.
In this article, we’ll look at some useful Linux commands we should know.
wc
The wc
command lets us count lines, words, or bytes.
For instance, we run:
wc -l test.txt
to count the number of lines in test.txt
.
We count the number of words in the file with:
wc -w test.txt
And we tet the number of bytes in the file with:
wc -c test.txt
The -m
flag gets the correct byte value.
open
The open
command lets us open files, directories, and programs.
We run
open <filename>
to open a file.
It also works with directory.
We can open the current directory with:
open .
passwd
The passwd
command lets us change a user's password.
We run it to get a prompt to enter the current password and the new password we want to set.
We can also run:
passwd <username> <new password>
to set a user’s password when we’re logged in as a superuser.
chmod
chmod
lets us change file permissions.
To change it, we run chmod
with the following letters:
-
a
stands for all -
u
stands for user -
g
stands for group -
o
stands for others
Then we type in +
to add a permission or -
to remove it.
Then we enter one or more permission symbols:
-
r
— read -
w
— write -
x
— execute
For instance, we run:
chmod a+r filename
to add the read permission to filename
for all users.
We can add permissions for multiple roles by adding multiple letters before the +
or -
:
chmod og-r filename
Now we remove the read permission to filename
for other and group.
We can apply permissions recursively with the -r
flag.
We can also set file permissions with numbers.
It can be one of the following:
-
0
no permissions -
1
can execute -
2
can write -
3
can write, execute -
4
can read -
5
can read, execute -
6
can read, write -
7
can read, write and execute
Then we run:
chmod 777 filename
We set the permission for user, group, and others with the digits.
7 is created by adding 1, 2, and 4 together.
This gives everyone full permission to work with filename
.
chown
We can change the owner of a file or directory with the chown
command.
The general format is:
chown <owner> <file>
Then we can change the permission by running:
chown user test.txt
We change the owner of test.txt
to user
.
Also, we can change the permission of all files and directories recursively with the -R
flag:
chown -R <owner> <file>
Or we can change the owner to a group with:
chown <owner>:<group> <file>
So we can run:
chown bob:users test.txt
to change the owner to bob
in the users
group for test.txt
.
We can also change the group of a file with the chgrp
command:
chgrp <group> <filename>
Conclusion
We can change file permissions, count file sizes, and change passwords with some Linux commands.