This post is a compilation of some of the notes I made about Linux commands. I usually come back to it for reference. I think it could be helpful to others as well.
To quickly check if a program is installed on Linux, you can use the command
dpkg -s followed by the unique package name for that program. The
-s flag in dpkg stands for
search which allows you to search for a program on your machine and check if it’s installed.
dpkg -s firefox
sudo apt-get install <package_name> sudo apt-get install firefox sudo apt-get remove gimp
sudo apt-get update
Note: Repositories are basically servers which contain sets of packages.
We can use the
history command to see the history in the terminal or use
ctrl+r shortcut for the same.
Checking the Kernel version:
uname -r // see the kernel version
Copying, moving and removing files:
cp <this file> <to this directory> cp *.jpg <to this directory> // copy all jpg files mv <this> <that> // rename file mv <this> <directory> // move the file to the directory rm <file> // remove file
User and passwords:
passwd <user> // set new pass sudo passwd -e <user> // expire user’s password and ask them to set new pass next time they log in sudo useradd <name> // add user sudio userdel <name> // remove user
There are three ownership types:
- u = owner
- g = group
- o = all
We can modify file permissions by using the
chmod u+x filename // give x permission to owner type chmod u-x filename // remove x permission from owner type chmod u+rx filename // give rx permission to owner type
There are three permission types:
- read (r) = 4
- write (w) = 2
- execute (x) = 1
The alternate (and popular) way to write
chmod command is
chmod 754 <filename>, where:
7= giving read+write+execute (4+2+1) permission to
5= giving read+execute (4+1) permission to
4= giving read (4) permission to
Other two relevant commands:
sudo chown omkar filename // change owner of the file sudo chgrp best_group_ever filename // change group of the file
du -h // check disk usage, -h gives info in human readable form df -h // disk free command, -h gives info in human readable form fsck /dev/sdb // file system check command. Warning: If you run fsck on a mounted partition, there's a high chance that it'll damage the file system.
When you start up your computer, the kernel creates a process called a init, which has a PID of one. It starts up other processes that we need to get our computer up and running.
ps -x // snapshot of processes running on the system
Process statuses: R = running, T = stopped, S = interruptible sleep (task is waiting for an event to complete before it resumes).
ps -ef // e flag = all processes (even run by other users), f = full details. ps -ef | grep Chrome // search for chrome process
Killing a Process:
// SIGINT - Signal Interrupt (example control + c) kill <PID> // kill a process giving it time to clean up kill -KILL <PID> // this is SIGKILL cmd, i.e. kill without giving the time to cleanup