DEV Community

John Au-Yeung
John Au-Yeung

Posted on • Originally published at thewebdev.info

Useful Linux Commands — Password and Permissions

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
Enter fullscreen mode Exit fullscreen mode

to count the number of lines in test.txt .

We count the number of words in the file with:

wc -w test.txt
Enter fullscreen mode Exit fullscreen mode

And we tet the number of bytes in the file with:

wc -c test.txt
Enter fullscreen mode Exit fullscreen mode

The -m flag gets the correct byte value.

open

The open command lets us open files, directories, and programs.

We run

open <filename>
Enter fullscreen mode Exit fullscreen mode

to open a file.

It also works with directory.

We can open the current directory with:

open .
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Then we can change the permission by running:

chown user test.txt
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Or we can change the owner to a group with:

chown <owner>:<group> <file>
Enter fullscreen mode Exit fullscreen mode

So we can run:

chown bob:users test.txt
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Conclusion

We can change file permissions, count file sizes, and change passwords with some Linux commands.

Top comments (0)