Basic Linux Commands to Navigate Server
Managing a Linux-based server is a complex task that involves using theShell command-line interface on a regular basis. This guide will provide you with a list of shell commands for navigating the server. This list will include the most commonly used commands for your daily management tasks.
All of the commands below will require you to be logged in to your server via SSH.
Please note that logging on as the “root” user will guarantee that all the commands below will work. If you log on as a different user, you might lack permissions to execute some of the commands, or the output might not display part of the content (i.e. files with different owner) |
List Directory Content
1 |
The ls command lists directory content. Usually the command is enhanced with the switch -lah which provides output in a user-friendly manner and displays hidden files as well.
Example:
#ls -lah
Output: |
Change Working Directory
1 |
The cd command changes your working directory.
Example:
#cd /var/log
This would switch the current working directory to /var/log.
|
Display Working Directory
1 |
The pwd command notifies you of your current directory.
Example:
#pwd
Output: |
Create directory
1 |
The mkdir command creates a new directory.
Example:
#mkdir dev
This will create a directory dev within your current working directory. Please note that the owner of that directory will be the user who created it. |
Copy file or directory
1 |
The cp command copies a file or directory.
Example 1:
#cp test test_bak
This would copy the file test to a file test_bak within the same directory. To copy files between directories, include the path to the original file and the copy.
Example 2:
#cp -R testdir testdir2
The “-R” option forces the system to recursively copy the entire directory structure and contents. It is mandatory to include this switch whenever you are copying directories that have content. |
Move file or directory
1 |
The mv command moves a file or directory. The mv command also is used for renaming items.
Example 1:
#mv test ../test
This would move a file test one level up from the current working directory.
Example 2:
#mv testdir testdir2
This would rename the directory “testdir” to “testdir2” within the same working directory. |
Delete file or directory
1 |
The rm command removes files or directories.
Example 1:
#rm test
This would remove the file test. You will be prompted to confirm the deletion.
Example 2 :
#rm -rf somedir
This would remove the directory somedir including all of its contents, and will not ask for confirmation. |
Change ownership of a file or directory
1 |
The chown command changes ownership of a file or directory.
Example 1:
#chown max test
This would change the owner of file test to system user max.
Example 2 :
#chown -R max:team somedir
This would change the owner and group of the directory somedir to a system user max, who is part of the system group team. The change will be followed recursively for any files and directories within somedir. |
Check file type
1 |
The file command is used to check a file type.
Example:
#file access_log
Output: |
Read content of a text file
1 |
The cat command prints the text content of a file.
Example:
#cat test
This would print all the text lines in the file test as output. |
Edit text file
1 |
There are multiple text editors with different functions available in Linux. However, most of them need to be installed as a separate package. The tool available with every Linux distribution is called vi.
Example:
#vi test
This would open the text file test for editing. If such a file does not exist, vi will create it in your current working directory. IMPORTANT: Vi is a complex and powerful Linux tool that comes with its own very specific working syntax. In order for you to use it properly, we would suggest you to check this page: |
Search for string in a text file
1 |
The grep command displays lines in text that contain a specific string.
Example:
#grep 11.22.33.44 access_log
This would print out the lines from the text file access_log that contain the string “11.22.33.44”. This tool is very useful when examining log files. |
Please note that the above tutorial only scratches the surface. Most of the commands described above have multiple options, or switches, that can be used, which makes them very useful in specific scenarios. In order for you to check all the options for a specific command, you can type: <command> --help |