Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Linux Man Page

The “man” is a short term for manual page. In unix like operating systems such as linux, man is an interface to view the system’s reference manual.

A user can request to display a man page by simply typing man followed by a space and then argument. Here its argument can be a command, utility or function. A manual page associated with each of these arguments is displayed.

If you will provide a section number in the command, then man will be directed to look into that section number of the manual and that section page will be displayed. And if not, then by default it will display the first page and you have to go through the entire sections in a pre-defined manner.

March 6, 2020

Linux File Content

There are many commands which help to look at the contents of a file. Now we’ll look at some of the commands like head, tac, cat, less & more and strings.

 

Head

The ‘head’ command displays the starting content of a file. By default, it displays starting 10 lines of any file.

head {filename}

// -n option followed by an integer specifying the number of lines to be shown.
head -n {number} {filename} 

// show content of multiple files to n line 
head -n {number} {filename1} {filename2} 

// -c option allows to print a specific number of bytes 
head -c {number} {filename} 

// show content of multiple files to number of bytes 
head -c {number} {filename1} {filename2}

Examples

head {filename1} {filename2}…

folaukaveinga@Folaus-MacBook-Pro-3 files % head  test.txt test2.txt 
==> test.txt <==
test
test1
sdf
ewr

sdfb
dcxzv

xcv
x

==> test2.txt <==
wer
qwe
rq
wer
qwe
rqwe
r
qwe
rq
wer

 

head -n {filename1} {filename2}…

folaukaveinga@Folaus-MacBook-Pro-3 files % head -n 5 test.txt
test
test1
sdf
ewr

folaukaveinga@Folaus-MacBook-Pro-3 files % head -n 5 test.txt test2.txt 
==> test.txt <==
test
test1
sdf
ewr


==> test2.txt <==
wer
qwe
rq
wer
qwe

head -c {number} {filename1} {filename2}…

folaukaveinga@Folaus-MacBook-Pro-3 files % head -c 10 test.txt         
test
test1%                                                                                                                                                                                                folaukaveinga@Folaus-MacBook-Pro-3 files % head -c 10 test.txt test2.txt
==> test.txt <==
test
test1
==> test2.txt <==
wer
qwe
rq%

 

Tail

Linux tail command is used to display the last ten lines of one or more files. Its main purpose is to read the error message. By default, it displays the last ten lines of a file. Additionally, it is used to monitor the file changes in real-time. The default use of the tail command displays the last ten lines of the files.

tail -n {number} {filename1} {filename2}…

Prints the last ‘n’ lines. n is mandatory to be specified in command otherwise it displays an error

folaukaveinga@Folaus-MacBook-Pro-3 files % tail -n 5 test.txt 
asdf
asd
fasd
fasd

tail -c {number} {filename1} {filename2}…

The ‘-c’ option displays the specified number of content for bytes from the last line.

folaukaveinga@Folaus-MacBook-Pro-3 files % tail -c 10 test.txt 
asd
fasd

tail -f {number} {filename1} {filename2}…

This option is mainly used by system administration to monitor the growth of the log files written by many Unix program as they are running. This option shows the last ten lines of a file and will update when new lines are added. As new lines are written to the log, the console will update with the new lines.

folaukaveinga@Folaus-MacBook-Pro-3 files % tail -f test.txt 
f
sad
f
i
asdf
asdf
asd
fasd
fasd



Cat

The ‘cat’ command can be used to display the content of a file, copy content from one file to another, concatenate the contents of multiple files, display the line number, display $ at the end of the line, etc.

 

cat {filename}

display all content of a file

folaukaveinga@Folaus-MacBook-Pro-3 files % cat test.txt 
test
test1
sdf
ewr
sdfb
dcxz
fasd

cat > {filename}

create a file

folaukaveinga@Folaus-MacBook-Pro-3 files % cat > test1.txt
easdf
asd
fasd
fasdf
^C
folaukaveinga@Folaus-MacBook-Pro-3 files % head test1.txt 
easdf
asd
fasd
fasdf

cat >> {filename}

>> appends input to the end of the file

folaukaveinga@Folaus-MacBook-Pro-3 files % cat >> test1.txt 
test123
sdfsdf^C

 

cat {filename1} {filename2}… > {newfilename}

