Linux commands for hosting: learn the essentials
Working via the command line is essential for server management. Whether you are managing WordPress, analyzing logs, or moving files - the right Linux commands make your work more efficient. At Theory7, we regularly provide SSH access to clients who want to leverage this power. In this guide, you will learn the most important commands.
Navigation commands
Show current location
pwd
This command shows the full path to your current directory. It is handy to know where you are in the file hierarchy, especially when working on multiple projects.
Show contents of directory
ls
With more details:
ls -la
What the options mean:
- -l - Long format with details, such as file permissions, owner, size, and date of last modification.
- -a - Also show hidden files (files that start with a dot).
Change to another directory
cd /var/www/html
Handy shortcuts:
- cd ~ - Go to your home directory.
- cd .. - One level up in the directory structure.
- cd - - Go to the previous directory.
Managing files and folders
Create a file
touch bestand.txt
This command creates a new, empty file. This is handy for quickly creating files that you want to edit later.
Create a folder
mkdir nieuweMap
Including parent directories:
mkdir -p pad/naar/diepe/map
Using the -p option ensures that if the parent directories do not exist, they will also be created.
Copying
File:
cp bron.txt doel.txt
Folder with contents:
cp -r bronmap doelmap
The -r argument stands for 'recursive' and is necessary when copying directories.
Moving/renaming
mv oudnaam.txt nieuwenaam.txt
mv bestand.txt /andere/locatie/
With the mv command, you can both move and rename files. This is a handy way to keep your files organized.
Deleting
File:
rm bestand.txt
Folder with contents:
rm -r mapnaam
Note: rm is permanent. There is no recycle bin, so be careful with this command.
Viewing files
Show complete content
cat bestand.txt
This command shows the full content of a file in the terminal. It is handy for quickly viewing text files.
First lines
head -20 bestand.txt
This shows the first 20 lines of a file. This is useful for quickly getting an overview of the content.
Last lines
tail -50 bestand.txt
This shows the last 50 lines of a file. This can be handy for log files where you want to see the most recent activities.
Follow log file in real-time
tail -f /var/log/apache2/error.log
With the -f option, you can follow a file in real-time. This is particularly useful for monitoring log files while making changes.
Browse file
less bestand.txt
With less, you can browse a file without fully loading it into memory. Navigation in less:
- Space - Page forward.
- b - Page back.
- / - Search.
- q - Quit.
Editing files
Nano (beginner-friendly)
nano bestand.txt
Nano is a simple text editor that is ideal for beginners. Commands are at the bottom:
- Ctrl+O - Save.
- Ctrl+X - Quit.
- Ctrl+W - Search.
Vim (advanced)
vim bestand.txt
Vim is a powerful text editor but has a steeper learning curve. Basic Vim:
- i - Insert mode (typing).
- Esc - Return to command mode.
- :w - Save.
- :q - Quit.
- :wq - Save and quit.
Permissions and ownership
View permissions
ls -la bestand.txt
The output shows information about file permissions, owner, group, size, and date of last modification. This helps you understand who has access to the file.
Change permissions
chmod 755 script.sh
chmod 644 bestand.txt
Common values:
- 755 - Executable for owner, readable for others.
- 644 - Read/write for owner, readable for others.
- 600 - Only owner may read and write.
Change owner
chown gebruiker:groep bestand.txt
Recursively for folders:
chown -R www-data:www-data /var/www/html
This is important for correctly setting file permissions, especially on web servers where the web server needs access to certain files.
Searching
Search for files
find /var/www -name "*.php"
Search for files with a specific extension. This is handy for developers who want quick access to their code files.
Search for files larger than 100MB:
find / -size +100M -type f
Search text in files
grep "zoekterm" bestand.txt
Recursively in all files:
grep -r "zoekterm" /var/www/
Case-insensitive:
grep -i "zoekterm" bestand.txt
This is particularly useful when debugging applications or searching for specific configurations.
System info
Disk space
df -h
This command shows the available and used disk space on all mounted files. This is crucial for server management, so you don't encounter unexpected storage issues.
Memory
free -h
Provides an overview of RAM and swap memory usage. This helps in monitoring your server's performance.
Running processes
top
Or better:
htop
htop provides a more user-friendly interface for managing processes and viewing system information.
Active services
systemctl status apache2
This command shows the status of a specific service, such as Apache. This is important for managing web servers and troubleshooting.
Compression
Create ZIP
zip -r backup.zip mapnaam/
This command creates a ZIP file from a folder. This is useful for archiving files or making backups.
Unzip ZIP
unzip backup.zip
This command unzips a ZIP file to the current directory.
Create TAR.GZ
tar -czvf backup.tar.gz mapnaam/
This is a commonly used method for archiving files on Linux systems.
Unzip TAR.GZ
tar -xzvf backup.tar.gz
This command unzips a TAR.GZ file. This is handy for restoring files from an archive.
Network
Test connectivity
ping google.com
This command tests network connectivity with an external server. This is a basic diagnostic tool for network issues.
View ports
netstat -tlnp
Or modern:
ss -tlnp
These commands show active network connections and the ports used by processes.
Download file
wget https://example.com/bestand.zip
Or with curl:
curl -O https://example.com/bestand.zip
These tools are essential for downloading files from the internet to your server.
Useful combinations
Search and count
grep -r "error" /var/log/ | wc -l
This command counts the number of times a specific term appears in log files, which is useful for debugging.
Find largest files
du -ah /var/www | sort -rh | head -20
This command helps you identify the largest files in a directory, which is useful for managing disk space.
Find and stop process
ps aux | grep apache
kill -9 PID
This is a handy way to manage processes and terminate unwanted processes.
Related articles
- Root access and sudo
- SSH connection from Mac/Linux
- VPS first steps after purchase
- Monitor server resources
More information about VPS servers at Theory7
Need help?
We are here for you! Are you facing any issues or do you have questions? Our support team is happy to assist you personally. Send us a message via the ticket system - we usually respond within a few hours and are happy to help you.
0 van 0 vonden dit nuttig