Skip to content
Menu
ChaoTic
  • Home
  • Python
  • APEX
  • PL/SQL
  • Oralce DB
  • Docker
  • OCI
  • Nginx
  • C#
  • Linux
  • JavaScript
  • Privacy Policy
ChaoTic

Linux CLI Most Used Commands (ongoing)

Posted on July 11, 2022June 21, 2023
ls/cd
head/ tail/ cat/ sort/ piping
Echo / Redirect
wc / diff
rm / cp / mv
expansion
find / grep
du / df
history
ps
top
kill
gzip
tar
nano
alias
xargs
in
Sudo & filepermissions
Commands Chaining
Environment Variables
nohup
whoami
#return current login user

man : display Manual of commands

man whoami
# to check mannual of a command
# use key 'Q' to escape 
# use 'SPACE' or 'ARROW' key to navigate
man man

ls : listing folder structures

cd: navigate folder structures

pwd
#current working directory
ls
ls -l #list with detailed info, e.g permission
ls -la #list hidden, .**
ls -a # .*
#go 1 level up, 2 level up
cd ..
cd ../../

Head,tail : peek content of a file (default 10 lines)

cat: concate files, display content of a file (full)

Sort: sort output lines (numerica or alphabic)

less: display file content with pagination

| : piping standard input to the next command

head file.txt #output first 10 lines (default)
head file.txt -n 100   #output first 100 lines
tail file.txt #output last 10 lines (default)
tail file.txt -n 100   #output the last 100 lines.
tail log.txt -f # nice to have to read logfile where it keeps update
cat file.txt #prints out all content

less file.txt #prints out all content with pagination
#within less
/patten #to search matching patten

# sort output 
# sort output of ls , -ru : reverse and unique values only and pipe it to wc to count lines
ls | sort -ru | wc -l
# sort output of ls, -n: sort on numbers 
ls | sort -n | wc -l

# display only unique lines from a file 
cat file.txt | sort -u
sort -u file.txt
# or combine it with uniq
# -d show only duplicated lines
sort file.txt | uniq -d
# -u only non duplicated lines
sort file.txt | uniq -u
# -c gives a count of each unique entries
sort file.txt | uniq -c
# to make it more fun, we can pipe it and sort it to give it a rank
sort file.txt | uniq -c | sort -n # low to high
sort file.txt | uniq -c | sort -nr # hight to low

>>, >: Edit file Content, redirect output into file. > repace >> concate

echo: print lines to console

# redirect output into a file with append mode
ls -la >> test.txt  
# redirect output into a file with replace mode
ls -la > test.txt  
# echo string to file
echo "test string " > test.txt
echo "test string " >> test.txt

wc: word counts

diff: compare file content differences

# count file # lines # of words # of bits
wc file.txt
man wc # to read more 

# use pipe to combine commands
# count lines of the output of ls -la
ls -la | wc

# concate file1.txt and file2.txt pipe it to wc and count lines/ words and bits
cat file1.txt file2.txt | wc

# the results of wc can also be redirected into a file like this
cat file1.txt file2.txt | wc > wcresult.txt

# compare two files , use man diff to read more 
diff filea.txt fileb.txt

rm, cp and mv: Copy , move and remove files / folder

cp -r  #recursive , to cp folder and its nested childs 
rm -r  #recursive , to rm folder and its nested childs

Expansions

# expansions

# print out all files names with .txt as extenion
echo *.txt
# print out all files start with a, b or cand with .txt as extension
echo {a,b,c}.txt 
# print out all files with two letter as extension
echo *.??
# print out 1 to 100
echo {1..100}
echo day{1..365}

# print out all app files where its extension in js, php or py
echo app.{js,php,py} 

Find

is used to search files with name/type/extension

Grep

is used to search content within a file.READMORE

#find files, find file with name in current directory
# find all files + directories with .py as extension
find . -name '*.py'
# find all directory with name start with cap A
find . -type d -name 'A*'
# -iname , case insensitive
find . -type d -iname 'A*'
# or operator 
# -type f , files only
find . -type d -iname 'A*' -or -name 'B*'