copy contents of one file(filename1} to another file {newfilename}

with (-) a new line will be inserted while concatenating multiple files by using a hyphen (-).

folaukaveinga@Folaus-MacBook-Pro-3 files % cat test1.txt > test4.txt
folaukaveinga@Folaus-MacBook-Pro-3 files % ls
test		test.txt	test1.txt	test2.txt	test3.txt	test4.txt
folaukaveinga@Folaus-MacBook-Pro-3 files % head test4.txt 
easdf
asd
fasd
fasdf
test123

folaukaveinga@Folaus-MacBook-Pro-3 files % cat test1.txt test2.txt > test5.txt

// add new line \n to the end of test.txt content
folaukaveinga@Folaus-MacBook-Pro-3 files % cat - test1.txt test2.txt > test5.txt

 

cat -n {filename}

-n shows the number line

folaukaveinga@Folaus-MacBook-Pro-3 files % cat -n  test5.txt
     1	easdf
     2	asd
     3	fasd
     4	fasdf
     5	test123
     6	123
     7	123
     8	s
     9	df
    10	asd

 

cat -e {filename}

The ‘cat-e’ option displays a ‘$’ sign at the end of every line. This is very useful when you are looking for the end of a line.

folaukaveinga@Folaus-MacBook-Pro-3 files % cat -e test.txt 
test$
test1$
sdf$
ewr$
sdfb$
dcxzv$
$

cat {filename} | more

This gives you an option to paginate what to display.

cat test.txt | more

 

 

sort {filename}

Sort content of file alphabetically.

folaukaveinga@Folaus-MacBook-Pro-3 Downloads % sort test.txt 

as
asd
asd
asd
asd
asd
asdf
asdf

 

 

 

 

March 6, 2020

Linux File

In Linux system, a file doesn’t include only text files, images and compiled programs,  partitions, hardware device drivers and directories. Linux consider everything as as file are also files.

Create a text file

touch test.txt 

// using vi
vi tes.txt

// using vim
vim test.txt

 

Update content of an existing file

// using vi 
vi tes.txt 

// using vim 
vim test.txt

 

Delete a file with rm command

rm means remove. This command is used to remove a file. The command line doesn’t have a recycle bin or trash unlike other GUI’s to recover the files. Hence, be very much careful while using this command. Once you have deleted a file, it is removed permanently.

rm test.txt

// delete multiple files
rm test1.txt test2.txt

// delete files having same extension.
rm *.json

// delete a directory recursively
rm -r test

// delete files/directories recursively and forcefully
rm -rf 

Copy a file with cp command

cp stands for copy.

folaukaveinga@Folaus-MacBook-Pro-3 files % ls
test.txt
folaukaveinga@Folaus-MacBook-Pro-3 files % cp test.txt test1.txt
folaukaveinga@Folaus-MacBook-Pro-3 files % ls
test.txt	test1.txt

Copy multiple files

// copy all directories and files int the current directory to the Download directory
copy -R * /Users/folaukaveinga/Download

 

Move a file from one place to another with mv command

mv command is used to move existing file or directory from one location to another. It is also used to rename a file or directory. If you want to rename a single directory or file then mv option will be better to use.

mv file destination
folaukaveinga@Folaus-MacBook-Pro-3 files % ls
test		test.txt	test1.txt
folaukaveinga@Folaus-MacBook-Pro-3 files % mv *.txt test
folaukaveinga@Folaus-MacBook-Pro-3 files % ls -la
total 0
drwxr-xr-x  3 folaukaveinga  staff   96 Nov  7 22:47 .
drwx------@ 8 folaukaveinga  staff  256 Nov  7 20:42 ..
drwxr-xr-x  4 folaukaveinga  staff  128 Nov  7 22:47 test
folaukaveinga@Folaus-MacBook-Pro-3 files % cd test 
folaukaveinga@Folaus-MacBook-Pro-3 test % ls
test.txt	test1.txt
folaukaveinga@Folaus-MacBook-Pro-3 test %

Rename a file

To rename a file there are other commands also like ‘mv’. But ‘rename’ command is slightly advanced then others. This command will be rarely used and it works differently on different distros of linux.

Get file type

