Basic Linux commands

Sayali KokaneSayali Kokane
11 min read

Of course! Here’s a handy list of basic Linux commands for you:

  • pwd - Print the current working directory.

  • ls - List directory contents.

  • cd - Change directory.

  • touch - Create an empty file.

  • cat - Concatenate and display file content.

  • cp - Copy files or directories.

  • mv - Move or rename files or directories.

  • rm - Remove files or directories.

  • mkdir - Create a new directory.

  • rmdir - Remove an empty directory.

  • chmod - Change file permissions.

  • chown - Change file owner and group.

  • File and Directory Operations Commands

CommandDescriptionOptionsExamples
lsList files and directories.-l (Long format listing), -a (Include hidden files), -h (Human-readable file sizes)ls -l (Detailed info), ls -a (Includes hidden files), ls -lh (Human-readable sizes)
cdChange directory.cd /path/to/directory (Change to specified path)
pwdPrint current working directory.pwd (Displays current directory)
mkdirCreate a new directory.mkdir my_directory (Creates directory)
rmRemove files and directories.-r (Recursively), -f (Forcefully)rm file.txt (Delete file), rm -r my_directory (Delete directory and contents), rm -f file.txt (Force delete)
cpCopy files and directories.-r (Recursively)cp -r directory destination (Copy directory), cp file.txt destination (Copy file)
mvMove/rename files and directories.mv file.txt new_name.txt (Rename file), mv file.txt directory (Move file)
touchCreate an empty file or update timestamps.touch file.txt (Create empty file)
catView the contents of a file.cat file.txt (Display file content)
headDisplay the first few lines of a file.-n (Number of lines)head file.txt (First 10 lines), head -n 5 file.txt (First 5 lines)
tailDisplay the last few lines of a file.-n (Number of lines)tail file.txt (Last 10 lines), tail -n 5 file.txt (Last 5 lines)
lnCreate links between files.-s (Symbolic links)ln -s source_file link_name (Create symbolic link)
findSearch for files and directories.-name (Filename), -type (File type)find /path/to/search -name "*.txt" (Find .txt files)

File Permission Commands

CommandDescriptionOptionsExamples
chmodChange file permissions.u (User), g (Group), o (Others), + (Add), - (Remove), = (Set)chmod u+rwx file.txt (Add read, write, execute to user)
chownChange file ownership.chown user file.txt (Change owner)
chgrpChange group ownership.chgrp group file.txt (Change group)
umaskSet default file permissions.umask 022 (Default permissions)

File Compression and Archiving Commands

CommandDescriptionOptionsExamples
tarCreate or extract archive files.-c (Create), -x (Extract), -f (File name), -v (Verbose), -z (gzip), -j (bzip2)tar -czvf archive.tar.gz files/ (Create compressed tar)
gzipCompress files.-d (Decompress)gzip file.txt (Compress file)
zipCreate compressed zip archives.-r (Recursively)zip archive.zip file1.txt file2.txt (Create zip archive)

Process Management Commands

CommandDescriptionOptionsExamples
psDisplay running processes.-aux (All processes)ps aux (Show all processes)
topMonitor system processes in real-time.top (Dynamic process view)
killTerminate a process.-9 (Forcefully)kill PID (Terminate process by ID)
pkillTerminate processes by name.pkill process_name (Terminate by name)
pgrepList processes by name.pgrep process_name (List by name)

System Information Commands

CommandDescriptionOptionsExamples
unamePrint system information.-a (All info)uname -a (All system info)
whoamiDisplay current username.whoami (Current username)
dfShow disk space usage.-h (Human-readable)df -h (Disk usage)
duEstimate file/directory sizes.-h (Human-readable), -s (Total size)du -sh directory/ (Directory size)
freeDisplay memory usage.-h (Human-readable)free -h (Memory usage)
uptimeShow system uptime.uptime (System uptime)
lscpuDisplay CPU information.lscpu (CPU details)
lspciList PCI devices.lspci (PCI devices)
lsusbList USB devices.lsusb (USB devices)

Networking Commands

CommandDescriptionExamples
ifconfigDisplay network interfaces.ifconfig (Interface details)
pingSend ICMP echo requests.ping google.com (Check connectivity)
netstatDisplay network connections.netstat -tuln (Listening TCP/UDP connections)
ssDisplay network sockets.ss -tuln (Listening TCP/UDP sockets)
sshSecurely connect to a remote server.ssh user@hostname (Connect via SSH)
scpSecurely copy files between hosts.scp file.txt user@hostname:/path/to/destination (Secure copy)
wgetDownload files from the web.wget http://example.com/file.txt (Download file)
curlTransfer data to/from a server.curl http://example.com (Retrieve webpage content)

Environment Variable Commands

CommandDescription
export VARIABLE_NAME=valueSets the value of an environment variable.
echo $VARIABLE_NAMEDisplays the value of a specific environment variable.
envLists all environment variables currently set in the system.
unset VARIABLE_NAMEUnsets or removes an environment variable.
export -pShows a list of all currently exported environment variables.
env VAR1=value COMMANDSets the value of an environment variable for a specific command.
printenvDisplays the values of all environment variables.

User Management Commands

