There are a lot of things you can do from the command line. Correction. There are a lot of things you can only do from a command line. My main system is a MacBook Pro which supports all of the above commands; because it’s UNIX. Linux isn’t different. My second laptop runs Arch Linux with XFCE and Deepin. My hosting server is the cutting edge Fedora which I tinker around with everyday. So even I can’t remember how much I know Linux. But suffice it to say, these commands are the stepping stone to getting familiar with Linux.
1. Login to a remote system via ssh
ssh username@yourdomainorip -i full-path-to-ssh-privatekey-including-file-name
2. Clear the terminal
clear
3. Show current directory
pwd
4. List files in the current directory
ls -al
cd foldername
cd ..
7. Moving and renaming files
mv file destination
Example:
mv wp-config.php /var/www/html/
Rename works the same
mv .htaccess wp.htaccess
8. Running a privileged command
sudo commandname
9. Switch to different (super-)user
sudo su username
10. Switch to ‘root’ user
sudo su -
11. Installing software
Fedora / RHEL based systems
sudo dnf install packagename
On Debian based systems
sudo apt-get install packagename
12. Updating the Server Softwre
Fedora / RHEL based systems
sudo dnf update
On Debian based systems
sudo apt-get update
13. Monitoring system performance
top
htop
(usually not installed by default)
atop
(usually not installed by default)
14. Creating backups
Create a tar archive of a folder
tar cvjf archivename.tar.bz2 foldertobackup
Example
tar cvjf mybackup.tar.bz2 wp-content
15. Backing up or dumping a database
mysqldump -u username --password="insert your password here" databasename > database.sql
16. Restoring / Importing a database dump
mysql -u username --password="insert your password here" databasename < database.sql
17. Extracting archive
tar xvjf archivename.tar.bz2
18. Simple recursive copy
cp -rv folder destination
Efficient copying over network
rsync -arzv directory destination
If you want to try a dry run without changing anything, try:
rsync -arzvn directory destination
19. Managing Services
Checking the status of a service
systemctl status service
Example: check the status of apache
systemctl status httpd
Enabling a service
systemctl enable service
Example: enable apache to automatically start on boot
systemctl enable httpd
Disabling a service
systemctl disable service
Example: disable apache from automatically start on boot
systemctl disable httpd
Start a service
systemctl start service
Example: start apache manually
systemctl start httpd
Restart a service
systemctl restart service
Example: restart apache service
systemctl restart httpd
20. Finding help
Finding help for linux is very easy. However before you resort to Google, you can try the following commands:
Most commands in Linux support the –help argument
command --help
Look up the manual
man command
Commands helpful for coders
21. Searching for strings in files
Search for string in files (r to search recursively, n to show line numbers in files, the dot means in the current directory and sub-directories)
grep -rn "string to search for" .
Example: Search for function do_action. Comes in handy to find where a WordPress function is declared and to study the function code.
grep -rn "function do_action" .
22. Finding Files
find . -iname "filename"
Example, let’s find the location of the apache configuration file:
find . -iname "httpd.conf"
Output: (The second line shows the path of the found file)
[root@localhost /]# find . -iname "httpd.conf"
./etc/httpd/conf/httpd.conf
23. Getting header information from command line.
This one is very useful. I’ve seen that redirects in .htaccess are cached by browsers. And when I want to be absolutely sure that the browser is not redirecting anymore I use curl. It also comes in handy when troubleshooting headers and various server-level and SEO kind of geek stuff.
curl -I http://google.com
Notice the difference from the regular URL.
Output:
curl -I http://google.com HTTP/1.1 302 Found Cache-Control: private Content-Type: text/html; charset=UTF-8 Referrer-Policy: no-referrer Location: http://www.google.co.in/?gfe_rd=cr&dcr=0&ei=lll7Wvz3Eojy8AezhrLQAw Content-Length: 271 Date: Wed, 07 Feb 2018 19:55:02 GMT
It gives a 302 found temporary header and had various other information about server headers.
24. Downloading via command line
wget https://wordpress.org/latest.tar.gz
This downloads the resource to the current directory (WordPress archive in the above example).
I’d love to cover a lot about Linux because I’ve been using it for about 20 years now. However let’s just keep this one to the basics. Once you get a grasp on these, it will be easier to learn the advanced stuff like piping commands.
Oh and did I forget the very basic reboot?
sudo reboot
sudo shutdown now
Those are easy once you know them.
The next step is to play around with regular expressions—cryptic strings that have the superpowers to find, match, replace complex strings with literals or tokens. But that’s for another day. For now, boot up a live Linux distro and hack it out. It won’t hurt a thing and you’ll learn a trick or two which will last you your entire career.
25. Disk Usage
sudo df -h
26. File / Folder Size
Current directory
du -sh
Only the direct child of the current directory:
du -h --max-depth=1
And finally, don’t forget to Google.