TECHY360
Everything You Need To Know About Tech

Linux command line basics question and answer for interview

0 53

This page contains exercises on basic commands for manipulating files and directories from the Linux command line. Each of these commands performs one simple action, but can later be used to compile more complex chains of commands.

Linux Commands
Linux Commands

Before you begin – the first simple question:

How to get help on the ssh command ?

man ssh

Basic commands for working with files

How to view the current working directory?

pwd

How to change the working directory? 

cd

for example, cd / var / www /

How to go to the parent directory? 

cd ..

How to return to your home directory? 

cd - invoking a command with no arguments results in switching to the directory specified in the $ HOME environment variable.

How to display a list of files in a directory? 

ls - list the files in the current directory
ls / var / log / - list files in the / var / log / directory
ls -l - display a detailed list of files

How to view the last change / access file /tmp/test.txt?

ls -l /tmp/test.txt - see the time of the last file change
ls -lu /tmp/test.txt - see the time of the last access to the file
stat /tmp/test.txt - shows 3 timestamps:
  • Access – the time of the last access to the file (any opening of the file for reading: viewing, searching the file, etc.)
  • Modify – last file modification time
  • Change – time of the file status change (access rights, owner, file change).

How to create a new test directory? 

mkdir test

How to create an empty file? 

There are many ways to create an empty file. Here are some of them:

  • cp empty.txt empty1.txt – copy any other empty file
  • touch empty.txt – “touch” a non-existent file
  • :> empty.txt – redirect the output of the command to the file, which does nothing
  • vi empty.txt, then immediately: wq – open the file and keep it empty

How to create a file /tmp/2mb.txt size of 2Mb? 

dd if = / dev / zero of = 2mb.txt bs = 1M count = 2

if in the previous command you specify bs = 1MB, then the file will be 2_000_000 bytes in size.

in this example, the file will be filled with zero bytes (0x00).

How to find out the file type? 

file

Related Posts
1 of 3

eg

$ file empty.txt 
empty.txt: empty 
$ file /usr/bin/perl 
/usr/bin/perl: symbolic link to `perl5.16.3' 
$ file /usr/bin/perl5.16.3 
/usr/bin/perl5.16.3: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, stripped 
$ file index.html 
index.html: HTML document, UTF-8 Unicode text, with very long lines 
$ file empty.txt 
empty.txt: empty 
$ file /usr/bin/perl 
/usr/bin/perl: symbolic link to `perl5.16.3' 
$ file /usr/bin/perl5.16.3 
/usr/bin/perl5.16.3: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.16, stripped 
$ file index.html 
index.html: HTML document, UTF-8 Unicode text, with very long lines 

How to rename a file? 

mv filename new_file_name

How to delete file / directory?

rm filename - delete file
rmdir directory_name or rm -r directory_name - delete directory
shred -uv filename - deleting a file for paranoids (without the -u option, simply overwrites the contents of the file with garbage)

How to create a symbolic / hard link to a file / directory?

ln -s filename_name link_name - create a symbolic link to a file
ln -s directory_name link_name - create a symbolic link to the directory
ln filename_name_list_name_ - create a hard link to the file

under normal conditions it is impossible to create a hard link to the directory

How to view file size? 

ls -l filename - file size in bytes
ls -lh filename - file size in a more familiar format

How to find out the size of the directory?

du -s directory_name is the size of the space in kilobytes (in blocks of 1024 bytes) that the directory with all its contents occupies on disk
du -sh directory_name - in a more familiar format

How to find out how much free space is left on a disk partition?

using the df command partition_mount_ or simply df
df -h will display sizes not in kilobytes, but in a more convenient format.
df -ih will display information on the number of used and free inodes

Work with text files

How to compare two text files?

diff file_1 file_2 or diff -a file_1 file_2

How to count the number of lines in a text file?

wc -l filename
or awk 'END {print NR}' filename
or you can open the file in a text editor vi filename and see the number of lines CTRL + G

How to display sorted lines of a text file?

sort filename

How to remove duplicate lines from a file? 

sort -u filename
or sort filename | uniq

How to add the contents of one text file to the end of the second?

cat file_1 >> file_2

How to split a text file into several 100 lines in each?

split -l 100 filename 100-

as a result of this command, files 100-aa, 100-ab … will appear in the directory containing 100 lines from the source file. If the source file contains a number of lines not multiple of 100, then in the last file there will be less than one hundred lines.

How to display the first 30 lines of a file?

head -n30 filename

How to display the last 30 lines of the file?

tail -n30 filename

How to view the contents of a text file?

cat filename - display the entire file
more filename - output the file to the screen page by page ( space - go to the next page, enter - one line down)
less filename - display the file on the screen with the ability to move up and down
vi filename or nano filename or emacs filename - open the file in your favorite text editor

How to display text file /tmp/file.txt lines starting with ‘START’? 

sed -n '/% $ @ ~ *! G4;:% # `START / p' filename
grep '% $ @ ~ *! G4;:% # `START' filename

How to display the contents of a text file without single-line comments (comment line starts with a # character)?

sed -n '/% $ @ ~ *! G4;:% # `[% $ @ ~ *! G4;:% #` #] / p' filename
grep -v '% $ @ ~ *! G4;:% # `#' filename

How among several files in the directory to find those that contain the word ‘test’?

grep 'test' * - search in all files in the current directory
grep 'test' file_1 file_2 file_3 - search only in the specified files

Get real time updates directly on you device, subscribe now.

Comments
Loading...

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More