CommandDescription
whoShow who is currently logged in.
sudo adduser usernameCreate a new user account on the system with the specified username.
fingerDisplay information about all the users currently logged into the system.
sudo deluser USER GROUPNAMERemove the specified user from the specified group.
lastShow the recent login history of users.
finger usernameProvide information about the specified user.
sudo userdel -r usernameDelete the specified user account from the system, including their home directory.
sudo passwd -l usernameLock the password of the specified user account.
su – usernameSwitch to another user account with the user’s environment.
sudo usermod -a -G GROUPNAME USERNAMEAdd an existing user to the specified group.

Bash Shortcuts Commands

NavigationDescriptionEditingDescriptionHistoryDescription
Ctrl + AMove to the beginning of the line.Ctrl + UCut/delete from cursor to beginning of line.Ctrl + RSearch command history (reverse search).
Ctrl + EMove to the end of the line.Ctrl + KCut/delete from cursor to end of line.Ctrl + GEscape from history search mode.
Ctrl + BMove back one character.Ctrl + WCut/delete the word before the cursor.Ctrl + PGo to the previous command in history.
Ctrl + FMove forward one character.Ctrl + YPaste the last cut text.Ctrl + NGo to the next command in history.
Alt + BMove back one word.Ctrl + LClear the screen.Ctrl + CTerminate the current command.
Alt + FMove forward one word.

Nano Shortcuts Commands

File OperationsDescriptionNavigationDescriptionEditingDescriptionSearch and ReplaceDescription
Ctrl + OSave the file.Ctrl + YScroll up one page.Ctrl + KCut/delete from cursor to end of line.Ctrl + WSearch for a string in the text.
Ctrl + XExit Nano.Ctrl + VScroll down one page.Ctrl + UUncut/restore the last cut text.Alt + WSearch and replace a string in the text.
Ctrl + RRead a file into the current buffer.Alt + \Go to a specific line number.Ctrl + 6Mark a block of text for copying/cutting.Alt + RRepeat the last search.
Ctrl + JJustify the current paragraph.Alt + ,Go to the beginning of the line.Ctrl + KCut/delete the marked block of text.
Alt + .Go to the end of the line.Alt + 6Copy the marked block of text.

VI Shortcuts Commands

CommandDescription
cwChange the current word. Deletes from cursor position to the end of the current word.
ddDelete the current line.
xDelete the character under the cursor.
REnter replace mode. Overwrites characters starting from the cursor position until you press Esc.
oInsert a new line below the current line and switch to insert mode.
uUndo the last change.
sSubstitute the character under the cursor and switch to insert mode.
dwDelete from cursor position to the beginning of the next word.
DDelete from the cursor position to the end of the line.
4dwDelete the next four words from the cursor position.
ASwitch to insert mode at the end of the current line.
SDelete the current line and switch to insert mode.
rReplace the character under the cursor with a new character entered from the keyboard.
iSwitch to insert mode before the cursor.
3ddDelete the current line and the two lines below it.
ESCExit from insert or command-line mode and return to command mode.
URestore the current line to its original state before any changes were made.
~Switch the case of the character under the cursor.
aSwitch to insert mode after the cursor.
CDelete from the cursor position to the end of the line and switch to insert mode.

Vim Shortcuts Commands

Normal ModeDescriptionCommand ModeDescriptionVisual ModeDescription
iEnter insert mode at the current cursor position.:wSave the file.vEnter visual mode to select text.
xDelete the character under the cursor.:qQuit Vim.yCopy the selected text.
ddDelete the current line.:q!Quit Vim without saving changes.dDelete the selected text.
yyCopy the current line.:wqSave and quit Vim.pPaste the copied or deleted text.
pPaste the copied or deleted text below the current line.:s/old/new/gReplace all occurrences of “old” with “new” in the file.
uUndo the last change.:set nu or :set numberDisplay line numbers.
Ctrl + RRedo the last undo.

File Permissions

  • chmod 775 file: Change mode of file to 775.

  • chmod -R 600 folder: Recursively chmod folder to 600.

  • chown user:group file: Change file owner to user and group to group.

File Permission Numbers First digit is owner permission, second is group, and third is everyone. Calculate permission digits by adding numbers below:

  • 4: read (r)

  • 2: write (w)

  • 1: execute (x)

————————————————————————————————————

Stay Connected

Thank you for reading my blog!
I hope you found the information and insights shared here helpful.

0
Subscribe to my newsletter

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

Written by

Sayali Kokane
Sayali Kokane

👨‍💻 DevOps Engineer | Tech Enthusiast | Knowledge Sharer As a dedicated DevOps engineer, I'm passionate about bridging the gap between development and operations to deliver high-quality, scalable solutions. With a solid foundation in cloud services, automation, and continuous integration/continuous deployment (CI/CD), I strive to optimize systems and streamline processes to drive efficiency and innovation. 🌐 Why Follow Me? I believe in the power of community and the importance of sharing knowledge. Whether you're a seasoned professional or just starting in the tech world, my goal is to provide valuable insights, tips, and resources that can help you excel in your DevOps journey. 📢 Let's Connect: Join me as we explore the ever-evolving world of DevOps.