Common Linux commands
Note:
To execute a command, enter the command with any options or arguments that it requires, and press Enter.
The following are some basic Linux commands and their functions:
1. ls
List files and directories that exist within your current directory. This command resembles the dir command in Windows®.
To view dotfiles (filenames that begin with a period) and additional file and directory details, add the -al options to the command:
ls -al
2. cd location
Navigate between directories.
Replace location with the path to the directory that you wish to navigate to. For example, to navigate to the /usr/local/apache/directory, run the following command:
cd /usr/local/apache/
3. cat filename
Print the contents of the specified file to the CLI.
Replace filename with the relative path to the file that you wish to view. For example, to print the contents of the filename.txt file, run the following command:
cat filename.txt
Note:
To view the data that currently displays on your server's console screen, run the following command:
cat /dev/vcs1
4. tail filename
Print the last 20 lines of a file to the command line interface (CLI).
Replace filename with the relative path to the file that you wish to view. For example, to print the last 20 lines of the filename.txtfile, run the following command:
tail filename.txt
You can add an argument to change the number of lines that this command prints. For example, to print the last 100 lines of the filename.txt file, run the following command:
tail -100 filename.txt
5. more filename
Print the contents of a file to the CLI, one screen at a time.
Replace filename with the relative path to the file that you wish to view. For example, to print the contents of the filename.txt file one screen at a time, run the following command:
more filename.txt
6. vi filename
Open the specified file in the vi text editor.
Replace filename with the relative path to the file that you wish to edit. For example, to open the filename.txt file in the vi editor, run the following command:
vi filename.txt
7. pico filename
Open the specified file in the pico text editor.
Replace filename with the relative path to the file that you wish to edit. For example, to open the filename.txt file in the pico editor, run the following command:
pico filename.txt
8. grep string filename
Search for a string in a specified file, and prints each line that contains a match to the CLI.
Replace string with a single word, or multiple words within single quotes (''). Replace filename with the relative path to the file that you wish to search. For example, to search for the string coffee filters in the grocerylist.txt file, run the following command:
grep 'coffee filters' grocerylist.txt
9. touch filename
Create an empty file in the specified location.
Replace filename with the relative path to the file that you wish to create. For example, to create an empty example.txt file, run the following command:
touch example.txt
10. ln -s file1 file2
Create a symbolic link between the two specified files.
Replace file1 with the relative path to the existing file, and file2 with the relative path to the new symbolic link file. For example, to create the symlink-file.txt file and point it to the /pointtome/file.txt file, run the following command:
ln -s /pointtome/file.txt symlink-file.txt
11. rm filename
Delete the specified file. After you run this command, the system prompts you to confirm the file's deletion.
Replace filename with the relative path to the file that you wish to delete. For example, to delete the trash.txt file, run the following command:
rm trash.txt
12. last
List which users recently logged in and the timestamp for each login.
13. w
List currently logged-in users and the location from which they logged in.
14. netstat
List all of the server's current network connections.
15. file filename
Guess a file's type, based on the file's contents.
Replace filename with the relative path to the file for which you want the system to guess the type. For example, to cause the system to guess the type for the example.txt file, run the following command:
file filename
16. du
Show the system's current disk usage for each directory and subdirectory.
17. wc filename
Display the word count for a specific file.
Replace filename with the relative path to the file for which you wish to view a word count. For example, to display a word count for the example.txt file, run the following command:
wc example.txt
18. cp file1 file2
Copy a file into a new file.
Replace file1 with the relative path to the existing file, and file2 with the relative path to the new copy file that you wish to create. For example, to copy the contents of the original.txt file to the /copies/duplicate.txt file, run the following command:
cp original.txt /copies/duplicate.txt
19. chmod permissions filename
Change a file's octal permissions.
Replace permissions with the three-digit octal permissions that you wish to grant to the file, and replace filename with the relative path to the file for which you wish to alter the permissions. For example, to change the permissions of the myfile.txt file to 755, run the following command:
chmod 755 myfile.txt
20. chown user:group filename
Change a file's user and group ownership.
Replace user with the user to whom you wish to grant ownership of the file, group with the group name, and filename with the relative path to the file. For example, to grant the user joe in the group joesgroup ownership of the joesfile.txt file, run the following command:
chown joe:joesgroup joesfile.txt
21. whereis name
Query applications that match the name value.
You can find the most common applications in the following locations:
/usr/sbin/sendmail/usr/bin/perl/bin/mail/usr/bin/php
22. ps
Return information about the server's current processes.
To view all of the running processes, run one of the following commands:
ps -auxww
ps -cef
