Friday, 26 April 2024

Top 20 Linux Interview Questions and Answers 2024

 

  1. How to remove file and directories?

rm  file1 file2

rmdir dir1 (only deletes empty directories)

rm –r dir1 dir2

  1.  How to change from one directory to other?

cd dir1

  1. How to find logs older than seven days and remove them?

find /path/ -type f -mtime +7 -name ‘*.gz’ -execdir rm — ‘{}’ \;

Details:

find: the UNIX command for finding files/directories/links etc.

/path/: the directory path where you are searching for old logs

-type f: specifying to check only for files.

-name ‘*.gz’: will find for the files that end with “.gz”.

-mtime +7: only consider the ones with modification time older than 7 days.

-execdir … \;: for each such result found, execute the following command.

rm — ‘{}’: remove the file; the {} part is where the output from previous command will be given as input.

  1. What is cron job and how to set it?

cron is a kind of a daemon that executes scripts or programs at specific time intervals.

These commands are called cron jobs.

This is like a scheduler for system admins and developers. Many use cases are present for which we can set cron jobs like log rotation,

emailing users to trigger any update for automatic data backup at any specified timings.

find the standard cron job writing style:- 

* * * * * script1

please find the details below for all the description for every *:-

# +—————- minute (0 – 59)

# |  +————- hour (0 – 23)

# |  |  +———- day of month (1 – 31)

# |  |  |  +——- month (1 – 12)

# |  |  |  |  +—- day of week (0 – 6) (Sunday=0)

# |  |  |  |  |

  *  *  *  *  *  command to be executed

if you want to save the output of the cron job, you can execute the below command.

Timing   Execute Path to script                   Output

* * * * * /usr/bin/scripts /var/www/html/crontest/cron.sh > /dev/null 2>&1

  1. How to copy the files in Linux?

cp command

we can use cp command to copy any data from the current directory to a different directory. 

For example:-the command cp test.txt /home/users/files would create a copy of test.txt at “/home/users/files”

  1. How to move the files in Linux?

mv command

mv command can be used to move files from one folder to another; This is also used to rename the files.

This works as cp command only. We need to type mv, the file’s name, and the destination’s directory. 

For example: mv file.txt /home/users/Docs.

  1. What is sudo command?

sudo stands for “SuperUser Do”, this command enables you to perform tasks that require administrative or root permissions. 

However, it is not recommended to use sudo command because most of the tasks would be performed as a particular user.

  1. Which command is used to check disk space and disk usage?

df command

df command is used to check the disk space, shown in percentage and KBs. 

If you want to see the report in megabytes, type df -m.

du command

this is used to check Disk Usage. 

However, the disk usage summary will show disk block numbers instead of the usual size format. 

If you want to see it in bytes, kilobytes, and megabytes, add the -h argument to the command line.

  1. How to check first few and last few lines of any linux file?

The head command is used to view the first lines of any text file. By default, it will show the first ten lines, 

but you can change this number to your liking. 

For example, if you only want to show the first five lines.

head -n 5 filename.ext.

tail command

This one has a similar function to the head command, but instead of showing the first lines, 

the tail command will display the last ten lines of a text file. 

tail -n filename.ext.

10. How to check the difference between two Linux files?

diff command

the diff command compares the contents of two files line by line. 

After analyzing the files, it will output the lines that do not match. 

Programmers often use this command when they need to make program alterations instead of rewriting the entire source code.The simplest form of this command

       diff file1.ext file2.ext

11  Which command is used to archive files in Linux?

The tar command is the most used command to archive multiple files into a tarball — a common Linux file format that is similar to zip format, 

with compression being optional.

12 How to kill a process?

If a process becomes unresponsive and you wish to kill that process. Get the pid of that process and kill using the below command.

Kill Sign option file1

Kill -9 file   ( 9 forces programs to stop immediately)

you also need to know the process identification number (PID) of the program you want to kill. If you don’t know the PID, simply run the command ps ux.

13 How to check connectivity of any linux  server?

Use the ping command to check your connectivity status to a server. 

For example, by simply entering ping example.com, 

the command will check whether you’re able to connect to the server and measure the response time.

14 How to download the file using CLI?

wget command is used to download the files from the internet.

15 How to check the OS version in Linux?

The “uname” command, short for Unix Name, will print detailed information about your Linux system like the machine name, operating system, kernel etc.

No comments:

Post a Comment