How to create files in a directory in a Linux server
There are different ways to create and edit files on a Linux server.
In a Linux server, creating environment files, e.g., .env or .ini files, and configuration files (settings.py) can seem daunting as you do not have access to the window management system (GNOME) present in a desktop environment.
However, you can access a powerful tool on a Linux server called the Terminal.
You can use the Terminal to create and edit files on a Linux server. The Terminal allows you to use utilities and applications like echo and nano to create and edit files. On top of that, you may install an optional graphical-rich application, gedit, to create and edit new files.
With nano, touch, and gedit, you have three alternatives for creating and editing files on the Linux server.
Use echo
Use nano
Use gedit
How to use nano to create and edit files in a Linux server
Creating files and editing them using the nano is the most preferred and straightforward way on a Linux server.
Using nano is very straightforward.
You need to type nano followed by the file name you want to create or edit.
Besides, you may specify the file’s location that you want to edit.
Here’s how to create a file using the nano command:
nano file.txt
You should see a similar window close to the one below.
Type your code.
To save the file, press CTRL + O
After you’re done editing and saving your file, you can exit the window by pressing CTRL + X
You will find these commands at the bottom of the nano window or by typing nano –help after closing the window.
To edit an existing file on your Linux file system, specify the file name if you’ve navigated into the file’s location or specify the file location.
When you’ve navigated into the file location:
nano file.txt
Specifying file location
nano /etc/file.txt
How to create files in a Linux server using echo
echo is another command you may use to create files faster without opening a new window. You just type the word echo followed by the code in single or double quotes, greater than sign, and finally, the file name.
Here’s an example
echo “new line of code” > file.txt
In the code above, we’re creating a new file called file.txt, with the first line of code being ‘new line of code’ in it.
You should see the text inside if you view the file’s contents.
cat file.txt
You may use the /n escape character to create a new line.
The command below will create a file.txt file with two lines of code
echo “first line of code \nsecond line of code” > file.txt
To create a tab, you may use the /t escape character.
echo "def new_function(): \n\tpass" > file.py
Here, I am using \n to create a new line and \t to create a tab line.
As you know, valid Python functions have such syntax. That is how you would create such files using the echo command without having to use a graphical window mode.
Besides, you may create a new file in another location that you may not be currently navigated into.
For example, to create a file in the HOME directory ~ and you’re not navigated into it, you need to specify the location with the echo command.
Here’s how.
echo "def new_function(): \n\tpass" > ~/file.py
NOTE:
You cannot use the echo command to edit existing files because it will overwrite the existing code or text inside the file. I prefer to use the echo command to create new files only. If you want to edit a file, use nano or gedit commands.
Using the echo command approach to create files is fast and easy as you can create files on the go.
If you prefer a more graphical interface for creating and editing files on a Linux Terminal or server, then, gedit is the perfect tool.
How to use gedit to create and edit files on a Linux server
Gedit is an application for creating and editing files on a Linux machine. The easiest way to open the gedit applications is using the Terminal. The best graphical application for editing files when logged into a Linux server is gedit.
However, gedit may not come preinstalled on your Linux server instance.
You must use the apt package manager to install the application.
To install gedit, type the following when you’re logged into your Linux server instance or in the Terminal:
sudo apt update && sudo apt install gedit
After installation, you may use the application to create new files and edit existing files on your Linux server.
Type the following to open a new gedit window
gedit
Hit Enter and a new window should pop up.
With the window open, you can write your code and press the save button on the right-hand side.
You will find the Open button on the left-hand side to open an existing file.
If you want to create a new file using the Terminal, add the file’s name at the end of the gedit command.
Here’s an example of creating a new file, file.txt, using the gedit command.
gedit file.txt
After adding the code to your file, press the save button to create a new file inside the directory you’re currently navigated into.
To edit an existing file on your Linux file system, specify the name of the file if you’re navigated into the location of the file or specify the file location.
When you’ve navigated into the file location:
gedit file.txt
Specifying file location
gedit /etc/file.txt
Creating and editing files on a Linux server or Linux Terminal is as easy as that.
You have three options for creating files: using nano, using echo, or using the gedit application.
I prefer nano as it is pretty close to a graphical interface and does not need to install a new application on my Linux server.
In addition to the commands above, there are times you may want to create just an empty file without adding to it any code or text.
How to create empty files on your Linux server file system using touch
You should opt for the touch command to easily create empty files on a Linux file system.
You may use the touch command to create new files without having to add anything to the files. Also, you can use the command to create a file when you are unsure whether it exists.
Although the main purpose of touch is to update a file’s access and modification times, you can use it to create files in directories where they must be existent easily.
For example, you don’t need to add anything to the file to store error logs. Besides, the file must be present for the system to record the logs. Thus, creating an empty file will make sense here.
So, here’s how to create new files using touch.
touch newfile.txt
The command will run successfully whether the file exists or not.
Other times, you may get Permission Denied alerts when you edit files that require administrative privileges.
How to create a file in the root directory in Linux
The commands we have executed earlier use a regular user account you have logged in to. Such an account does not have administrative privileges, and creating or editing files inside the root directories leads to Permission Denied alerts.
Try this.
Open the Terminal or log into your server instance using SSH.
Type the following:
cd /etc/ && touch file.txt
See, you get the permission denied alert: touch: cannot touch ‘file.txt’: Permission denied
If you use nano, you may see an alert indicating that the Directory ‘.’ is not writable.
You cannot create files in a root ‘/’ directory as a regular user.
Creating or editing files inside the root directory or any other directory that requires administrator privileges will not work with nano, echo, touch, and gedit commands.
The reason is that you must run these applications and utilities (nano, touch, echo, gedit) using an account with elevated permissions such as the sudo so that you can have the permissions to access, read, and write files in such directories.
An account with elevated permissions such as the sudo has administrative privileges allowing it to view, create, edit, and delete any file or directory within the whole Linux system.
What is sudo?
SUDO means superuser do. SUPERUSER in Linux identifies the privilege level allowing you to make changes to files that require administrative access. Thus, adding sudo to a command allows you to run the command or application in a superuser do mode with elevated privileges.
Fortunately, creating and editing a file in the root directory is very straightforward. You need to add sudo before you type in the command to create or edit a file.
So, if you want to create a file called file.env using sudo, type in sudo nano file.env
You should not get a permission denied alert.
Otherwise, you will get an error (Error writing file.env: Permission denied). The reason is that you must have administrator privileges to create files inside the root directory.
Pro tip: How to execute commands with administrative permissions without typing sudo in every command.
Let’s say you have too many successive commands that require you to use sudo. Well, you would have to type every command with the word sudo. It can be time-consuming.
An alternative is to switch to a privileged user, and running commands with administrative privileges will be automatic. You do not need to type the word sudo on every command, and the command will run with administrative privileges.
Log into your Linux server using SSH.
Type
sudo su
The command above allows you to change to a root account that has administrative privileges. The root user has all the permissions and control over everything on your Linux server.
With that, you can run any command, and it will run using sudo.
Try this:
cd /etc/ && touch file.txt
The above command should run successfully even without using sudo before the command.
You can run as many commands with elevated permissions in the current instance.
After completing the administrative tasks or commands that require administrative privileges, you may want to switch back to a regular account.
To switch back to a regular account, you just need to type, su your_username
For example, on an Ubuntu server, you would type
su ubuntu
If you’re using the username, hoofhoof, you will type
su hoofhoof
su means switch user.
Another way to switch back to a regular account is to log out of the current instance.
After logging out of your Linux server, the instance Terminal will have the regular user permissions, and commands requiring administrative privileges must be pre-appended with the word sudo.
And that’s it basically for this tutorial.
See you next time, hearties!