Needful CLI Commands:

Vishal PandeyVishal Pandey
7 min read

Computers understand only binary language so to interact with computer machines we need some bridge which fills this communication gap and this is done by shell.
Our operating system has a special user program known as shell which can read human language(commands) and can interact with the kernel to perform various tasks.

Here are some important cli commands:

  • Create a file/folder (touch,cat,mkdir,)

touch file.txt #create an empty file if it does not exist
cat > file.txt #if file does not exist; it can create file and let us write the content
mkdir foldername   #create only single folder
mkdir -p a/b/c     #create nested folders , work (parent exist or not)
  • Navigating folders

cd foldername      #move into a folder
cd ..              #Go one level back
cd or cd ~         #go to home directory
cd -               #go to previous working directory
cd /               #go to root directory
pwd                #print the current directory
  • Write/Overwrite content (echo,cat)

echo "hello world!" > file.txt
cat > file.txt #it can also create file and let you overwrite content, ctrl + d to save
  • Append content to file (echo,cat)

echo "Append line!" >> file.txt
cat >> file.txt   # type and press ctrl + d to save
  • Move/Rename a file/folder to another directory (mv)

mv file.txt /path/to/another/directory/
mv oldname.txt newname.txt
mv oldname newname            #rename a folder
mv foldername /path/to/dir/   #move folder to a different directory
  • Delete a File/Folder (rm,rmdir)

rm file.txt #No recycle bin-it's gone!
rmdir foldername   #remove an empty folder
rm -r foldername   #remove folder and its contents(recursive)
rm -rf foldername  #force remove folder without prompt
  • Empty a File (>,:)

> file.txt
: > file.txt
  • View File Content (cat)

cat file.txt
  • Other useful viewers (less,more)

less file.txt
more file.txt
  • Copying folders/files

cp -r folder1 folder2    #copy folder and its contents
#If folder2 is exist before copying folder1 then folder2 will have whole folder1
#If folder2 is not exist then there will be folder2 same as folder1
cp file1.txt file2.txt  #copying file1 to file2
  • Search inside a File (grep)

grep "keyword" file.txt #show that line
  • Edit a file using editor (nano , vim)

nano file.txt  #simple
vim file.txt   #advanced
  • Hidden file/folders (using .)

touch .hiddenfile.txt #create hidden file
cat .hiddenfile.txt  #To open hidden file
ls -a  #List all hidden files
mkdir .hiddenfolder      #create a hidden folder(starts with a dot)
ls -a                    #show hidden folders
  • Listing contents

ls       #list files/folders
ls -l    #long listing with details
ls -a    #show hidden files and folders
ls -R    #list recursively
ls -dl file.txt  #long listing for specific files
  • Search files (find)

#Let say we want to search all file named "hello.txt"
find . -name "hello.txt" -type f

#Let say we want to delete all files  having extension ".txt"
find . -name "*.txt" -type f -delete

find . -type d                     #find all folders in current directory
find /path -name foldername        #search for a folder by name
find . -name foldername -type d    #search for a folder by name

#let say we want to search a folder name hello
find . -name hello -type d
find ./ -name "*hello*" -type d | xargs rm -r 

#let say we want to delete all folder in nested ,named hello
find . -name hello -type d -delete
  • Giving file permission (chmod)

    Using Symbolic mode

#Make it readable, writable, and executable for the owner:
chmod u+rwx hello.txt
#Make it readable and executable for group:
chmod g+rx hello.txt
#Remove write permission for others:
chmod o-w hello.txt

Using Numeric mode
Each permission has a number:
r = 4 , w = 2 , x = 1
Read + Write = 4 + 2 = 6
Read + Execute = 4 + 1 = 5
Write + Execute = 2 + 1 = 3
Read + Write + Execute = 4 + 2 + 1 = 7

#Owner ➔ 7 (rwx)
#Group ➔ 5 (r-x)
#Others ➔ 5 (r-x)

chmod 755 hello.txt 

chmod 755 foldername     #change folder permissions
  • Changing the owner (chown)

#Let's say you want to give ownership of hello.txt to a user named vishal.
#You need sudo because changing ownership usually needs admin rights.
sudo chown vishal hello.txt

