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"
We can also use it with < to get input from it and sort it with sort :
sort <(printf "1\n3\n2")
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
And we can redirect to stderr with:
echo "stderr" >&2
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
to add the foo user.
userdel
userdel lets us delete the user.
For example, we run:
`sudo` userdel `foo`
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
to add the foo group.
groupdel
groupdel lets us delete a group.
We can run:
sudo groupdel foo
to delete a group.
cmp
cmp gets the byte difference between 2 files.
For instance, we run:
cmp a b
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
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/
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
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:~
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/* .
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/* .
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.
Top comments (0)