Linux interview questions for beginners - part2

Ques.26. What is the purpose of 'touch' command?

Ans. Touch command serve two purpose in Linux-

  1. With a new file name, touch is used to create an empty file.
    touch newFile
  2. For existing files or directories, touch will change the last access time to current time.

Ques.27. What is the difference between 'cp' and 'mv' command?

Ans. The 'cp' command is used to copy content of one file to another. Whereas, 'mv' command moves a file from one location to another, deleting the source file. 'mv' command is also used for renaming files. Usage-

#Copies content of file1 to file2
cp file1 file2

#Moves/renames file1 to file2
mv file1 file2

Ques.28. Explain the usage of 'head' and 'tail' commands.

Ans. Head command is used to display first few lines(default 10) of a file. Whereas, tail is used to display last few lines(default 10) of a file. Usage-

#Display first 10 lines of a file
head fileA

#Display first 5 lines of a file
head -5 fileA

#Display last 10 lines of a file
tail fileA

#Display last 5 lines of a file
tail -5 fileA

Ques.29. What is 'sed' in Linux?

Ans. Sed stands for Stream Editor. It is used to edit a file or input from a pipeline. Example-

#Prints first 5 lines
sed -n 1,5p fileA.txt

Ques.30. How to print content of line 5 to 10 from one file to another?

Ans. Using 'sed'

#'p' to print and '-n' to not print each line
sed -n 5,10p fileA.txt > fileB.txt

Ques.31. What is 'awk'?

Ans. AWK named after the initials of its authors "Aho, Weinberger, and Kernighan", is an interpreted programming language for text processing. Usage-

#To display the content of file using awk
awk '{print}' fileA.txt

Ques.32. How to count the number of words, lines and characters in a file?

Ans. Using wc command (word count)-

#Number of characters
wc -m fileA.txt

#Number of words
wc -w fileA.txt

#Number of lines
wc -l fileA.txt

Ques.33. How to combine the content of multiple files into a single file?

Ans. Using 'cat' and redirection-

#merging content of fileA and fileB into mergedFile
cat file1 file2 > mergedFile

Ques.34. What is use of 'tac' command?

Ans. Tac command as the name suggests, is the reverse of 'cat' command. It displays the content of file in reverse order - line by line, displaying the last line first and moving up till the first line.

#Displays the content of fileA from the bottom line to top line
tac file!

Ques.35. What is difference between 'more' and 'less' command?

Ans. Both more and less commands are used for viewing large files page wise. Unlike any editor like vi, using these commands for opening a file does not load the whole file in memory. One limitation of more command is, we can only scroll down but not up. Whereas, using less command, we can scroll both upwards and downwards in a file.

Ques.36. How can we find the top 3 space consuming file or directories within a directory?

Ans. Using du(disk usage) command, we can find the disk usage of all directories and sub-directories. Using du with -a option, will list the disk usage of all files as well, along with the directories. The '-h' option displays the size in human readable form. Later, we can use this command along with sort and head command, to first sort the files/directories by size and then fetch the required number of files.

#Sorts the files and directories within artOfTesting directory by size
du -h -a artOfTesting/ | sort -n -r | head -n 3

Ques.37. How to create aliases in Linux?

Ans. Aliases are used to create aliases or some abbreviated short-name for a command or a group of commands.

#Alias for some log directory
alias logs="cd /user/application/logs"

Ques.38. What is the use of shred utility? How it is different from rm command?

Ans. Shred utility is used for deleting the content of file in a such a way that the content cannot be recovered using some data recovery tools/utilities. Whereas, when we delete a file using rm command, the space taken by the file is marked available for other files, hence there is possibility to recover the content of the file. Thus, shred is better for deleting files with sensitive data.

#Overwrites the content of the file to make it impossible to recover
shred file1

Ques.39. What is the difference between find and locate command?

Ans. Both find and locate commands are used for searching files on Linux. The 'find' command searches file in real time and thus take longer time to return the search result in case there are too many files to scan through. Whereas, 'locate' command is much faster than find as instead of real time search it looks up in a database - updatedb. Since, the updatedb takes snapshot of the file system only once a day output of locate is not always accurate or updated.

#Find all text files
find / *.txt

#Find all text files
locate "*.txt"

Ques.40. How can we introduce wait time in scripts?

Ans. Sleep command can be used to introduce wait time in scripts. We can specify wait in terms of seconds, minutes, hours and days.

#Wait for 5 seconds
sleep 5s

#Wait for 5 minutes
sleep 5m

#Wait for 5 hours
sleep 5h

#Wait for 5 days
sleep 5d

Ques.41. How to compress and decompress files in Linux?

