Basic Linux Commands for day to day life
1. See the available
disk space in each of the partitions in your system
df
2. Know the disk usage
of a file in your System
du
3. To view the file
sizes of all the files in a folder.
ls -lah
4. Prints most of the
information about the system.This prints the Kernel release date, version,
processor type. etc
uname -a
5. Count lines in
file(foo)
wc -l foo
6. Search for the given
string in a single file
grep
"literal_string" filename
7. Checking for the
given string in multiple files.
grep
"this" demo_*
8.Case insensitive
search using grep -i
grep -i
"string" FILE
9.Match regular
expression in files
grep
"lines.*empty" demo_file
10.Checking for full
words, not for sub-strings using grep -w
grep -i
"is" demo_file
grep -iw
"is" demo_file
11.Highlighting the
search using GREP_OPTIONS
export
GREP_OPTIONS='--color=always' GREP_COLOR='100;8'
grep
--color=always abc a_file.txt > myoutput.txt
12.Searching in all
files recursively using grep -r
grep -r
"ramesh" *
13. Sed
This will replace
every instance of pattern1 with pattern2 in the file file.txt and will write
the result to newfile.txt.
The original file
file.txt is unchanged.
sed
"s/pattern1/pattern2/g" file.txt > newfile.txt
Sed command is
mostly used to replace the text in a file.
The below simple sed
command replaces the word "unix" with "linux" in the file.
14.Awk is one of the
most powerful tools in Unix used for processing the rows and columns in a
file.
Awk has built in
string functions and associative arrays.
1. awk '{print $1}'
input_file
Here $1 has a
meaning. $1, $2, $3... represents the first, second, third columns... in a row
respectively.
This awk command
will print the first column in each row as shown below.
15.Searchfile in a directory and move it to some other directory
find /E2E/incoming/finished/ -name "NGVAUK40220180502.dat"
mv /E2E/incoming/finished/NGVAUK40220180502.dat /VAM_Hybrid_E2E/Bulker/
mv /E2E/incoming/finished/NGVAUK40220180502.dat /VAM_Hybrid_E2E/Bulker/
16.Search any String in the directory
grep -rni
"string" *
r = recursive i.e,
search subdirectories within the current directory
n = to print the
line numbers to stdout
i = case insensitive
search
17.Search any String in the log file
grep -rni "WMSG0036E" SystemErr.log
18.Split any large size log file into small size file
split -b 50M tuxlap.txt
19.Find any running process
ps -ef | grep java
ps :Process Status
ps -ef | grep java
UID PID PPID C STIME TTY TIME CMD
The -e option generates a list of information about every process currently running.
The -f option generates a listing that contains fewer items of information for each process than the -l option.
The -l option generates a long listing, and when used together with the -e and -f options creates a table with 15 columns:
ps -efl
20.List of file that starts with some string
ls -lrt | grep $ NGVA*
21.How to check OS and version using a Linux command
21.How to check OS and version using a Linux command
lsb_release -a
Q 21 How to find files larger than 10MB in size in /usr directory ?
Ans: # find /usr -size +10M
Q 22 How to find files in the /home directory that were modified more than 120 days ago ?
Ans: # find /home -mtime +l20
Q 23 How to find files in the /var directory that have not been accessed in the last 90 days ?
Ans: # find /var -atime -90
Q 24 Search for core files in the entire directory tree and delete them as found without prompting for confirmation
Ans: # find / -name core -exec rm {} \;
Q 25 What is cpio command ?
Ans: cpio stands for Copy in and copy out. Cpio copies files, lists and extract files to and from a archive ( or a single file).
Q 26 How to check current run level of a linux server ?
Ans: ‘who -r’ & ‘runlevel’ commands are used to check the current runlevel of a linux box.
Q 27 How can you determine the total memory used by LINUX?
free –m , the option ‘m' displays all the data in MBs.
cat /proc/meminfo
vmstat –s
Top command: This command determines the total memory usage as well as also monitors the RAM usage.
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
Q 28 Enlist some Linux file content commands?
head: Displays the beginning of the file
tail: Displays the last part of the file
cat: Concatenate files and print on the standard output.
more: Displays the content in pager form and is used to view text in the terminal window one page or screen at a time.
less: Displays the content in pager form and allows backward and single line movement.
Q 29 Given a file find the count of lines containing the word "ABC".
grep –c "ABC" file1
Q 30 How can I send a mail with a compressed file as an attachment?
zip file1.zip file1|mailx –s "subject" Recipients email id
Email content
EOF
#!/bin/sh
TO="devayan.thakur@reflexisinc.com"
CC="devayan.thakur@reflexisinc.com,Anil.Jamkhandikar@reflexisinc.com"
cat body-message.txt | mail -s "Reflexis Support Monthly Service Review Report" -a Reflexis_Support_Service_Report.pdf -c $CC $TO
Q31 How do we create command aliases in a shell?
alias Aliasname="Command whose alias is to be created".
Q32 How can we find the process name from its process id?
We can use "ps –p ProcessId"
Q34 What are the four fundamental components of every file system on Linux?
Bootblock, super block, inode block and Datablock are found fundamental components of every file system on Linux.
Bootblock:This block contains a small program called "Master Boot record"(MBR) which loads the kernel during system boot up.
Super block contains all the information about the file system like the size of file system, block size used by its number of free data blocks and list of free inodes and data blocks.
Inode block: This block contains the inode for every file of the file system along with all the file attributes except its name.
Q35 How will you connect to a database server from Linux?
We can use isql utility that comes with open client driver as follows:
isql –S serverName –U username –P password
Q 36 I have 2 files and I want to print the records which are common to both.
comm -12 file1 file2 ... 12 will suppress the content which are
unique to 1st and 2nd file respectively.
Q37 I want to connect to a remote server and execute some commands, how can I achieve this?
We can use ssh to do this:
ssh username@serverIP -p sshport
Example
ssh root@122.52.251.171 -p 22
Once above command is executed, you will be asked to enter the password
Q38 I want to monitor a continuously updating log file, what command can be used to most efficiently achieve this?
We can use tail –f filename. This will cause only the default last 10 lines to be displayed on std o/p which continuously shows the updating part of the file.
Q39 How will you find the 99th line of a file using only tail and head command?
tail +99 file1|head -1
List All Processes in Current Shell
Display every active process (ps -A or ps -e )
To perform a full-format listing (ps -ef )
Display User Running Processes ( ps -x )
Print All Processes Running as Root ( ps -U root -u root )
HEAD
display the first default 10 lines.(head filename.txt )
head -n 30 filename.txt
Display a Specific Number of Bytes ( head -c filename.txt or head -c 5k filename.txt )
TAIL
Last 4 lines of a file
tail -n 4 /etc/group
tail -f -s <sleep interval in seconds> /path/to/file
tail -f command can be replicated using less command well
To print 15th line to 20th line in /etc/passwd file use below example.
head -n 20 /etc/passwd | tail -n 5
Q 39 Count how many times the word iPhone is mentioned in the file
Ans: # find /usr -size +10M
Q 22 How to find files in the /home directory that were modified more than 120 days ago ?
Ans: # find /home -mtime +l20
Q 23 How to find files in the /var directory that have not been accessed in the last 90 days ?
Ans: # find /var -atime -90
Q 24 Search for core files in the entire directory tree and delete them as found without prompting for confirmation
Ans: # find / -name core -exec rm {} \;
Q 25 What is cpio command ?
Ans: cpio stands for Copy in and copy out. Cpio copies files, lists and extract files to and from a archive ( or a single file).
Q 26 How to check current run level of a linux server ?
Ans: ‘who -r’ & ‘runlevel’ commands are used to check the current runlevel of a linux box.
Q 27 How can you determine the total memory used by LINUX?
free –m , the option ‘m' displays all the data in MBs.
cat /proc/meminfo
vmstat –s
Top command: This command determines the total memory usage as well as also monitors the RAM usage.
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
Q 28 Enlist some Linux file content commands?
head: Displays the beginning of the file
tail: Displays the last part of the file
cat: Concatenate files and print on the standard output.
more: Displays the content in pager form and is used to view text in the terminal window one page or screen at a time.
less: Displays the content in pager form and allows backward and single line movement.
Q 29 Given a file find the count of lines containing the word "ABC".
grep –c "ABC" file1
Q 30 How can I send a mail with a compressed file as an attachment?
zip file1.zip file1|mailx –s "subject" Recipients email id
Email content
EOF
#!/bin/sh
TO="devayan.thakur@reflexisinc.com"
CC="devayan.thakur@reflexisinc.com,Anil.Jamkhandikar@reflexisinc.com"
cat body-message.txt | mail -s "Reflexis Support Monthly Service Review Report" -a Reflexis_Support_Service_Report.pdf -c $CC $TO
Q31 How do we create command aliases in a shell?
alias Aliasname="Command whose alias is to be created".
Q32 How can we find the process name from its process id?
We can use "ps –p ProcessId"
Q34 What are the four fundamental components of every file system on Linux?
Bootblock, super block, inode block and Datablock are found fundamental components of every file system on Linux.
Bootblock:This block contains a small program called "Master Boot record"(MBR) which loads the kernel during system boot up.
Super block contains all the information about the file system like the size of file system, block size used by its number of free data blocks and list of free inodes and data blocks.
Inode block: This block contains the inode for every file of the file system along with all the file attributes except its name.
Q35 How will you connect to a database server from Linux?
We can use isql utility that comes with open client driver as follows:
isql –S serverName –U username –P password
Q 36 I have 2 files and I want to print the records which are common to both.
comm -12 file1 file2 ... 12 will suppress the content which are
unique to 1st and 2nd file respectively.
Q37 I want to connect to a remote server and execute some commands, how can I achieve this?
We can use ssh to do this:
ssh username@serverIP -p sshport
Example
ssh root@122.52.251.171 -p 22
Once above command is executed, you will be asked to enter the password
Q38 I want to monitor a continuously updating log file, what command can be used to most efficiently achieve this?
We can use tail –f filename. This will cause only the default last 10 lines to be displayed on std o/p which continuously shows the updating part of the file.
Q39 How will you find the 99th line of a file using only tail and head command?
tail +99 file1|head -1
List All Processes in Current Shell
Display every active process (ps -A or ps -e )
To perform a full-format listing (ps -ef )
Display User Running Processes ( ps -x )
Print All Processes Running as Root ( ps -U root -u root )
HEAD
display the first default 10 lines.(head filename.txt )
head -n 30 filename.txt
Display a Specific Number of Bytes ( head -c filename.txt or head -c 5k filename.txt )
TAIL
Last 4 lines of a file
tail -n 4 /etc/group
tail -f -s <sleep interval in seconds> /path/to/file
tail -f command can be replicated using less command well
To print 15th line to 20th line in /etc/passwd file use below example.
head -n 20 /etc/passwd | tail -n 5
Q 39 Count how many times the word iPhone is mentioned in the file
grep -o -i iphone Tweet_Data | wc -l
grep -wc "iPhone" Tweet_Data
- -i, --ignore-case
- -w, --word-regexp
- -c, --count
- -o, --only-matching
- -n, --line-number
- -R, -r, --recursive
- Q Steps that you should follow if you want the thread dump of your StandAlone Java Process
- Step 1: Get the Process ID for the shell script calling the java program
linux$ ps -aef | grep "runABCD"
Step 2: Get the Process ID for the Child which was Invoked by the runABCD.
Use the above PID to get the childs.
-
ps -aef | grep **8535** Step 3: Get the JSTACK for the particular process. Get the Process id of your XYSServer process. i.e. 8536
linux$ jstack **8536** > threadDump.log
Q40.What is netstat command in Linux?
netstat command in Linux shows the network status. This netstat command shows network ports in use and their incoming connections.



0 comments:
Post a Comment