50 useful Linux Commands – Part 2
Hello everyone, in this two part tutorial, we will learn some very basic and useful Linux commands. So lets get started. For Part-1, check here
- Check command history : “history” command is used viewing all the previous command.
history
- Search in command history: Lets say you want to search a specific keywords in previously runned commands. then you have to run:
history | grep search_term
- Write in a file: You can write a text in a file by running below command
echo "Hello world!" > hello.txt
Note: This command will create a file named “hello.txt” inside “Hello World!” written in it.
- Create new file: For creating a new file, below command is used: eg: touch filename.filetype
touch test.txt
- Install new package: For install new package, apt install command is used. eg: apt install package_name
apt install git apt install ftp
- Remove package: To remove an installed package, you have to run:
apt purge *git*
Note: cp is copy command, then file name, finally directory where you want to copy the file
- Start a service: If you want to run/start any service, you have to run below command: service service_name start
eg: service apache start
- Start a service at start up: To start or run a service in startup, you have to use “systemctl” command
systemctl enable service_name
Note: eg: systemctl anable ssh
- Stop any service: To stop any service, you have to run
service service_name stop
Note: eg: service apache2 stop
- Stop any startup service: To disable a startup service, you have to run:
systemctl disable service
- Check Service Statuses: Check service all service status :
service --status-all
Note: Running service will have “+” sign
- Restart a service: To restart any service:
systemctl restart service_name
- Check service status: If you want to check any service status, run:
systemctl status service_name
Note: You have to run this command as super admin
- Reload a service: If you want to reload any service without restarting it, only reload the configurations, run below command
systemctl reload service_name
Note: After running the command, you have to provide user name, password and password confirmation
- Reload or restart: If you are not sure wheather to reload or restart, run below command, it will check itself if new configuration to load or to restart the service
systemctl reload-or-restart service_name
Note: Here we reading the “/etc/passwd” file, which contains all users info.
- Change permission: “chmod” command is use to provide specific access to a file or folder. Afile may have read (r), write(w) or execute (x) permission. If you want to give execute permission to a file, you have to run:
chmod +x script
Note: Be careful of this file. Do not give full access to this file for security reason.
- Exit terminal: For exit the current shell terminal, you have to run:
exit
Note: Such as, if you want to switch to user jhon, run “su john“
- Turn off machine: If you want to turn off the machine, just run:
shutdown
Note: The default shutdown time is one minute, if you want to shutdown immediately, type: “shutdown now“
- Unzip a zipped folder: To unzip/extract the contents of any zip folder, you have to run below command:
unzip zipped_folder_name.zip
Note: It will provide Interface Configuration related data
- Show Processes: If you want to check running processes, you should use “htop” command. It may not be installed by default. Then just install it by typing “y”
htop
- Terminate any process: If any app is unresponsive, you can use “kill” command with process ID or application binary name. Such as:
kill chrome kill process_id
- Remove a file and make it unrecoverable: To remove any file and make it unrecoverable, you have to run below command:
shred file_name.extension
Note: Be cautious before running this file
- Print current user: To print current user, just run:
whoami
- Get location of any app: Get location of any app, run “where” command. eg:
where php
- Get word count of a text file:To get word count of a text file, run below command::
wc text_file.txt
Note: cp is copy command, then file name, finally directory where you want to copy the file
- Update Packages list and Upgrade Packages: If you want to update all the packages list and also upgrade all the packages in one single command, you have to run
apt update && apt upgrade