Collection of simple bash commands to get started with Terminal
~ --> This symbol means we are in User's Home directory
$ --> This symbol means we are in Root Directory
[Command] + [K] --> Clears out Terminal Screen
[Control] + [C] --> Break / Exit from application within Terminal
-
In order to change the shell, please use the following command
-
chsh -s /bin/bash
(Bash By Default) -
chsh -s /bin/zsh
(Zsh by Default)
Command : who am i
Shows Username, Terminal ID, Date & Time
Command : whoami
Shows Username only
Command : exit
Exits terminal Window
Command : clear
Clears the terminal Window
Command : history
Provides history of all past commands you have typed in the shell
History n
--> Only lists the last n commands
Command : echo
Prints the text
echo "Hello World"
--> Prints Hello world text
Command : alias
Give a pseudonym to another command
alias cdul = "cd .."
--> When we type in cdul, "cd .." command would be executed.
Note
In order to add alias
to terminal, we have to add these alias to Bash config file ~/.bashrc
Type sudo open ~/.bashrc
, edit this file and add alias here and save it. Restart your Terminal for alias to take effect.
Command : unalias
Removes existing alias
unalias cdul
--> Removes alias from the terminal.
Command : pwd
Print Working Directory
Command : ls
List files in a directory
ls -l
--> files displayed as list
ls -lr
--> r stands for human readable file size
Command : cd
Change Directory
cd
--> Change Directory
cd ~
--> Goes to Home directory
cd ..
--> Up a level from current directory
cd ...
--> Up two level from current directory
cd Pictures
--> Goes to pictures directory
cd Users/Jvalaiyapathy
--> Takes us to User directory
Command : mkdir
Creates a new directory in the current folder
mkdir sampleFolder
--> Creates a new folder called "SampleFolder"
mkdir folder1 folder2 folder3
--> Creates multiple folder at the same time
mkdir -p /home/misc/salesforce
--> Creates all parent folder such as misc, salesforce on the fly
Flags
-v
--> Get confirmation where the directory was created
-p
--> Allows to create all parent folder on the fly. When we are using this flag, mkdir won't error out if the folder already exist. If it does not exist, then it creates the given folder.
Command : rmdir
Removes the directory in the current folder
rmdir sampleFolder
--> Removes "SampleFolder" directory
Command : cp
Copy files to specific location
cp *.txt ~
--> Copy all files of type txt to Home directory
cp text1.txt ~\HomeTab
--> copy specific file to HomeTab folder
Command : mv
mv is short for move
Rename folder or file
It is also used to move files to different location
mv text1.txt text2.txt
--> Moving file to a different location.
text1.txt is the old name of the file, text2.txt is the new name
rm -f text1.txt
--> You may want to consider removing old file 'text.txt' from current directory
mv test1.text ~/SampleDirectory
--> Move test1.text file to Sample Directory.
mv *.txt ~/SampleDirectory
--> Move all files of type txt to Sample Directory
mv * .
--> Move all files to current directory [This does nothing]
mv * ..
--> Move all files to 1 level up directory
mv * ...
--> Move all files to 2 level up directory
Command : rm
Remove a folder or a file
rm Jag.txt
--> Removes file
rm yetAnotherFolder
--> Removes folder "yetAnotherFolder"
rm -rfv yetAnotherFolder
--> Remove with force. Perform Hard delete.
Flags
-f
--> to ignore non-existent files, never prompt
-r
--> to remove directories and their contents recursively
-v
--> to explain whats being done
Command : touch
create new files or changes file stamp. It is only used to create empty files
touch index.html
--> creates a new file called "Index.html"
Command : open
open current location or file
open index.html
--> open a file
open index.html -a "Sublime Text
--> open a file in Sublime Text editor
open . -a "Sublime Text"
--> Open the current folder in Sublime Text editor
open /custom/path/ -a "Sublime Text"
--> Open specific folder in Sublime Text editor
Flags
-a
--> Specify the name of the application which can be used to open the file. Please note that the name of the application is case sensitive
Command : screencapture
Takes screenshot of the screen
screencapture -x -t jpg screenshot.jpg
--> creates JPG screenshot and stores in current directory
Flags
-P
--> Take screenshot and preview immediatly
-x
--> Take screenshot silently
-t
--> Specify the filetype
Command : cat
Outputs the contents of the file
cat index.html
--> Prints the contents of Index.html in the terminal (In this case CAT opens the file)
cat < index.html
--> In this case the shell opens the file passing it as CAT's standard Input
cat > newFile.txt
--> This helps to CREATE A NEW FILE called "newFile.txt". We will be prompted to enter text in terminal.
To save the contents of this file press Ctrl + D
to save and exit.
cat file1.txt file2.txt > mergedfile.txt
--> Here CAT is used to merge 2 files to a single file.
Command : ln
Used to Link a file. There are 2 types of links (Soft Link, Hard link)
ln old.txt new.txt
--> Now the old.txt file is linked to new.txt. new.txt is similar to a replica of old.txt.
Any changes made to old.txt will be reflected in new.txt. This is hard link
If old.txt is deleted, new.txt will still be accessible
ln -s old.txt new.txt
--> This creates a soft link between old.txt and new.txt. If old.txt file is deleted, new.txt will not be accessible
When we create a file in Shell, there are 4 options which set by default:
A. User (User who owns the file)
--> Read and Write Permission (Default)
B. Group (User belonging to file defined ownership group)
--> Read Only Permission (Default)
C. Other (Everyone else)
--> Read Only Permission (Default)
D. Execute (Allow executing file as Program)
--> False (Default)
Numeric Values of Pemissions:
Read (r)
- 4
Write (w)
- 2
Execute (x)
- 1
A file at best can have a total numeric value of (4+2+1) = 7 <br /F>
Command : umask
Returns the value of system file mode creation mask. Default value is 022
When we create a file, the system as default subtracts 666 - 022 = 644 (For File), 777 - 022 = 755 (For Folder)
So the default permissions for a file is 644.
6
--> Owner of the file can Read and Write ( 6 = 4 + 2)
4
--> Group can only Read file
4
--> Other can only Read file
coming soon!
coming soon!
coming soon!
Reference: https://courses.cs.washington.edu/courses/cse390a/14au/bash.html