#grep keyword "green" from file.txt
grep green file.txt
#find keyword "green" case sensitive within all files and subfiles in /root/files 
grep -r "green" /root/files
#find keyword "green" case insensitive within all files and subfiles in /root/files
grep -ir "green" /root/files
# or if directory is not mentioned, it earches current active directory
grep -ir "green"
# grep also allows regular express -o prints only matching part of the lines
grep -rE -o '^[a-zA-Z0-9]*@[a-zA-Z].[com|nl]' file.txt

DU: display directory and files sizes

# -h human readable, -m megabytes, -g gigabyte
du -h /foldername
du -m /foldername 
du -g /foldername
#get all file size in human readable format and sort it from ASC humanreadable
du -h /foldername | sort -h 

du -h /foldername | sort -h | tail -12 # get the largest 12 files
du -h /foldername | sort -h | head -12 # get the smallest 12 files

DF: get disk usage information

df -h
df -h /folder

History: dispay history commands with sequence number

history | sort -r -n # display history and sort it large to small on number
  126  df
  125  history | sort -r -n
  124  history | sort -n
  123  history | sort -r -n
  122  history
  121  df -h
  120  clear
  119  du -h
  118  df
  117  clear
  116  du -h | sort -h | head 
  115  du -h | sort -h | tail -r -12
  114  du -h | sort -h | tail -12 -r
  113  du -h | sort -h | tail -12
  112  du -h | sort -h
  111  du -mh | sort -h
# here with "!" and the sequence number, can trigger run of the same command. it's called expansion
!121
# previous 10 history 
history -10

#pipe history to grep and file 'grre' in the output
history | grep  'grre'

PS: display running processes

ps             #display user processes
ps axww        #display all (user + system processes)

# display all processes and find Safari.app
ps axww | grep  'Safari.app'

Top: used to show all running processes lively dashboard

top           #sorting on CPU usage

top -o mem    #sorting on memory usage 

Kill:kill process

Killall: name

killall Safari
kill 3289283
# jobs, bg, and fg

jobs # display a list of running /pending/paused jobs
# bg or '&' sign run process in the background
fg # brings process to the foreground

gzip : create zip files from file to reduce size

gzip -k file.txt 
# this will create a new file called file.txt.gz
# use ls -lh to check file size
ls -lh

# -kv keep and vbose mode to show details of the zip 
gzip -kv file.txt 

# gzip -d = gunzip

Tar : used to create archives, used to group files into 1 single file

tar -cf groupfile.tar file1.txt file2.txt file3.txt
# - c f , create file name : groupfile.tar, group file 1, 2, 3

tar -tf groupfile.tar 
# - t f, shows what are files are in the .tar archives


tar -xf groupfile.tar
# extract all files within the archives in the current directory

tar -xf groupfile.tar -C /root/foldername
# to extract to specific folder 

# Combine tar archive with gzip!
# a new file is created and compressed
tar -czf groupfile.tar.gz file1.txt file2.txt file3.txt


# combine unzip and untar
tar -xf groupfile.tar.gz 

Nano: text editor in console

nano file.txt
# ctrl + k 
# to cut current line
# ctrl + u 
# to past cutted line

# ctrl + shift + 6 start select 
# use arrow key to select more 
# option + 6 to copy
# ctrl + u to paste

# navigation
# ctrl + A to the begining of current line
# ctrl + E to the end of current line
# ctrl + Y go to top
# ctrl + V go to end

there seems to be no redo and undo on Mac

# option + u  redo 
# option + e  undo

Alias: to create alias for commands

alias my_ls_lah='ls -lah'  # this way is not persistent

# add this line to .bashrc to make it persistent
# aad this line to .zshrc to make it persistent on Mac 

# Attention, "" and '' double and single quotes
alias a001="echo $PATH"
alias a002='echo $PATH'

# a001 the path varible to be fixed, even after new path is added after. you will not seee it
# a002 the path is expanded at run time, any new path is displayed.

xargs: similar to piping, but this is special arguments

find . -size +1M | xargs ls -lah

# find a files where size is larger than 1 mb and pipe arguments to ls -lah to display details 

ln : links, hard and soft links. they are like shortcuts on windows

# create a new hard link