Ans. Using 'zip' and 'unzip' commands, we can compress and decompress files in Linux.

#Creates a file with fileA and fileB compressed
zip zipFile fileA fileB


Ques.42. What are the different file permissions or access modes in Linux?

Ans. Each file and directory in Linux can have following 3 permissions, represented by a three digit octal number-

  1. Read - Provides the ability to read the content of a file (represented by 'r' in first position - "r--")
  2. Write - Provides the ability to edit or delete the content of a file (represented by 'r' in second position - "-w-")
  3. Execute - Provides the ability to execute or run a file (represented by 'x' in third position - "--x")

Ques.43. Explain the meaning of the permission mode "drwxr-x--x".

Ans. The 10 characters of permission mode provides information about the permission to the user, group and other users.

  • 1st character indicates whether the given thing is file or directory, '-' for file and 'd' for directory.
  • Next 3 characters indicate the read-write-execute permission to the user or owner of the file.
  • Next 3 characters indicate the read-write-execute permission to the group belonging to the file.
  • Last 3 characters indicate the read-write-execute permission to all the other users.

So, the permission mode "drwxr-xr-x", means a directory for which the user have all the read-write-excute permission, group has read and execute permission and other users can only execute the file.

Ques.44. How can we grant or remove access to a file in Linux?

Ans. The 'chmod' command is used to grant permission to a file or directory in Linux. There are two modes to grant or remove permission using chmod-

  1. Symbolic mode - With symbolic mode we can grant permission to a file using '+' operator, remove permission using '-' operator and set the required permission using '=' operator.
    #Add read and execute permission to group
    chmod g+rx fileA
    #Remove execute permission to owner
    chmod u-x fileA
    #Set write and execute permission to other users
    chmod o = wx fileA
  2. Absolute mode - With absolute mode, an octal number a used to specify different permissions.
    #751 will grant permission 7 to owner, 5 to group and 1 to other users
    chmod 751 fileA

Ques.45. Explain the octal number representation for different permission types in Linux.

Ans. The absolute permission of chmod uses an octal number. The different octal numbers for different permission types are-

Octal number Permission granted
0 No permission (---) i.e. 0+0+0=0
1 Execute (--x) i.e. 0+0+1=1
2 Write (-w-) i.e. 0+2+0=2
3 Write and execute (-wx) i.e. 0+2+1=3
4 Read (r--) i.e. 4+0+0=4
5 Read and execute (r-x) i.e. 4+0+1=5
6 Read and write (rw-) i.e. 4+2+0=6
7 Read, write and execute (rwx) i.e. 4+2+1=7

Ques.46. How can we change ownership and group ownership of a file in Linux?

Ans. The 'chown' command (change owner) is used to change the ownership of a file.

chown userK fileA

The 'chgrp' command is used to change the group ownership of a file.

chgrp group fileA

Ques.47. How to sort the contents of a file?

Ans. Sort command can be used to sort the elements of a file line wise.

#File content - kuldeep, abc, 1India, zed each in separate line 
sort file



Ques.48. How can we remove the duplicates in a file?

Ans. Using 'uniq' utility with 'sort' command.

#File content - bat, all, cat, cat, all, cat each in separate line 
sort fileWithDuplicateContent | uniq



Ques.49. What are soft links in Linux? What are its advantage over cp command?

Ans. Soft links are also known as symbolic links or symlinks. These are used to create a symbolic links or pointers to a file. In Linux, soft links are created using 'ln' command with '-s' option.

#Creates a soft link 'fileA' to the file 'fileA.txt'
#'-s' option to create a soft link
ln -s fileA.txt fileA

It has got advantage over cp command (which creates a new copy of the file) in the sense that in order to change the permission to the files, with links only the permission of the original file need to be changed. Whereas, with cp command, the permission to all the copies of the file need to be changed.

Ques.50. What are hard links in Linux? How it is different from soft link?

Ans. Hard links are like another name for an existing file. Instead of pointing to a name(like soft link), hard links point to the content of the file directly(point to inode - a table containing metadata about a file).

#Creates a hard link 'fileA' to the file 'fileA.txt'
ln fileA.txt fileA

It is different from soft link in the sense that in case the original files gets deleted its symbolic link will not point to anything and thus lead to "No such file or directory" error. Whereas, the hard link will still point to the content of the original file even if the file gets deleted.

#Create hard-link to file1
ln file1 hardF1

#Create soft-link to file1
ln file1 softF1

#Delete original file
rm file1

#Check content of the hard link - hardF1
cat hardF1
#output -> valid content

#Check content of the soft link - softF1
cat softF1
#output -> "No such file or directory"