Skip to content

Instantly share code, notes, and snippets.

@ziadtawfeek
Last active May 14, 2021 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ziadtawfeek/d75b34a122871264dffdd94d6013b5c2 to your computer and use it in GitHub Desktop.
Save ziadtawfeek/d75b34a122871264dffdd94d6013b5c2 to your computer and use it in GitHub Desktop.
Getting started:
----------------
passwd : change user password
whoami : know which user are you logged-in
logout : logout from the system
init 0 : powers off the system using predefined scripts to synchronize and clean up the system prior to shutdown
reboot : reboot the system
shutdown : shutdown the system
.program name : invisible file is one whose first character is the dot or period character
uname : display the operating system information
date : display the date and time
cal : display the calendar
su - username : change to the directory to other user
Moving around the file system:
------------------------------
pwd : show the "present working directory", or current directory
cd : change current directory to your HOME directory
cd / : change a working directory within hierarchy of filesystem
cd .. : change current directory to the parent directory of the current directory
cd ~ : change current directory to the home directory
cd /etc/ : contains all system related configuration files
cd /proc/ : used as an interface to kernel data structures
sudo : allows a user with proper permissions to execute a command as super user
sudo -s : used to do actions as super user
command1;command2; : execute multiple commands
ln -s : create a symbolic link file
Listing directory contents:
---------------------------
ls : list a directory
ls -i : list your files including the inode numbers
ls -a : list all files and directories including hidden files and directories
ls -l : lists your files including the exact size of the file, who owns the file and the permissions, and when it was last modified
ls -R : lists the content of a directory and all its subdirectories
ls directory name : list the files in a directory
Changing file permissions and attributes:
-----------------------------------------
ls -l|grep keypair : check the permission of a keypair
chown user filelist : stands for "change owner" and is used to change the owner of a file
chgrp group filelist : stands for "change group" and is used to change the group of a file
chmod a+rw filename : give read and write permissions on the file to all users
chmod a-rw filename : take read and write permissions on the file to all users
chmod 400 filename : changes the permissions of file for the owner, and for the group and the world
umask 022 filename : affects and modifies the default file permissions assigned to the file or directory
The options of chmod are as follows:
------------------------------------
Symbol Meaning
u : user
g : group
o : other
a : all
r : read
w : write (and delete)
x : execute (and access directory)
+ : add permission
- : take away permission
Viewing and editing files:
--------------------------
cat filename -b : see the content of a file and display line number by using the option -b
cat /proc/cpuinfo : check CPU info
cat /etc/issue.net : check OS info
cat /proc/meminfo : check memory info
fdisk -l : check the fixed disks in hard drive
route -n : check default gateway
touch filename : create a filename
history : show ordered list of all the commands entered
file * : classifies the named files according to the type of data they contain, for example ascii (text), pictures, compressed data, etc..
echo file/variable : display a line of text
find filename : search for a filename in the system
cmp file1 file2 : compare the contents of two files byte by byte
comm file1 file2 : compare the contents of two sorted files line by line
spell filename : check text for spelling error
Archiving files:
----------------
compress filename : reduces the size of a file with format (.zip)
uncompress filename : uncompress the file with format (.zip)
gzip filename : zip the file and place it in a file format in smaller sizes (.gzip)
gunzip filename.gz : unzip the file with format (.gzip)
gzcat filename : displays files that were compressed with either the gzip or compress commands
tar -cf filename : create a new archive file and specify the archive file
zip trgtfile srcfiles : compresses and archives multiple files into a single file in one go
zcat : display a compressed file
Basics of the vi editor:
-------------------------
vi : open the default editor mode
vi filename : edit filename starting at line 1
vi -R filename : open an existing file in read only mode
view filename : opens an existing file in read only mode
K : moves the cursor up one line
J : moves the cursor down one line
H : moves the cursor to the left one character position
L : moves the cursor to the right one character position
Command mode in vi editor:
--------------------------
:w<Return> : write current contents to file
:w filename1<Return> : save the file as filename1
:q! : exit vi without saving any of the changes
ZZ or :wq : exit vi with saving the changes in file
<ESC> : terminate insertion or overwrite mode
:f filename : renames current file to filename
:w filename : write to file filename
:e filename : opens another file with filename
:cd dirname : changes current working directory to dirname
U : restore the current line to the state it was in before the cursor entered the line
U : undo the last change to the file. Typing 'u' again will re-do the change
:! ls : check whether a file exists before you try to save your file to that filename
:w >> filename : append the contents of a file to another file
:set nu : show line numbers
:set nonu : hold line numbers
Moving within a vi file:
------------------------
0 or | : positions cursor at beginning of line
$ : positions cursor at end of line
w : positions cursor to the next word
b : positions cursor to previous word
( : positions cursor to beginning of current sentence
) : positions cursor to beginning of next sentence
E : move to the end of Blank delimited word
{ : move a paragraph back
} : move a paragraph forward
[[ : move a section back
]] : move a section forward
n| : moves to the column n in the current line
1G : move to the first line of the file
G : move to the last line of the file
nG : move to nth line of the file
:n : move to nth line of the file
H : move to top of screen
nH : moves to nth line from the top of the screen
M : move to middle of screen
L : move to botton of screen
nL : moves to nth line from the bottom of the screen
:x : colon followed by a number would position the cursor on line number represented by x
Editing in a vi file:
---------------------
i : inserts text before current cursor location
I : inserts text at beginning of current line
a : inserts text after current cursor location
A : inserts text at end of current line
o : creates a new line for text entry below cursor location
O : creates a new line for text entry above cursor location
Deleting in a vi file:
----------------------
x : deletes the character under the cursor location
X : deletes the character before the cursor location
dw : deletes from the current cursor location to the next word
d^ : deletes from current cursor position to the beginning of the line
d$ : deletes from current cursor position to the end of the line
D : deletes from the cursor position to the end of the current line
Changes in a vi file:
--------------------
cc : removes contents of the line, leaving you in insert mode
cw : changes the word the cursor is on from the cursor to the lowercase w end of the word
r : replaces the character under the cursor, vi returns to command mode after the replacement is entered
R : overwrites multiple characters beginning with the character currently under the cursor. You must use Esc to stop the overwriting.
s : replaces the current character with the character you type. Afterward, you are left in insert mode
S : deletes the line the cursor is on and replaces with new text. After the new text is entered, vi remains in insert mode
nP : recover a recent deletion where n is up to 3 characters
Copy and paste in a vi file:
----------------------------
Yy : copies the current line
Yw : copies the current word from the character the lowercase w cursor is on until the end of the word
P : puts the copied text after the cursor
P : puts the yanked text before the cursor
Search in a vi file:
--------------------
/word : searches forwards (downwards) in the file for word
?word : searches backwards (upwards) in the file for word
fc : move forward to c
Fc : move back to c
:s/search/replace/g : search for the word 'search' and replace the word with 'replace'
n : searches for the next occurrence of the string after searching command for a string
N : searches for the previous occurrence of the string after searching command for a string
Basics of the nano editor:
--------------------------
nano filename : opens a new text file in nano editor
Ctrl + O : saves a file, 'O' stands for output
Ctrl + X : exits the nano program, 'X' stands for exit
Ctrl + G : opens a help menu
nano ~/.bash_profile : opens a file used to store environment settings
source ~/.bash_profile : activates the changes in a file for the current session
alias var="command" : allows you to create keyboard shortcuts, or aliases, for commonly used commands
export USER="var" : makes the variable to be available to all child sessions initiated from the session you are in to persist across programs
Moving, renaming, and copying files:
------------------------------------
. : means current directory
cp srcfile destfile : copy a directory/file from source to destination or the content of a directory/file from source to destination
cp . : copy a file to the current directory
cp -r srcdir destdir : copy a directory from source to destination
mv file1 newname : rename a file
mv file1 ~/AAA/ : move file1 into sub-directory AAA in your home directory
mkdir directory name : make a new directory
mkdir dir1 dir2 : make directories (dir1 and dir2)
mkdir /dir/sub-dir : creates the directory sub-dir in the directory
rmdir dir1 dir2 : remove an empty directory
Displaying the contents of a file on the screen:
------------------------------------------------
clear : clear all text and leave you with the % prompt at the top of the window
less filename : writes the contents of a file onto the screen a page at a time
head filename : writes the first ten lines of a file to the screen
tail filename : writes the last ten lines of a file to the screen
more filename : display data in paginated form
[space-bar] : if you want to see another page
[q] : if you want to quit reading
Searching the contents of a file:
---------------------------------
less filename/[word] : search for a word after using the command less
grep science filename : searches files for specified words (e.g. science) or patterns
grep -i word filename : -i used to ignore upper/lower case distinctions
grep -i 'pattern' file : used to search for a phrase or pattern, you must enclose it in single quotes
grep -R directory : search within a directory
wc -w filename : count words in the file
wc -l filename : count letters in the file
sed 's/find/replace/' : search the file for the first instance of the word "find" and replaces it with "replace"
sed 's/find/replace/g' : search the file for the word "find" and replaces it with "replace" globaly
Redirecting the output:
-----------------------
> : redirect the output of a command
>> : appends (adds) the output (content) to the file on the right
cat > list : create a file called list then type any values for the file
Ctrl D : used to stop the command
Redirecting the input:
----------------------
< : redirect the input of a user
sort < filename : used to alphabetically or numerically sorts a list
sort -n filename : used to sort numerically a list
sort -f filename : used to sort upper and lower-case together
sort -r filename : used to reverse the order of sort
Pipes:
------
who : used to see who is on the system with you
who | sort : used to see who is on the system with sorted users
cat filename | uniq : filters out adjacent, duplicate lines in a file
command1 | command2 : pipe the output of command1 to the input of command2 often referred to as filter
Wildcards:
----------
ls list* : list all files in the current directory starting with list
ls *list : list all files in the current directory ending with list
ls ?list : list all files that have one character starting with list
Getting help:
-------------
man command : read the online manual page for a command, which options a particular command can take, and how each option modifies the behaviour of the command
apropos keyword : match commands with keyword in their man pages when you are not sure of the exact name of a command
Processes and Jobs:
-------------------
jobs : list current jobs
kill %number of job : kill job number
ps : list current processes
sleep seconds : waits a given number of seconds before continuing
Running the software:
---------------------
bin : The binary executables needed to run a program
info : GNU info formatted documentation
man : Man pages
share : Shared data files
./program name : Run the program name in the current directory
Environment Variables:
----------------------
USER : your login name
HOME : the path name of your home directory
HOST : the name of the computer you are using
ARCH : the architecture of the computers processor
DISPLAY : the name of the computer screen to display X windows
PRINTER : the default printer to send print jobs
PATH : the directories the shell should search to find a command
VARIABLE_NAME=value : set a variable name and assign it to 'value' without using the $ sign
$VARIABLE_NAME : access the variable name with the $ sign
PS1 : defines the makeup and style of the command prompt
env : returns a list of the environment variables for the current user
Printing Files:
---------------
nroff filename : formats documents for display or line-printer
pr -NUM filename : format file on screen to produce NUM-number of columns
lp/lpr -nNUM filename : print file with NUM-number of copies
lpstat/lpq -o : shows what's in the printer queue
cancel printername : terminates a printing request from the lp command
lprm printername : terminates lpr requests
Background Processes:
---------------------
command & : execute the command in a background process
ps -f : reports a snapshot of the status of currently running processes
top : updates frequently and shows information about physical and virtual memory, CPU usage, load averages, and your busy processes
nohup command [arg] : runs a command, making it immune to any HUP (hangup) signals (e.g. nohup ./file.sh &)
Networking commands:
--------------------
curl URL : open the url
wget URL : download file over a network
apt-get [install] : used to install packages on your system
yum : perform system updates, installation of new packages, removal of old packages and perform queries on the installed and/or available packages
ssh -i keypair root@IP : allows you to connect and log in to a specified host
scp [user@host1:]file [[user@]host2:]file2 : copies files securely between hosts on a network
scp srcfile user@host:directory/trgtfile : copy a file from local directory to a remote host
scp user@host:/directory/srcfile trgtfile : copy a file from remote host to a local directory
mail [-s subject] [-c cc-addr] [-b bcc-addr] to-addr : send mail to the specified address
Unix Communication:
-------------------
ping URL/IP : used to test whether another system is reachable over a network
ftp host : used to to transfer files to and from a remote network site
ftp>open : opens a connection with another computer on the network
ftp>put filename : upload filename from local machine to remote machine
ftp>get filename : download filename from remote machine to local machine
ftp>mput file list : upload more than one files from local machine to remove machine
ftp>mget file list : download more than one files from remote machine to local machine
ftp>prompt off : turns prompt off, by default you would be prompted to upload or download movies using mput or mget commands
ftp>prompt on : turns prompt on
ftp>Dir : list all the files available in the current directory of remote machine
ftp>cd dirname : change directory to dirname on remote machine
ftp>lcd dirname : change directory to dirname on local machine
ftp>Quit : logout from the current login
telnet host : allows a computer user at one site to make a connection, login and then conduct work on a computer at another site
finger host/user : displays information about users on a given host either local, remote or specifying a certain user
Unix Shell:
-----------
#!/bin/sh : alert the system that a shell script is being started
# comment : put comments in a script
./file.sh : execute the shell script
var_name=var_value : define variables
$var_name : access variable value
readonly variable_name : mark variables as read-only and its value cannot be changed
unset variable_name : tells the shell to remove the variable from the list of variables that it tracks
File System:
------------
/ : the root directory which should contain only the directories needed at the top level of the file structure
/bin : where the executable files are located. They are available to all user
/dev : device drivers.
/etc : supervisor directory commands, configuration files, disk configuration files, valid user lists, groups, ethernet, hosts
/lib : contains shared library files and sometimes other kernel-related files
/boot : contains files for booting the system
/home : contains the home directory for users and other accounts
/mnt : used to mount other temporary file systems, such as cdrom and floppy for the CD-ROM drive and floppy diskette drive, respectively
/proc : contains all processes marked as a file by process number or other information that is dynamic to the system
/tmp : holds temporary files used between system boots
/usr : includes administrative commands, shared files, library files, and others
/var : contains variable-length files such as log and any other type of file that may contain a variable amount of data
/sbin : contains binary (executable) files, usually for system administration. For examplefdisk and ifconfig utlities
/kernel : contains kernel files
file filename : identifies the file type (binary, text, etc)
whereis filename : shows the location of a file
which filename : shows the location of a file if it is in your PATH
df -h : reports on the space left on the file system
du : outputs the number of kilobyes used by each subdirectory
mkfs.ext3 /dev/sdb : create file system and a parition and format it
mount /dev/sdb /mnt : mounts a file system to be ready to be used to the /mnt directory
unmount /dev/sdb /mnt : unmount (remove) the file system from the mount point
quota : displays disk usage and limits for a user of group
edquota : this is a quota editor. Users or Groups quota can be edited using this command
quotacheck : scan a filesystem for disk usage, create, check and repair quota files
User Administration:
--------------------
useradd user_name : adds accounts to the system
-d homedir : specifies home directory for the account
-g groupname : specifies a group account for this account
-m : creates the home directory if it doesn't exist
-s shell : specifies the default shell for this account
-u userid : specify a user id for this account
usermod user_name : modifies account attributes
userdel user_name : deletes accounts from the system
groupadd group_name : adds groups to the system
groupmod group_name : modifies group attributes
groupdel group_name : removes groups from the system
Bash Scripting:
---------------
[ $e -gt 2 ] : compare value of e if it's greater than 2 and evaluate the output as an error code
echo $? : print last error code in the terminal
[[ $e -gt 2 ]] : construct over the single bracket version and handle null-value variables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment