0% found this document useful (0 votes)
62 views

Mastery On Linux

Linux commands cheat sheet

Uploaded by

files.public95
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Mastery On Linux

Linux commands cheat sheet

Uploaded by

files.public95
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Linux Mastery

General Linux

Linux Root Directory System

home :→ This is the home directory, it contains all the user specific folders.

home/<userName> :→ This is a directory under home directory which contains .bashrc &
.profile files for respective users.

bin :→ Contains all the binary executable files.

lib :→ Contains all the packages, modules and libraries.

tmp :→ All temporary files generated by system.

sbin :→ All system related binary files.

To exit from second terminal window we have to press q .

General Information
Get help of any command :- man <command-name>

Show System Date :- date

Show uptime of the system :- uptime

To get the currently logged in Username :- whoami

Show mounted drives :- mnt

List all the running process :- top

Kill any process using PID :- kill PID <ID>

Check distribution version :- lsb_release -a

Keyboard Shortcut

Clear terminal screen :- ctrl + L or clear


Strop a command execution :- ctrl + C

Logout from the current user account or end session :- ctrl + D or exit

Go to start of a line :- ctrl + A

Go to end of a line :- ctrl + E

Keyboard Shortcut for nano editor


Open with nano editor :- nano file.ext
Save a file :- ctrl + O , press enter to check allow the file name.
Exit from file :- ctrl + X

User Management

Add a new user :- sudo adduser <username> . It add a new user. After adding user we can
add that user to "sudo" group to give that user sudo privilege.

Add a new user to a specific group :- sudo adduser <userName> <groupName> . It adds the
user to the mentioned group.

Remove a user :- sudo deluser --remove-home? <userName> it delete the mentioned user
with it's home directory (Optional).

Add a new user group :- sudo addgroup <groupName> it add a new group.

Remove a user group :- sudo delgroup <groupName> it removes the mentioned group.

Activate root user mode :- sudo -i , it activates the root user mode which changes the $
sign to # sign. This command works only if a password is set for root user.

Login as a specific user (Switch User) :- su <userName> , It switches the user.

Set password for a user :- passwd <userName>

Operate with root privilege from a user :- sudo ...rest of the command

User & file Permissions


Format - user-group-others => wrx-wrx-wrx
Read permission - r = 4
Write permission - w = 2
Execute permission - x = 1
Change permission - chmod<UGO> <dir/file> Example- chmod777 myFile.ext
Change ownership - chown <ownerUserName> <dir/file> Example- chown root myFile.ext

Add any package


To add any package we have three way -

From app store sudo apt <packageName>


From a repository curl <package source url>
From downloaded tar file - unzip the tar file.

Tar zip
Make a tar zip from a source folder - tar -cvf <filename.tar> <SourceFolder>.
Unzip a tar zip file - tar -xvf <filename.tar>

How to recover forgotten password of wsl distribution ?

Set default user to root from CMD. ubuntu<version without dot> config -–default-
user root

Go to Linux terminal and change password for target user. passwd <userName>
Set default user to target user from CMD. ubuntu<version without dot> config -–
default-user <userName>

How to change hostname

sudo nano /etc/wsl.conf and add [network]\n hostname = hostName\n


generateHosts = false to the file.

sudo nano /etc/hosts and replace all the occurrence of old host name with new
one.
Terminate ubuntu instance from CMD > wsl -t <instanceName>
Relaunch after 8 seconds.

File and Folder operations

Navigate through directory


Show current working directory path - pwd
To change directory - cd /absolute/path/from/root
Directly go to root folder - cd /
Directly go to relative_root(parent) folder - cd ./ , useful when working from mounted
drive.
Directly go to home directory - cd ~

/ root directory | . Currently working directory | ~ Home directory | ./ Currently working


root directory.

To work with space or dash containing file/folder name we should enclose it within a
quote.

Listing content of directory


ls [options?] /<targetDir?>
Options
-l Show Long list.
-a Show all including hidden.
-r Show in reverse order.
-1 Show one per line.
-A Show all excluding hidden dot files.

Operations

command [options?] /folder/file

[options]
-r recursive
-f force

Create a folder - mkdir /<dirName>

Remove a folder - rmdir /<dirName> if non empty :- rm -rf /<dirName>

Create a file - touch /<file.ext>

Remove a file - rm /<file.ext>

Copy folder & file - cp /<src_file_folder> /<dest_file_folder>

Move folder & file - mv /<src_file_folder> /<dest_file_folder>

Show a file content as output - cat /<file.ext> or less /<file.ext>

Command line operators


; → Run one command after another.

&& → Run second command if first command is successful.

|| → Run second command if first command is un-successful.

& → Sends a process/script/command to the background.

| → Standard output of first command is input of second command.

> file → stdout of first command stored into the file.

>>file → stdout of first command appended into the file.

<file → First command takes input from a file.

() → Grouping operator to set precedence.

{} → Grouping operator to.

Schedule cron job


Can automate system maintenance, disk space monitoring, and schedule backups.

List out all the available cron-tab for current user:- crontab -l .

List out all the available cron-tab for any user:- crontab -u <userName> -l . To do this
must have root privilege.

Add a new crontab job using any editor for current user :- crontab -e

Add a new crontab job using any editor for any user:- crontab -u <userName> -e To do
this must have root privilege.

Delete a crontab : crontab -r

CRON JOB FORMAT

Minute Hour MonthDay Month WeakDay /directory/command what-after-execution-of-cron-


job?

Special string syntax @when /path/command Example- @daily /command.sh

@when @hourly, @daily, @midnight, @weekly, @monthly, @yearly, @reboot


Cron job's output automatically sent to an local email to stop that we can use >/dev/null
2>&1 command on output section

To set custom email address :- add MAILTO = "[email protected]" at the top of cron file.

Find file and folder


find <startDir> -name <fileName> -o? name <anotherFile> -empty? -type? D/F

Starts finding from startDir directory, name to search for , for multiple name used -o, checks if
empty, finding type directory or file.

Search word from a file


grep -?win "searchWord" /<fileName> -w → Word search | -i → Case insensitive search | -
n → Show line number.

You might also like