DEV Community

loading...

Useful Linux Commands — Users, Groups, and Remote Connections

aumayeung profile image John Au-Yeung Originally published at thewebdev.info ・3 min read

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.

printf

printf is an improved version of the echo command.

It lets us format and adds escape sequences:

printf "1\n3\n2"
Enter fullscreen mode Exit fullscreen mode

We can also use it with < to get input from it and sort it with sort :

sort <(printf "1\n3\n2")
Enter fullscreen mode Exit fullscreen mode

0 / 1 / 2

0, 1, and 2 are the standard input, output, and error streams, respectively.

For instance, we can redirect to stdout with:

echo "stdout" >&1
Enter fullscreen mode Exit fullscreen mode

And we can redirect to stderr with:

echo "stderr" >&2
Enter fullscreen mode Exit fullscreen mode

users

The users command shows all the users currently logged in.

To see all users on the system, we can check /etc/passwd .

useradd

To add a user to the system, we run useradd .

For instance, we run:

sudo useradd foo
Enter fullscreen mode Exit fullscreen mode

to add the foo user.

userdel

userdel lets us delete the user.

For example, we run:

`sudo` userdel `foo`
Enter fullscreen mode Exit fullscreen mode

to delete the foo user from the system.

groups

groups show all the groups of which the current user is a member.

We can see all groups in the /etc/group .

We shouldn’t change /etc/group unless we know what we’re doing.

groupadd

groupadd lets us add groups into our system.

We can run:

sudo groupadd foo
Enter fullscreen mode Exit fullscreen mode

to add the foo group.

groupdel

groupdel lets us delete a group.

We can run:

sudo groupdel foo
Enter fullscreen mode Exit fullscreen mode

to delete a group.

cmp

cmp gets the byte difference between 2 files.

For instance, we run:

cmp a b
Enter fullscreen mode Exit fullscreen mode

to get the byte difference between files a and b .

cut

cut lets us cut a line into sections on some delimited.

The -d flag lets us specify a delimiter.

-f specifies the field index to print.

For instance, we run:

printf "117.99.234.23" > c
cut -d'.' c -f1
Enter fullscreen mode Exit fullscreen mode

to print 117 onto the screen.

sed

sed lets us replace a string with another string in a file.

For instance, we runL

echo "old" | sed s/old/new/
Enter fullscreen mode Exit fullscreen mode

to remove old with new .

ssh

ssh lets us connect from one machine to another machine.

We run:

ssh –p <port> bob@1.2.3.4
Enter fullscreen mode Exit fullscreen mode

to connect to machine with IP address 1.2.3.4 with user bob .

scp

We can use scp to copy a file securely from one machine to another.

For instance, we can run:

scp –P <port> hello bob@1.2.3.4:~
Enter fullscreen mode Exit fullscreen mode

to copy the hello file from the machine with IP address 1.2.3.4.

rsync

The rsync utility lets us copt files with the least amount of data possible being transmitted.

This is done by looking at changes between files.

For instance, we run:

rsync -av ../s/* .
Enter fullscreen mode Exit fullscreen mode

to sync the files from the ../s directory with the current directory with rsync .

rsync also works over ssh .

We run:

rsync -avz -e "ssh -p <port>" bob@1.2.3.4:~/s/* .
Enter fullscreen mode Exit fullscreen mode

to copy the data over to the machine with IP 1.2.3.4.

Conclusion

We can print files, manipulate users and groups, and connect to other machines with some Linux commands.

Discussion (0)

pic
Editor guide