file command is used to determine the file type. It does not care about the extension used for file. It simply uses file command and tell us the file type.

folaukaveinga@Folaus-MacBook-Pro-3 files % file test.txt
test.txt: ASCII text

 

 

 

 

 

 

March 6, 2020

Linux Directory

A directory is a file which job is to store the file names and the related information of its content. All the files, whether ordinary, special, or directory, are contained in directories.

Unix uses a hierarchical structure for organizing files and directories. This structure is often referred to as a directory tree. The tree has a single root node, the slash character (/), and all other directories are contained below it.

Create a directory with mkdir command

With mkdir command, you can create a new directory wherever you want in your system. The mkdir stands for ‘make directory’. If you dont not provide a path then by default your file will be created in your current directory only. If you want to create your directory some where else, then provide the path of your destination directory and your file will be created there.

mkdir {directory-name}

// create a directory in a different directory from your current directory
mkdir /path/{directory-name} 

// create multiple directories
mkdir {directory-name1} {directory-name2}

Examples

root@ac0ab5ab9eaf:/home# mkdir site
root@ac0ab5ab9eaf:/home# ls
site
root@ac0ab5ab9eaf:/home# mkdir dir1 dir2
root@ac0ab5ab9eaf:/home# ls
dir1  dir2  site
root@ac0ab5ab9eaf:/home# 

 

Remove/Delete a directory with rmdir command

This command is used to delete a directory. But will not be able to delete a directory including a sub-directory. It means, a directory has to be empty to be deleted.

rmdir {directory-name}

//remove directory in a different location

rmdir /path/{directory-name}

// remove directory and its subdirectories
rmdir -p {directory-name}

 

List directories and files

The ls is the list command in Linux. It will show the full list or content of your directory. Just type ls and press the enter key. The whole content will be shown.

ls

//list out hidden directories and files. hidden files start with . (dot) symbol and they are not visible in the regular directory.
ls -a

//show the list in a long list format.
ls -l

folaukaveinga@Folaus-MacBook-Pro-3 ~ % ls -l 
total 0
drwx------@  4 folaukaveinga  staff   128 Aug 19 13:55 Applications
drwx------+ 14 folaukaveinga  staff   448 Nov  7 12:55 Desktop
drwx------@  5 folaukaveinga  staff   160 Oct 30 21:11 Documents
drwx------@ 21 folaukaveinga  staff   672 Nov  7 09:06 Downloads
drwx------@ 30 folaukaveinga  staff   960 Nov  6 22:39 Google Drive
drwxr-xr-x   6 folaukaveinga  staff   192 Nov  3 20:09 dumps

//show file size in Mb, Gb, Tb, etc.
ls -lh

folaukaveinga@Folaus-MacBook-Pro-3 ~ % ls -lh
total 0
drwx------@  4 folaukaveinga  staff   128B Aug 19 13:55 Applications
drwx------+ 14 folaukaveinga  staff   448B Nov  7 12:55 Desktop
drwx------@  5 folaukaveinga  staff   160B Oct 30 21:11 Documents
drwx------@ 21 folaukaveinga  staff   672B Nov  7 09:06 Downloads
drwx------@ 30 folaukaveinga  staff   960B Nov  6 22:39 Google Drive
drwxr-xr-x   6 folaukaveinga  staff   192B Nov  3 20:09 dumps

//Sort the list by displaying recently modified filed at top.
ls -lt

folaukaveinga@Folaus-MacBook-Pro-3 ~ % ls -lt
total 0
drwx------+ 14 folaukaveinga  staff   448 Nov  7 12:55 Desktop
drwxr-xr-x@ 10 folaukaveinga  staff   320 Nov  7 12:18 GitHub
drwx------@ 21 folaukaveinga  staff   672 Nov  7 09:06 Downloads

 

Navigation with cd command

To move around from one directory to another, use cd command. The “cd” stands for ‘change directory.’ It is one of the most frequently used commands in the Linux terminal.

// navigate from root folder and go home directory
cd /Users/folaukaveinga/

// nagivate from current directory to another directory within the current directory
cd site/src

// navigate back to parent folder
cd ..

//or

cd -

// navigate to home directory
cd ~

// navigate to a directory whose name has space
cd 'Dir name with space'  

//or