#Change owner and group together
sudo chown vishal:developers hello.txt #owner->vishal , group->developers
  • grep commands (grep)

    Key Notes:

    • -i makes search case-insensitive

    • -r enables recursive directory searching

    • -o with wc -l is the proper way to count total word occurrences

    • Quotes around search patterns ("") are good practice for multi-word searches

    • -v is useful for filtering out unwanted lines

    • -n helps locate matches by line numbers in files

 # 1. Basic search - finds lines containing "harry" (case-sensitive)
grep "harry" tempFile.txt

# 2. Inverted search - shows lines NOT containing "harry"
grep -v "harry" tempFile.txt

# 3. Numbered results - shows lines with "harry" including line numbers
grep -n "harry" tempFile.txt

# 4. Count matching lines - shows count of lines containing "Harry" (not total occurrences)
grep -c "Harry" tempFile.txt

# 5. Show only matches - prints each "Harry" match on separate line
grep -o "Harry" tempFile.txt

# 6. Count characters - counts total characters in all "Harry" matches (not recommended)
grep -o "Harry" tempFile.txt | wc -m

# 7. Count words - counts words in output (misleading for this use)
grep -o "Harry" tempFile.txt | wc -w

# 8. Count occurrences - correct way to count total "Harry" words in file
grep -o "Harry" tempFile.txt | wc -l

# 9. Recursive search - searches for "harry" in current directory and subdirectories
grep -r harry ./

# 10. Recursive case-insensitive search in home directory - looks for "aur" in ~/
grep -ri aur ~/

# 11. Same as 11 but with exact word matching (better practice)
grep -ri "aur" ./
ps -ef | grep -i 'chrome' | grep -v grep | awk '{print $2, $3}'     # Show Chrome's process ID and its parent process ID
open -a "safari"                                                    # Open safari on mac
chrome $                                                            # Open chrome on linux 
pkill -i safari                                                     # Force stop Safari browser
ps -A -o pid,ppid,comm,%cpu,%mem | sort -k4 -nr | head -n 3         # Top 3 CPU consuming processes
ps -A -o pid,ppid,comm,%cpu,%mem | sort -k5 -nr | head -n 3         # Top 3 memory consuming processes
python3 -m http.server 8000                                        # Start a simple HTTP server on port 8000
kill -9 $(lsof -ti :8000)                                          # Kill the process running on port 8000
python3 -m http.server 90                                          # Start a server on port 90 
netstat -a                                                         # Show all active network connections and ports
lsof -i :5432                                                      # Show process using port 5432
pgrep Telegram                                                     # Show the pid of Telegram
pgrep Telegram | xargs kill                                         # Kill Telegram by getting pid of telegram
curl -L -o Harry_Potter.txt "url"  # Download the Harry Potter book text file
cat Harry_Potter.txt | head -3                                     # Show first 3 lines of the book
cat Harry_Potter.txt | tail -10                                    # Show last 10 lines of the book
cat Harry_Potter.txt | grep "Harry" -o | wc -w                     # Count occurrences of the word "Harry"
cat Harry_Potter.txt | grep "Ron" | wc -w                          # Count number of lines containing "Ron"
cat Harry_Potter.txt | grep "Hermione" | wc -w                     # Count number of lines containing "Hermione"
cat Harry_Potter.txt | grep "Dumbledore" | wc -w                   # Count number of lines containing "Dumbledore"
cat Harry_Potter.txt | head -200 | tail -100                       # Show lines from 101 to 200
cat Harry_Potter.txt | grep -oE '\w+' | sort -u | wc -w                # Count number of unique words in tempFile.txt
brew install htop                                                  # Install 'htop' 
brew install vim                                                   # Install 'vim' text editor
brew install nginx                                                 # Install Nginx web server
brew uninstall nginx                                               # Uninstall Nginx from system

Some Miscellaneous commands

ifconfig                                                           # Display network interface configuration
nslookup google.com                                                # DNS lookup for google.com
ping google.com                                                    # Test internet connectivity
which node                                                         # Show path to Node.js binary
which code                                                         # Show path to VS Code binary
curl ifconfig.me                                                   # Show public IP on mac
ipconfig getifaddr en0                                             # Show private IP on mac

# All in one local + private ip address
echo "Local IP: $(ipconfig getifaddr en0 2>/dev/null || hostname -I | awk '{print $1}')" && echo "Public IP: $(curl -s ifconfig.me)"
0
Subscribe to my newsletter

Read articles from Vishal Pandey directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vishal Pandey
Vishal Pandey