Basic Terminal commands
Terminals, also known as command lines or consoles. Using a terminal allows us to send simple text commands to our computer to do things.
1. grep
grep searches for PATTERNS in each FILE. grep prints each line that matches a pattern.
Syntax:
grep “text to search” file_name
To match the whole word
grep -w “text to search” file_name
To ignore case-sensitive
grep -i “text to search” file_name
To know the line number of matched text
grep -n “text to search” file_name
To include the required no of lines(4) after matched pattern
grep -A 4 “text to search” file_name
To include required no of lines(4) before matched pattern
grep -B 4 “text to search” file_name
To include required no of lines(4) before and after matched pattern
grep -C 4 “text to search” file_name
To search all files in current directry
grep “text to search” ./*
./*.txt –> will search all txt files
grep -r “text to search” . -> will search in subdirectories too
grep -rl “text to search” . -> will show the file names and directory only, not matching text
grep -rc “text to search” . -> will show the file names and directory with no of matching text
history | grep “text to search ” -> will show the history having searching text only
grep “…-…-….” Filename or grep -p “\d{3}-\d{3}-\d{4}”-> will show matching of “any 3 digit — any 3 digit — any 4 digit”
To check version
grep -v
2. curl
curl is a command line tool to transfer data to or from a server, using any of the supported protocols.
curl URL -> will show the response from the given URL
curl -i URL -> will show the response from the given URL with response header
curl -d ”key=value&key=value” URL -> to send an post request with data
curl -X PUT -d ”key=value&key=value” URL -> to send an PUT request
curl -X DELETE URL -> to send an DELETE request
curl -o filename.format URL -> to save response as a file
3. wget
The wget command is a command line utility for downloading files from the Internet.
wget URL -> will download the specific file
wget -r URL -> will download all files and sub folders too
wget -q URL -> to turn off output
4. tail
It prints the specified lines of the specified files.
tail filename -> will print last 10 lines
tail -n no.of_lines filename OR tail -no.of_lines filename
tail +from_line filename -> will print from the given(from_line) line
tail -q filename_1 filename_2 OR tail filename_1 filename_2 -> will print the lines from the given file
tail -f filename -> will print the last 10 lines and will update when the file is changed. (used for logfiles).
tail -v filename -> will print the filename in starting of text
tail — version -> to find the tail version
5. head
Head is also similar to tail. the tail will print the lines from bottom but the head will print the lines from the top.
for example, tail filename -> will print the first 10 lines of the given file
6. Less
less filename -> will show the file contents.
less -N filename -> to view the line numbers
7. Find
Used to find files and directories.
find directory -name filename
find directory -type d -> will search directories
find directory -type f -> will search files within given directory
find directory -type f -name “filename” -> will search the files based on given name in the given directories
find directory -type -iname “filename” -> will search the file with name case insensitive
find directory -type f -mmin -10 -> will show the files modified within 10 min ago
find directory -type f -mmin +10 -> will show the files modified more than 10 min ago
find directory -type f -mmin +1 -mmin -5 -> will sow the modified file more than 1 min and less than 5 min ago
find directory -type f -mtime -20 -> will show the modified files less than 20 days ago
find directory -size +5M -> will show the files over 5MB
find directory -perm 777 -> will show the files that have permission 777
find directory -type d -exec chmod 775 {} + -> will change mode (permission) of directories to 775. Where, exec -> execute the command.
find directory -type f -name “filename” -maxdepth 1 -> will show the files that match the given and within a directory of 1 (current directory).
8. ssh
ssh stands for “Secure Shell”. It is a protocol used to securely connect to a remote server/system.
ssh user_name@host (IP/Domain_name)
9. Kill
Kill is a built-in command which is used to terminate processes manually.
kill PID -> it will kill the process of the given PID. To get PIDs run the command ps