Bash Commands Cheat Sheet โจ
Basic Commands
Displaying Output:
Echo a calculation result:
num1=4 num2=5 echo $((num1+num2)) # Prints: 9 echo 'end' # Prints: end
๐ Tip:
echo
is 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:
ls
Show hidden files and folders:
ls -a
Show permissions of contents:
ls -la
โก Pro Tip: Permissions include read (r), write (w), and execute (x).
File and Folder Operations ๐
Create Files:
Using
touch
command:touch script.js script1.js script2.js script3.js
Create Folders:
Using
mkdir
command: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 fileName
Delete an empty folder:
rmdir folderAddress
Delete 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
cat
command:cat fileName
โจ Note:
cat
means concatenate and is used to display file content.
Edit File Data:
Using
nano
editor:nano fileName
Using
vim
editor:vim fileName
โก Pro Tip: To save in
vim
, pressESC
then type:wq
to 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 ๐