ln file.txt filelink.txt

echo "new content" >> file.txt 
echo "new content" >> filelink.txt  
# does the same thing..

# even
rm file.txt 
# filelink.txt still there with content.

# create a soft link (windows shortcuts)
ln -s file.txt filelink.txt

# examples of link

ls -la /usr/bin/python*

lrwxr-xr-x  1 root  wheel      75 Feb  6 22:22 /usr/bin/python -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x  1 root  wheel      82 Feb  6 22:22 /usr/bin/python-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
lrwxr-xr-x  1 root  wheel      75 Feb  6 22:22 /usr/bin/python2 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x  1 root  wheel      75 Feb  6 22:22 /usr/bin/python2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
lrwxr-xr-x  1 root  wheel      82 Feb  6 22:22 /usr/bin/python2.7-config -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7-config
-rwxr-xr-x  1 root  wheel  167072 Feb  6 22:22 /usr/bin/python3
lrwxr-xr-x  1 root  wheel      76 Feb  6 22:22 /usr/bin/pythonw -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7
lrwxr-xr-x  1 root  wheel      76 Feb  6 22:22 /usr/bin/pythonw2.7 -> ../../System/Library/Frameworks/Python.framework/Versions/2.7/bin/pythonw2.7

# where python command is pointing at 2.7

who : used to display users logged in to the system

Su : switch user

Sudo : super user do

Chown: change ownership

Red: Owner | Yellow: Group Owner
# list all groups 
groups 

# change group ownership

sudo chown work:root /folder

# change ownership of a file
sudo chown <USER> <FILE>

# change ownership of a folder
sudo chown -R <USER> <Folder>

#under stand permissions 

drwxr-xr-x
12345678910
drwxr–xr–x
file typereadwriteexecutereadwriteexecutereadwriteexecute
Position 1 options: {-,d,c,l} – file, d directory, c character special file, l symbolic link

Position 1: indicates file type.

Position 2 ~ 4 : indicates Owner Rights

Position 5 ~ 7 : indicates Owner Group Rights

Position 8 ~ 10: indicates Everyone’s Rights

“x” on folders gives <cd> into permission. Without it, users cannot cd into the folder


chmod : used to change file/folder permissions

When specifying permissions with chmod, we use a special syntax to write permission statements.

First, we specify the “who” part using following values:

  • u – user (the owner of the file/ folder)
  • g – group (owner group)
  • o – Others (the rest of the world)
  • a – all of the above (u, g, o)

Next, we tell chmod “what” we are doing using the following characters:

  • – (minus sign) removes a permission
  • + (plus sign) to grant a permission
  • = (equals sign) set a permission and removes others

Finally, the “which” values:

  • r – the read permission
  • w – the write permission
  • x – the execute permission
# add write permission to group 

sudo chmod g+w file.txt

# remove write permission to All ( user, group and world)
sudo chmod a-w file.txt

# add multiple permission at the same time

sudo chmod g+rwx file.txt
sudo chmod o-rwx file.txt

# use "="

chmod g=r file.txt # will remove "wx" for group if exists (replace)
chmod g=rx file.txt # will remove "w" for group if exists (replace)

# change permission for user group at the same time

chmod ug+r file.txt # this will add read permission for user and group at the same time

Commands Chaining

; semi column || double pipe( or ) && ampersand ampersand ( and )
mkdir test || echo "directory already exisits"

Environment Variables

printenv
# to read all environment variables
printenv

echo $PATH 
# or 
printenv PATH

Leave a Reply Cancel reply

You must be logged in to post a comment.

Recent Posts

  • Oracle APEX cheat sheets (on going)
  • Second Take on React Native – ReadCast
  • Switch Between APEX builder Authentication schemes
  • Use BitBucket Pipelines to Automate Oracle APEX deployment
  • MARKDown TEST

Categories

  • APEX
  • C#
  • chatgpt
  • Docker
  • JavaScript
  • Linux
  • Nginx
  • OCI
  • Oracle APEX
  • Oralce DB
  • PL/SQL
  • Python
  • Uncategorized
©2025 ChaoTic | Powered by SuperbThemes
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT
Scroll Up