In this tutorial, we will explain some of the most useful and frequently used Linux commands, along with some examples to help you understand their usage.
The general structure of a Linux/UNIX command line looks like:
command [-flag(s)] [-option(s) [value]] [argument(s)]
List the files and directories in the current working directory
ls
Gives you a long listing of all files including hidden files
ls -la
Prints the current working directory
pwd
Changes your directory path helping you navigate through your directories
cd
Move one level up from the current directory
cd ..
Creates directories
mkdir
Used for copying files and directories
cp
Copy single file index.php to destination directory bak:
cp index.php bak
Used for renaming and moving files and folders
mv
mv old_filename new_filename
mv directory_name /path/to/destination/
Used to delete files and folders
rm
Recursive removal and allows removal without confirmation
rm -rf <directory or file>
Creates an empty file
touch <file>
Clears the terminal window
clear
Prints the contents of the file
cat
View the content of multiple files with line numbers
cat -n file1 file2
Used to display the manual for a specific command. The basic syntax is as follows:
man [command]
Returns the current user’s username
whoami
Searches for specific strings in one or more files
grep
grep -rnw “.” -e “string”
The dot (.) represents the current directory in the filesystem.
-r is recursive
-n is line number
-w stands for match the whole word
Establishes a secure shell connection to a remote server
ssh
ssh user@host -p 22
-p port to connect to on the remote host
Add your IP Address in place of host
Add your username in place of user
Used to display the amount of disk space used and available on the file systems
df
Prints sizes in a human-readable format
df -h
Used to change the permissions of a file or directory
chmod
chmod <permissions> <file>
Used to change the ownership of a file or directory
chown
chown new-user:new-group file-name
Used to download a file from within the terminal
wget
To download a single file from a URL
wget http://example.com/sample.php
Used for commands that you’ve defined
alias
The basic syntax for alias is as follows:
alias shortname=’longer command’
System monitoring tool that displays system processes
top
Creates a new user
useradd username
Changes the password for the current user
passwd
Gives you information about your network connections, the ports that are in use, and the processes using them.
netstat
netstat -tunlp
-t tcp connections
-u udp connections
-n numeric
-l show only listening sockets
-p show the PID and the name of the program to which each socket belongs
The zip command compresses one or multiple files into a zip archive
zip zip_file_name file1 file2
Extract a compressed file into your current working directory
unzip zip_file_name
Enables non-root users who are part of the sudo group to execute administrative commands
sudo your_command
Lets you switch to another user in the Terminal session
su
Used to test the network connectivity of a website or another computer
ping [hostname/IP address]
Allows you to print the last few lines from files
tail [options] file_name
Prints the last 10 lines
tail -n 10 filename
Allows you to print the first few entries of a file
head [options] file_name
Prints the first 10 lines
head -n 10 filename
Lets you open a file for editing with the default Linux editor (visual editor)
vi file_name
Lets you open a file for editing with the editor called Nano
nano file_name
Replaces a string in a file
sed ‘s/old_string/new_string/g’ file_name
Allows you to count the number of lines of each given file and print the result. The -l option prints the number of lines.
wc -l
Linux package managers generally include a clean command. DNF is a package manager for CentOS. The following can be a helpful command if you are having package errors due to corrupted metadata.
dnf clean packages
Uninstall a package that you no longer need on your CentOS system
dnf remove packagename