cd Dir\ name\ with\ space  

Show the current directory

pwd

Absolute Path

A path is a unique location to a file or a folder in a file system of an OS. A path to a file is a combination of / and alpha-numeric characters.

An absolute path is defined as the specifying the location of a file or directory from the root directory(/). In other words we can say absolute path is a complete path from start of actual filesystem from / directory or root directory.

// you are in home directory
folaukaveinga@Folaus-MacBook-Pro-3 ~ % pwd
/Users/folaukaveinga

// you want to navigate to the download directory(/Users/folaukaveinga/Downloads)
folaukaveinga@Folaus-MacBook-Pro-3 ~ % cd /Users/folaukaveinga/Downloads 
folaukaveinga@Folaus-MacBook-Pro-3 Downloads % pwd
/Users/folaukaveinga/Downloads

 

Relative Path

Relative path is defined as the path related to the current directory(pwd). See this example below.

// you are in home directory
folaukaveinga@Folaus-MacBook-Pro-3 ~ % pwd
/Users/folaukaveinga

// you want to navigate to the download directory(/Users/folaukaveinga/Downloads)
folaukaveinga@Folaus-MacBook-Pro-3 ~ % cd Downloads 
folaukaveinga@Folaus-MacBook-Pro-3 Downloads % pwd
/Users/folaukaveinga/Downloads

Linux Directories

From top to bottom, the directories you are seeing are as follows.

/bin

/bin is the directory that contains binaries, that is, some of the applications and programs you can run. You will find the ls program mentioned above in this directory, as well as other basic tools for making and removing files and directories, moving them around, and so on. There are more bin directories in other parts of the file system tree, but we’ll be talking about those in a minute.

/boot 

The /boot directory contains files required for starting your system. Do I have to say this? Okay, I’ll say it: DO NOT TOUCH!. If you mess up one of the files in here, you may not be able to run your Linux and it is a pain to repair. On the other hand, don’t worry too much about destroying your system by accident: you have to have superuser privileges to do that.

/dev

/dev contains device files. Many of these are generated at boot time or even on the fly. For example, if you plug in a new webcam or a USB pendrive into your machine, a new device entry will automagically pop up here.

/etc 

/etc is the directory where names start to get confusing. /etc gets its name from the earliest Unixes and it was literally “et cetera” because it was the dumping ground for system files administrators were not sure where else to put.

Nowadays, it would be more appropriate to say that etc stands for “Everything to configure,” as it contains most, if not all system-wide configuration files. For example, the files that contain the name of your system, the users and their passwords, the names of machines on your network and when and where the partitions on your hard disks should be mounted are all in here. Again, if you are new to Linux, it may be best if you don’t touch too much in here until you have a better understanding of how things work.

/home

/home is where you will find your users’ personal directories. In my case, under /home there are two directories: /home/paul, which contains all my stuff; and /home/guest, in case anybody needs to borrow my computer.

/lib

/lib is where libraries live. Libraries are files containing code that your applications can use. They contain snippets of code that applications use to draw windows on your desktop, control peripherals, or send files to your hard disk.

There are more lib directories scattered around the file system, but this one, the one hanging directly off of / is special in that, among other things, it contains the all-important kernel modules. The kernel modules are drivers that make things like your video card, sound card, WiFi, printer, and so on, work.

/media 

The /media directory is where external storage will be automatically mounted when you plug it in and try to access it. As opposed to most of the other items on this list, /media does not hail back to 1970s, mainly because inserting and detecting storage (pendrives, USB hard disks, SD cards, external SSDs, etc) on the fly, while a computer is running, is a relatively new thing.

/mnt 

The /mnt directory, however, is a bit of remnant from days gone by. This is where you would manually mount storage devices or partitions. It is not used very often nowadays.

 /opt 

The /opt directory is often where software you compile (that is, you build yourself from source code and do not install from your distribution repositories) sometimes lands. Applications will end up in the /opt/bin directory and libraries in the /opt/lib directory.

A slight digression: another place where applications and libraries end up in is /usr/local, When software gets installed here, there will also be /usr/local/bin and /usr/local/lib directories. What determines which software goes where is how the developers have configured the files that control the compilation and installation process.

/proc

