Most input lines entered at the shell prompt have three basic elements:-
- Command
- Options
- Arguments
The command is the name of the program you are executing. It may be followed by one or more options (or switches) that modify what the command may do. Options usually start with one or two dashes, for example, -p or --print, in order to differentiate them from arguments, which represent what the command operates on. However, plenty of commands have no options, no arguments, or neither.
List Of Some Basic Commands:-
ls
→ Use to see all the list of files or folders inside a directoryls -a
→ list of all the files and folders including hidden files & foldersls -l
→ Use to see all the files and folders including file permissions as wellls -al
→ combine the above two commands - hidden files and folders + detailsls -R
→ show all the sub directory of a directorycd <directory name>
→ it is used for changing from any directory into another directorycd..
→ go back to previous foldercd -
→ change to previous directorycd ~
→ go back to home directorypwd
→ print working directory or present working directoryman <command>
→ describes the manual about any command.
→ the current directory..
→ the previous directorycat <filename>
→ Use to show the content of any file quickly in the terminaltac <filename>
→ Use to look at a file backwards, starting with the last line.cat > <filename>
→ quickly create a file of given filename and add some content in it.>
→ Redirection command: redirect the output to some other file
cat file1 file2 > file3
→ concatinate the content of file1 and file2 and paste that merged content into file3echo
→ Use to print anything in the terminalecho "something" > filename
→ add the text into the mentioned filetree
→ displays a tree view of the filesystem
Commands for Working with File system :-
mkdir <dir name or path>
→ Use to create any new directorymkdir sampdir
- It creates a directory named sampdir under the current directory.
mkdir /usr/sampdir
- It creates a directory called sampdir under /usr directory.
mkdir -p dir1/middle/dir2
→ creating a directory in the middle ofdir1
&dir2
rmdir <dir name>
→ Removing a directory is done with rmdir. The directory must be empty or the command will failrm -rf <dir name>
→ To remove a directory and all of its contents recursively, it is extremely dangerous and should be used with the utmost care (-rf means forcely remove)rm -f
→ Forcefully remove a filetouch <filename or path>
→ create a new filecp <filename> <new filename>
→ Use to make a copy of a filecp file1 file2
→ It will make a copy of file1 and naming that file2
mv <filename> <location>
→ To move a file to a new location- can also be used to rename a file
mv <filename> <newfilename>
mv files.txt newfile.txt
→ Here files.txt will be moved to newfile.txt but newfile.txt does not exist for the first time. So, files.txt will be moved and also renamed as newfile.txt
locate "file extension"
→ locates all the files with the same extension as mentioned- Example:
locate "*.txt"
- Example:
sudo
→ super user do (it provides the administrative permissions). It always ask for sudo password because it is kind of admin control command.df
→ it shows disk space usage in kb but if we writedf -m
then it will show usage in mbdf -h
→ -h is a flag which means human readable format
du
→ it shows disk usage statistics. It also has flagdu -h
means in human readable formathead <filename>
→ It prints by default first 10 lines of the given filehead -n 4 <filename>
→ will only display the first 4 lines.
less
→ Used to view larger files because it is a paging program. It pauses at each screen full of text, provides scroll-back capabilities, and lets you search and navigate within the file.tail
→ Used to print the last 10 lines of a file by defaulttail -n 5 <filename>
→ will display the last 5 lines.
diff file1 file2
→ compares both the files & print the uncommon linesfind
→ find things in a file or a directoryfind /usr -name gcc
→ Searching for files and directories named gccfind /usr -type d -name gcc
→ Searching only for directories named gccfind /usr -type f -name gcc
→ Searching only for regular files named gccfind -name "*.swp" -exec rm {} ’;’
→ To find and remove all files that end with ".swp". The {} (squiggly brackets) is a placeholder that will be filled with all the file names that result from the find expression, and the preceding command will be run on each one individually. you have to end the command with either ‘;’ (including the single-quotes) or "\;"- for more flags check
man find
File Permissions Commands:-
There are three types of permission:
- read (r)
- write (w)
- execute (x)
- These are generally represented as in rwx
These permissions affect three groups of owners:
- user/owner (u)
- group (g) → an entity among a user group
- guests or the other people (o)
ls -l <filename>
→ shows the file permissions for that particular fileExample:-
ls -l myfile.txt -rw-r--r-- 1 bishal bishal 98 May 4 23:07 myfile.txt
Changing the file permissions
using
chmod
commandchmod u=rwx,g=rx,o=rw <filename>
- changes made here:
- User:
rwx
- Group:
rx
- Other:
rw
- User:
- changes made here:
Using octal numbers with
chmod
chmod 777 <filename>
777
→ -rwx all three permission fully accessable for user,group & others.
Categories
4
→ if read permission is desired3
→ if write permission is desired1
→ if execute permission is desired.0
→ no permission(Thus, 7 means read/write/execute, 6 means read/write, and 5 means read/execute.)
Example:-
chmod 577 myfile.txt
(Here User have read and execute permission only as 5 and group,others have all permission as it's 77)
Changing the users of a file
chown
commandsudo chown root <filename>
- we have to use
sudo
as we are accessingroot
permissions root
user has the highest number of permissions in linux/unix based systems
- we have to use
grep command
- Global regular expression print
- Allows to search some text in files
Case sensitive
Versions of grep:
grep -V
Mac → BSD grep
- Linux → GNU
Simple searching a word
grep "bishal" names.txt **bishal**
- If present, it will return the string itself!
Expanding the whole word
grep -w "bishal" names.txt **bishal** das
- Using
-w
(means word-regexp), we searched for the “bishal” & it returned the whole string associated with that word!
- Using
To ignore the search case-sensitivity
grep -i "bishal" names.txt **bishal** das
Print the line number of the word
grep -n "bishal" names.txt 1:bishal das
Print lines after & before the word
grep -B 5 "bishal" names.txt Carlos santana Ishan kurnawat Micheal cade John irshad Arsab roy **bishal** das
-B 5
→ means print 5 lines before the actual word we are searchinggrep -A 5 "bishal" names.txt
-A 5
→ means print 5 lines after the actual word we are searching
To search the entire directory for a word
grep -win "bishal" ./*.txt
- This will check all the
.txt
files in the current directory for the word (-w/-i/-n all flag together = win)
- This will check all the
Which file contains a match and count all file
grep -wirl "bishal" .
using the
-l
tag here, we found the list of file that contained “bishal” in the current directorygrep -wirc "bishal" .
using the
-c
tag here, we count the list of file that contained “bishal” in the current directory
- Using
regex
to search- Regular expression commands
- Come back to it later
- Using your own command
- Steps :-
- open .bashrc file with any editor vim/gedit by
vi .bashrc
orgedit .bashrc
command - add your customized command at the last line or anywhere of that .bashrc file and save the file.
alias gpom = 'git push origin main'
- then execute this command
. ~/.bashrc
in your terminal (home directory) - your alias command is successfully updated in .bashrc file
- now you can use "gpom" command very easily
- open .bashrc file with any editor vim/gedit by
- Steps :-
Useful terminal shortcuts
history
command- shows a history of all the commands you ran in that particular session
to directly use a command from history
!<history-number>
ctrl + A
→ Cursor goes to the beginingctrl + e
→ Cursor goes to the endctrl + k
→ after cursor all will be deletedctrl + u
→ entire command will be deletedctrl + r
→ search previous command;
→ to use multiple commands in the same linegit add .;git commit -m "message";git push origin main
ping <URL>
→ to check the connectivity status of a website by sending packets (ping - Packet Internet Groper)wget <URL>
→ to download any file from the internet- you’ll have to install this using your package managers
apt install
brew install
yum install
- you’ll have to install this using your package managers
top
→ process running + CPU usagekill <process_id>
→ to stop a running process
uname
→ prints out your OS name- has many tags associated
zip & unzip
zip details.zip name.txt phone.txt
→ create a zip file named "details.zip" and zipped two files "name.txt" & "phone.txt" into "details.zip"unzip details.zip
→ it will unzip "details.zip"
hostname
→ prints the host’s namehostname -i
→ ip address of hostnslookup <URL>
→ checkout ip address of a domainnetstat
→ show all the portsps aux
→ showing all the running processes with PIDvmstat
→ checking your virtual memoryid
→ to check user & group id’sgetent
→ if a particular user exists or notlscpu
→ all details of cpulsof
→ list all the open files
Working with Operators:-
- Combining various commands together
&
operator&&
operatorecho "first" && echo "second"
- only when the
first
command is completed, then thesecond
will run
- only when the
;
operator||
operator (OR)|
operator (pipe)!()
operator (NOT)>
→ over-write>>
→ appendecho "hey" >> names.txt
{}
→ combination operator
Thanks! for reading this blog :)
Hope this blog will help you!👍
Follow Kubesimplify on Hashnode, Twitter and Linkedin. Join our Discord server to learn with us.