Bash Commands Cheat Sheet โจ
Hey I am Full Stack Developer.
Basic Commands
Displaying Output:
Echo a calculation result:
num1=4 num2=5 echo $((num1+num2)) # Prints: 9 echo 'end' # Prints: end๐ Tip:
echois great for printing text or results in the terminal!
Get the Current Directory:
Print working directory:
pwd๐ Displays the current location in the filesystem.
Check Current User:
Who am I?
whoami๐ค Shows the username of the logged-in user.
Listing Directory Contents ๐
See All Files and Folders:
Basic listing:
lsShow hidden files and folders:
ls -aShow permissions of contents:
ls -laโก Pro Tip: Permissions include read (r), write (w), and execute (x).
File and Folder Operations ๐
Create Files:
Using
touchcommand:touch script.js script1.js script2.js script3.js
Create Folders:
Using
mkdircommand:mkdir src
Copy Files:
Copy a file into a folder:
cp fileNameToCopy folderNameToPaste
Move Files:
Move a file to another folder:
mv fileNameToCopy folderNameToPaste
Rename Files:
Move and rename a file:
mv fileNameToMoveOrRename newFileName
Delete Files and Folders:
Delete a file:
rm fileNameDelete an empty folder:
rmdir folderAddressDelete a folder with contents:
rm -r folderAddress # -r means recursiveโก Pro Tip: Use with caution as this permanently deletes files!
For Loops in Bash ๐
Create Multiple Files:
100 JavaScript files:
for i in {1..100}; do touch "app$i.js"; done
Delete Multiple Files:
Remove those 100 files:
for i in {1..100}; do rm "app$i.js"; done
Viewing and Editing Files ๐ง
View File Data:
Using
catcommand:cat fileNameโจ Note:
catmeans concatenate and is used to display file content.
Edit File Data:
Using
nanoeditor:nano fileNameUsing
vimeditor:vim fileNameโก Pro Tip: To save in
vim, pressESCthen type:wqto write and quit.
Summary
This cheat sheet gives you a quick and interactive guide to essential bash commands. From managing files ๐ to looping ๐ and editing content ๐ง, bash scripting makes automating tasks fun and efficient!
Happy Coding ๐