/proc, like /dev is a virtual directory. It contains information about your computer, such as information about your CPU and the kernel your Linux system is running. As with /dev, the files and directories are generated when your computer starts, or on the fly, as your system is running and things change.

/root 

/root is the home directory of the superuser (also known as the “Administrator”) of the system. It is separate from the rest of the users’ home directories BECAUSE YOU ARE NOT MEANT TO TOUCH IT. Keep your own stuff in you own directories, people.

/run

/run is another new directory. System processes use it to store temporary data for their own nefarious reasons. This is another one of those DO NOT TOUCH folders.

/sbin

/sbin is similar to /bin, but it contains applications that only the superuser (hence the initial s) will need. You can use these applications with the sudo command that temporarily concedes you superuser powers on many distributions. /sbin typically contains tools that can install stuff, delete stuff and format stuff. As you can imagine, some of these instructions are lethal if you use them improperly, so handle with care.

/usr 

The /usr directory was where users’ home directories were originally kept back in the early days of UNIX. However, now /home is where users kept their stuff as we saw above. These days, /usr contains a mish-mash of directories which in turn contain applications, libraries, documentation, wallpapers, icons and a long list of other stuff that need to be shared by applications and services.

You will also find binsbin and lib directories in /usr. What is the difference with their root-hanging cousins? Not much nowadays. Originally, the /bin directory (hanging off of root) would contain very basic commands, like lsmv and rm; the kind of commands that would come pre-installed in all UNIX/Linux installations, the bare minimum to run and maintain a system. /usr/bin on the other hand would contain stuff the users would install and run to use the system as a work station, things like word processors, web browsers, and other apps.

But many modern Linux distributions just put everything into /usr/bin and have /bin point to /usr/bin just in case erasing it completely would break something. So, while Debian, Ubuntu and Mint still keep /bin and /usr/bin (and /sbin and /usr/sbin) separate; others, like Arch and its derivatives just have one “real” directory for binaries, /usr/bin, and the rest or *bins are “fake” directories that point to /usr/bin.

/srv

The /srv directory contains data for servers. If you are running a web server from your Linux box, your HTML files for your sites would go into /srv/http (or /srv/www). If you were running an FTP server, your files would go into /srv/ftp.

/sys

/sys is another virtual directory like /proc and /dev and also contains information from devices connected to your computer.

In some cases you can also manipulate those devices. I can, for example, change the brightness of the screen of my laptop by modifying the value stored in the /sys/devices/pci0000:00/0000:00:02.0/drm/card1/card1-eDP-1/intel_backlight/brightness file (on your machine you will probably have a different file). But to do that you have to become superuser. The reason for that is, as with so many other virtual directories, messing with the contents and files in /sys can be dangerous and you can trash your system. DO NOT TOUCH until you are sure you know what you are doing.

/tmp

/tmp contains temporary files, usually placed there by applications that you are running. The files and directories often (not always) contain data that an application doesn’t need right now, but may need later on.

You can also use /tmp to store your own temporary files — /tmp is one of the few directories hanging off / that you can actually interact with without becoming superuser.

/var

/var was originally given its name because its contents was deemed variable, in that it changed frequently. Today it is a bit of a misnomer because there are many other directories that also contain data that changes frequently, especially the virtual directories we saw above.

Be that as it may, /var contains things like logs in the /var/log subdirectories. Logs are files that register events that happen on the system. If something fails in the kernel, it will be logged in a file in /var/log; if someone tries to break into your computer from outside, your firewall will also log the attempt here. It also contains spools for tasks. These “tasks” can be the jobs you send to a shared printer when you have to wait because another user is printing a long document, or mail that is waiting to be delivered to users on the system.

Your system may have some more directories we haven’t mentioned above. In the screenshot, for example, there is a /snap directory. That’s because the shot was captured on an Ubuntu system. Ubuntu has recently incorporated snap packages as a way of distributing software. The /snap directory contains all the files and the software installed from snaps.

March 6, 2020

Linux Introduction

Linux is being used in our everyday lives in almost every device. As a developer it’s very important that you understand the basic use of linux for your day to day work. Some of your daily tasks may not require command line interaction as GUI for development tools rises but the 20% of the time we need command line interaction we have to know how linux works.

 

March 6, 2020