Basic lnux commands
Basic lnux commands
Creating a new file in Linux is straightforward, but there are also some surprising and
clever techniques.
Prerequisites
Access to a command line/terminal window (Ctrl–Alt–F2 or Ctrl–Alt–T)
A user account with sudo privileges (optional for some
files/directories)
Here are a few commands for creating a file directly from the command line.
touch test.txt
This creates a new empty file named test.txt. You can see it by entering:
ls
The ls command lists the contents of the current directory. Since no other
directory was specified, the touch command created the file in the current
directory.
If there’s already a file with the name you chose, the touch command will
update the timestamp.
This symbol tells the system to output results into whatever you specify
next. The target is usually a filename. You can use this symbol by itself to
create a new file:
> test2.txt
Note the redirection operator. Typically, the command displays the contents
of test2.txt on the screen. The redirection operator > tells the system to
place it in the test2.txt file.
ls
The system should now have test.txt, test2.txt, and test3.txt in the list.
ls
You should see the test4.txt file added to the list. Use the cat command to
display the contents of the new file:
cat test4.txt
The system should display Random sample text (or whatever you entered
with the echo command.)
To add two lines of text, separate each line with the \n option:
You can use the cat command on either of these files to display their
contents.
Note: To use several terminal instances in a single window manager,
consider using Linux screen. It enables additional features and an enhanced
command line for working with Linux files.
Vi Text Editor
Vi is the oldest text editor in Linux. It was created alongside the Linux
operating system for directly editing text files. Since it’s unlikely you’ll see a
Linux distribution without it, it’s a safe editor to know.
vi test7.txt
Your screen will change. Now you’re in the text editor. Press the letter i to
switch to insert mode, then type a few words to try it out.
vim test8.txt
This screen will look similar to the Vi editor screen. Press i to insert text, and
type a few words. Save file and exit by entering:
nano test9.txt
By default, Nano puts you directly into editing mode. It also displays a helpful
list of commands at the bottom of the screen.
Enter some text, then press Ctrl+O to save the changes.
In this article, we will talk about how to create new user accounts
using the useradd command.
useradd Command
The general syntax for the useradd command is as follows:
useradd [OPTIONS] USERNAME
Copy
Only root or users with sudo privileges can use the useradd command
to create new user accounts.
For example to create a new user named username you would run:
sudo useradd usernameCopy
When executed without any option, useradd creates a new user
account using the default settings specified in
the /etc/default/useradd file.
To be able to log in as the newly created user, you need to set the
user password. To do that run the passwd command followed by the
username:
sudo passwd usernameCopy
You will be prompted to enter and confirm the password. Make sure
you use a strong password.
Changing password for user username.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
Copy
The command above creates the new user’s home directory and
copies files from /etc/skel directory to the user’s home directory. If
you list the files in the /home/username directory, you will see the
initialization files:
ls -la /home/username/Copy
drwxr-xr-x 2 username username 4096 Dec 11 11:23 .
drwxr-xr-x 4 root root 4096 Dec 11 11:23 ..
-rw-r--r-- 1 username username 220 Apr 4 2018 .bash_logout
-rw-r--r-- 1 username username 3771 Apr 4 2018 .bashrc
-rw-r--r-- 1 username username 807 Apr 4 2018 .profile
Copy
Within the home directory, the user can write, edit and delete files
and directories.
By default, when a new user is created, the system assigns the next
available UID from the range of user IDs specified in
the login.defs file.
The -g (--gid) option allows you to create a user with a specific initial
login group. You can specify either the group name or the GID
number. The group name or GID must already exist.
The -s (--shell) option allows you to specify the new user’s login
shell.
Use the chage command to verify the user account expiry date:
sudo chage -l usernameCopy
System users are created with no expiry date. Their UIDs are chosen
from the range of system user IDs specified in the login.defs file,
which is different than the range used for normal users.
You can verify that the default shell value is changed by running the
following command:
sudo useradd -D | grep -i shellCopy
SHELL=/bin/bash
Copy
Conclusion
We have shown you how to create new user accounts using
the useradd command. The same instructions apply for any Linux
distribution, including Ubuntu, CentOS, RHEL, Debian, Fedora, and
Arch Linux.
useradd is a low-level utility, Debian and Ubuntu users can use the
friendlier adduser command instead.