linux commands
linux commands
Linux is an operating system, a software program that controls your computer. Most vendors load an
operating system onto the hard drive of a PC before delivering the PC, so, unless the hard drive of your
PC has failed, you may not understand the function of an operating system.
An operating system solves several problems arising from hardware variation. As you're aware, no two
PC models (or models of other computers, for that matter) have identical hardware. For example, some
PCs have an IDE hard drive, whereas others have a SCSI hard drive. Some PCs have one hard drive,
others have two or more. Most PCs have a CD-ROM drive, but some do not. Some PCs have an Intel
Pentium CPU, whereas others have an AMD K-6, and so on. Suppose that, in a world without operating
systems, you're programming a new PC application, perhaps a new multimedia word processor. Your
application must cope with all the possible variations of PC hardware. As a result, it becomes bulky and
complex. Users don't like it because it consumes too much hard drive space, takes a long time to load,
and - because of its size and complexity - has more bugs than it should.
Operating systems solve this problem by providing a single standard way for applications to access
hardware devices. When an operating system exists, applications can be more compact, because they
share the commonly used code for accessing the hardware. Applications can also be more reliable
because this code is written only once, and by expert programmers, rather than by every application
programmers.
As you'll soon learn, operating systems do many other things as well; for example, they generally provide
a filesystem so that you can store and retrieve data, and a user interface so that you can control the
operation of your computer. However, if you think of a computer's operating system as its subconscious
mind, you won't be far off the mark. It's the computer's conscious mind - applications such as word
processors and spreadsheets - that do useful work. But, without the subconscious - the operating
system - the computer would cease breathing and applications would not function.
Linux is distinguished from many popular operating systems in three important ways.
Linux is a cross-platform operating system that runs on many computer models. Only Unix, an
ancestor of Linux, rivals Linux in this respect. In comparison, Windows 95 and Windows 98 run
only on CPUs having the Intel architecture. Windows NT runs only on CPUs having the Intel
architecture or the DEC Alpha.
Linux is free, in two senses. First, you may pay nothing to obtain and use Linux. On the other
hand, you may choose to purchase Linux from a vendor who bundles Linux with special
documentation or applications, or who provides technical support. However, even in this case, the
cost of Linux is likely to be a fraction of what you'd pay for another operating system. So, Linux is
free or nearly free in an economic sense.
Second, and more important, Linux and many Linux applications are distributed in source form.
This makes it possible for you and others to modify or improve them. You're not free to do this
with most operating systems, which are distributed in binary form. For example, you can't make
changes to Microsoft Windows or Microsoft Word - only Microsoft can do that. Because of this
freedom, Linux is being constantly improved and updated, far outpacing the rate of progress of
any other operating system. For example, Linux will likely be the first operating system to support
Intel's forthcoming Merced 64-bit CPU.
Linux has attractive features and performance. Free access to Linux source code lets
programmers around the world implement new features, and tweak Linux to improve its
performance and reliability. The best of these features and tweaks are incorporated in the
standard Linux kernel or made available as kernel patches or applications. Not even Microsoft
can mobilize and support a software development team as large and dedicated as the volunteer
Linux software development team, which numbers in the hundreds of thousands, including
programmers, code reviewers, and testers.
Linux traces its ancestry back to a mainframe operating system known as Multics (Multiplexed Information
and Computing Service). Begun in 1965, Multics was one of the first multi-user computer systems and
remains in use today. Bell Telephone Labs participated in the development of Multics, along with the
Massachusetts Institute of Technology and General Electric.
Two Bell Labs software engineers, Ken Thompson and Dennis Richie, worked on Multics until Bell Labs
withdrew from the project in 1969. One of their favorite pastimes during the project had been playing a
multi-user game called Space Travel. Now, without access to a Multics computer, they found themselves
unable to indulge their fantasies of flying around the galaxy. Resolved to remedy this, they decided to port
the Space Travel game to run on an otherwise unused PDP-7 computer. Eventually, they implemented a
rudimentary operating system they named Unics, as a pun on Multics. Somehow, the spelling of the name
became Unix.
Their operating system was novel in several respects, most notably portability. Most previous operating
systems had been written for a specific target computer. Just as a tailor-made suit fits only its owner, such
an operating system could not be easily adapted to run on an unfamiliar computer. In order to create a
portable operating system, Ritchie and Thompson first created a programming language, called C. Like
assembly language, C let a programmer access low-level hardware facilities not available to
programmers writing in a high-level language such as FORTRAN or COBOL. But, like FORTRAN and
COBOL, a C program was not bound to a particular computer. Just as a ready-made suit can be
lengthened or shortened here and there to fit a purchaser, writing Unix in C made it possible to easily
adapt Unix to run on computers other than the PDP-7.
As word of their work spread and interest grew, Ritchie and Thompson made copies of Unix freely
available to programmers around the world. These programmers revised and improved Unix, sending
word of their changes back to Ritchie and Thompson, who incorporated the best such changes in their
version of Unix. Eventually, several Unix variants arose. Prominent among these was BSD (Berkeley
Systems Division) Unix, written at the University of California, Berkeley, in 1978. Bill Joy, one of the
principals of the BSD project, later became a founder of Sun Microsystems, which sold another Unix
variant (SunOS) to power its workstations. In 1984, AT&T, the parent company of Bell Labs, began selling
its own version of Unix, known as System V.
What Ritchie and Thompson had begun in a distinctly non-commercial fashion ended up spawning
several legal squabbles. When AT&T grasped the commercial potential of Unix, it claimed Unix as its
intellectual property and began charging a hefty license fee to those who wanted to use its Unix. Soon,
others who had implemented Unix-like operating systems were distributing licenses only for a fee.
Understandably, those who had contributed improvements to Unix considered it unfair for AT&T and
others to appropriate the fruits of their labors. This concern for profit was unlike the democratic, share-
and-share-alike spirit of the early days of Unix.
Some, including MIT scientist Richard Stallman, yearned for the return of those happier times and the
mutual cooperation of programmers that then existed. So, in 1983, Stallman launched the GNU (GNU's
not Unix) project, which aimed at creating a free Unix-like operating system. Like early Unix, the GNU
operating system was to be distributed in source form so that programmers could read, modify, and
redistribute it without restriction. Stallman's work at MIT had taught him that, by using the Internet as a
means of communication, programmers the world over could improve and adapt software at incredible
speed, far outpacing the fastest rate possible using traditional software development models, in which few
programmers actually see one another's source code.
As a means of organizing work on the GNU project, Stallman and others created the Free Software
Foundation (FSF), a non-profit corporation that seeks to promote free software and eliminate restrictions
on the copying, redistribution, understanding, and modification of software. Among other activities, the
FSF accepts tax-deductible charitable contributions and distributes copies of software and documentation
for a small fee, using this revenue to fund its operations and support the GNU project.
If you find it peculiar that the FSF charges a fee - even a small fee - for "free" software, you should
understand that the FSF intends the word free to refer primarily to freedom, not price. The FSF believes in
three fundamental software freedoms:
You can copy GNU software and give it away to anyone you choose.
If you're a programmer, you can modify GNU software any way you like, because you have
access to the source code.
You can distribute improved versions of GNU software. However, you cannot charge anyone a
fee for using your improved version (although you can charge a fee for providing a user with a
physical copy of your software).
The origins of Linux and the availability of its source code set it apart from other operating systems. But
most users choose an operating system based on features and performance - and Linux delivers these in
spades. Table 1.4compares certain features and performance characteristics of Linux with those of
Microsoft Windows NT 4.0 and Sun Microsystems Solaris 2.6.[ 5] Each of these three operating systems
can be run on an Intel-architecture PC.
As you can see, Linux fares well in this comparison. It runs on a wider range of hardware platforms and
runs adequately on less costly and powerful systems. Moreover, the typical downtime of a Linux system is
less than that of a Windows NT system and its performance surpasses that of a Solaris system. Its multi-
processing capabilities exceed those of Windows NT and its support of advanced TCP/IP networking
facilities is superior to that of Windows NT and Solaris. As a group, Linux users are more satisfied than
Windows NT users and Solaris users. Linux source code is readily available. And, the Linux installed
base dwarfs that of Solaris and approaches that of Windows NT.
But this impressive inventory of selling points doesn't end the matter. Let's consider some other technical
characteristics of Linux that distinguish it from the pack. Foremost in the minds of many is the low cost of
Linux. Comparable server operating systems can cost more than $100,000. The low cost of Linux makes
it practical for use even as a desktop operating system. In that mode, it truly eclipses the competition.
Many desktop systems are occasionally, even regularly, employed as servers. Because Linux was
designed for use as a server operating system, its features and performance readily outshine those of
desktop operating systems used as makeshift servers. For example, Microsoft's software license for
Windows NT Workstation restricts the number of simultaneous client connections to 10; if your Windows
NT Workstation computer accepts more than 10 client connections, it is operating in breach of license.
However, Linux imposes no such restriction: your Linux desktop is free to accept as many client
connections as you think it can handle.
Again, because it was designed as a server, Linux provides more reliable data storage than competing
desktop operating systems. Most Linux users store their disk data using the EXT2 filesystem, which is
superior in performance and reliability to filesystems (partition types) provided by Microsoft operating
systems, including FAT, FAT32, and NTFS.
This section introduces you to the cycle of Linux system use. If you're a user of Microsoft Windows, you're
accustomed to a pattern of system use that forms a cycle:
The cycle of Linux system use is similar, even though you perform the tasks somewhat differently.
4.1.1 Booting the System
Most Linux users boot their system from its hard drive. Of course, if you made a boot diskette during
system installation, you can use it to boot your system.
First, you must prepare your system for booting. If your system is running, you must shut it down by
following the proper procedure for shutting down the operating system that's active. For example, if you're
running Microsoft Windows, click Start Shut Down and select the Shut Down option in the Shut Down
dialog box. Press OK to begin the system shutdown. After a few seconds, Windows displays a screen
telling you that it's safe to turn off power to your system. Turn off the power or, if your system
automatically powers down, wait a few seconds until the system powers itself down.
Next, you must set your system to boot from the desired device. To boot your system from its hard drive,
remove any floppy diskette from your system's floppy drive. To boot your system from a floppy diskette,
insert your Linux boot diskette into your system's floppy drive.
Now, you're ready to boot your system. Switch your system on (or press your system's reset button, if
your system is powered on) and watch as it performs its self test. Shortly thereafter, you should see
a boot: prompt on the system's monitor. If you like, you can list the available boot configurations stored on
the boot device by pressing Tab. To boot the system, type the name of the desired configuration and
press Enter, or simply press Enter to boot using the default configuration.
Once it loads, Linux begins probing your system and its devices, printing status information on your
system's monitor. This status information is helpful if your system fails to boot properly, because it
discloses the point in the boot process where the problem occurred.
When Linux has completed its boot process, your system's monitor will display a login prompt similar to
this:
4.1.2 Logging In
Before you can use the system, you must identify yourself by logging in. The install program created a
special user named root; by identifying yourself as the root user, you can gain access to the system.
Normally, you use the rootuserid only when performing system administration tasks, because
the root user has special capabilities that other users lack.
To log on, type root and press Enter. The system prompts you for the password associated with
the root userid. Type the password you established during the installation process and press Enter. To
prevent anyone nearby from learning your password, Linux does not display it as you type. If you suspect
you've typed it incorrectly, simply press Enter and start over; or press Backspace once (or more) for
each character you've entered and then re-enter it. If you type the userid or password incorrectly, Linux
displays the message "login incorrect" and prompts you to try again.
Like other members of the Unix family, the Linux operating system is case sensitive. Be sure to type the
userid root just as it appears, using all lowercase characters. Similarly, you must type the password
exactly as you entered it in the Root Password dialog box during system installation.
Also, some Linux programs require you to type Ctrl-BACKSPACE, rather than BACKSPACE. If you press
BACKSPACE and see ^H echoed to the console, try pressing Ctrl-BACKSPACE instead.
When you've successfully logged in, you'll see a command prompt that looks something like this:
root@desktop:/root#
This prompt tells you that the Linux bash shell is ready to accept your commands.
The component of Linux that interprets and executes commands is called the shell. Linux supports a
variety of different shells, but the most popular is the bash shell. This chapter presents the basics of using
the bash shell; you'll learn more about the shell in Chapter 13, Conquering the BASH Shell.
The Linux bash shell presents the user with a command-line interface (CLI). CLIs are familiar to Windows
users who have worked in the MS-DOS Prompt window, and indeed the Microsoft Windows MS-DOS
Prompt window is a kind of command-line shell for Windows. The Linux bash shell works much like the
MS-DOS Prompt window. You type text commands and the system responds by displaying text replies.
As your first Linux command, type w and press Enter. Your screen should look something like this:
root@desktop:/root#
w
11:12am up 6 min, 1 user, load average: 0.00, 0.08, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root tty1 11:13am 0.00s 0.20s 0.11s -bash
The w command tells Linux to display the system status and a list of all system users. In the example, the
output of the command tells you that it's now 11:12 a.m., that the system has been up for 6 minutes, and
that only one user -root - is currently logged in. Notice that the command output is very terse, packing
much information into a few lines. Such output is typical of Linux commands. At first, you may find Linux
output cryptic and difficult to read, but over time you'll grow to appreciate the efficiency with which Linux
communicates information.
Linux provides many commands besides the w command; so many that you may despair of learning and
recalling them. Actually, the number of commands you'll use regularly is fairly small. Soon, these will
become second nature to you.
root@desktop:/root#
date
Tue Feb 23 11:15:20 PST 1999
If you find working with MS-DOS distasteful or intimidating, you may not immediately enjoy working with
the Linux command line. However, give yourself some time to adjust. The Linux command line has
several features that make it easier to use, and more powerful, than MS-DOS. If, after working with the
Linux command line for several days, you don't find yourself at home, don't despair. Linux provides a
graphical user interface in addition to its command-line interface. You'll learn about the graphical user
interface in Chapter 6, Using the X Window System.
4.1.4 Correcting Commands
Sometimes you may type a command incorrectly, causing Linux to display an error message. For
example, suppose you typed dat instead of date:
root@desktop:/root#
dat
bash: dat: command not found
In such a case, carefully check the spelling of the command and try again. If you notice an error before
pressing Enter, you can use the Backspace key to return to the point of the error and then type the
correct characters.
Just as a web browser keeps track of recently visited sites, Linux's BASH shell keeps track of recently
issued commands. This list is called the history list, and you can scroll back through it using the Up arrow
key, or back down using the Down arrow key, just as you would with the Back and Forward buttons on a
web browser. In fact, the history list provides several powerful ways to remember and reuse frequently
issued commands, as we'll see inChapter 13.
The Up and Down arrow keys let you scroll through a list of commands recently issued. This feature is
handy when you want to repeat a command. Simply use the Up arrow key to find the command and
press Enter to re-execute it. You can also use this feature when you want to issue a command similar to
one you recently issued. Use the Up arrow key to find the original command. Then, use the Left and Right
arrow keys to position the cursor and make whatever changes to the command you like. Finally,
press Enter to execute the command.
In Microsoft Windows, you can have several MS-DOS Prompt windows simultaneously active. Although
the bash shell doesn't have a graphical user interface, you can nevertheless work with several instances
of the shell, by using Linux virtual consoles. Linux provides six virtual consoles; you can use special
keystrokes to switch between them. The keystroke Alt-F n, where n is the number of a virtual console (1-
6), causes Linux to display virtual console n. For example, you can display virtual console 2 by typing Alt-
F2. You can view only a single console at a time, but you can switch rapidly between consoles by using
the appropriate keystroke.
Virtual consoles are handy when you've started a time-consuming task and want to be able to perform an
unrelated task while the original task is working. You'll also find them useful after you've established
several userids on your system, because you can log on as one userid on one virtual console while you're
logged on as another userid on a different console.
Virtual consoles have a screen saver feature like that found on Microsoft Windows. If a virtual console is
inactive for an extended period, Linux blanks the monitor screen. To restore the screen without disturbing
its contents, press the Shift key.
When you're done using a virtual console, you should log out by typing the command exit and
pressing Enter. When you log out, the system frees memory and other resources that were allocated
when you logged in, making those resources available to other users.
When the system logs you out, it immediately displays a login prompt. If you change your mind and want
to access the system, you can login simply by supplying your userid and password.
You shouldn't turn off power to a computer while it's running Linux; instead, you should shut down the
operating system and then turn off power. To shut down a Linux system, you use
the shutdown command, which resides in a directory named /sbin:
root@desktop:/root#
/sbin/shutdown -h now
Don't type the prompt, which automatically appears on the command line. Only the root user can issue
the shutdown command. If you want to restart a Linux system, you can use an alternative form of
the shutdown command:
root@desktop:/root#
/sbin/shutdown -r now
Or, even more conveniently, you can use the familiar MS-DOS "three-finger salute": Ctrl-Alt-Del, which
simply issues a shutdown command on your behalf.
When you shut down a system, Linux automatically logs off all users, terminates all running programs,
and closes all open files. Before shutting down a system, you should check each virtual console to
determine if an important operation is in progress. If so, you should delay shutting the system down until
the operation completes.
To make Linux commands easy to use, they share a simple, common structure. This section describes
their common structure and explains how you can obtain helpful information on the commands available
to you.
The command identifies the command you want Linux to execute. The name of a Linux command almost
always consists of lowercase letters and digits. Remember that, unlike Microsoft Windows, Linux is case
sensitive; be sure to type each character of a command in the proper case.
Most commands let you specify options or arguments. However, in any given case, you may not need to
do so. For example, typing the w command without options and arguments causes Linux to display a list
of current users.
Options modify the way that a command works. Most options consist of a single letter, prefixed by a dash.
Often, you can specify more than one option; when you do so, you separate each option with a space or
tab. For example, the -h option of the w command causes the output of the command to omit the header
lines that give the time and the names of the fields. Typing:
root@desktop:/root#
w -h
Arguments specify filenames or other targets that direct the action of the command. For example,
the w command lets you specify a userid as an argument, which causes the command to list only logins
that pertain to the specified userid. Typing:
root@desktop:/root#
w root
prints a list of current logins by the root user. Some commands let you specify a series of arguments; you
must separate each argument with a space or tab.
Because Linux provides so many commands and because Linux commands provide so many possible
options, you can't expect to recall all of them. To help you, Linux provides the man command and
the apropos command, which let you access a help database that describes each command and its
options.
Each Linux command is described by a special file called a manual page. The manual pages are stored in
a group of subdirectories comprising a help database. To access this database, you use
the man command, which resembles the MS-DOS help command. For example, to get help on using
the w command, type:
root@desktop:/root#
man w
Figure 4.1 shows the resulting output, which the command displays one page at a time. Notice the colon
prompt, which appears at the bottom left of the screen. To page forward, press the Space key; to page
backward, press the bkey. To exit the man program, press the q key.
The manual pages are organized according to a common format. At the beginning of a manual page,
you'll find the name of the page and the section of the manual page database from which the page
comes, shown in parentheses. For example, the figure shows the manual page named w, which comes
from section 1 of the manual page database. Table 4.1 describes the sections of the manual page
database; most sections are primarily of interest to programmers. As a user and administrator, you'll be
interested primarily in sections 1 and 8.
Table 4.1: Manual Page Sections
Section Description
1 Executable programs and shell commands
2 System calls (provided by the kernel)
3 Library calls (provided by system libraries)
4 Special files (for example, device files)
5 File formats and conventions
6 Games
7 Macro packages and conventions
8 System administration commands
9 Non-standard kernel routines
Next in the output comes the name and brief description of the command. Then comes a synopsis of the
command, which shows the options and arguments that you can specify. Brackets enclose parts of a
command that you can choose to include or omit. Next comes a detailed description of the operation of
the command, followed by a description of its options.
As you're learning your way around Linux, you may find it convenient to reserve a virtual console for
running the man command. That way, you can enter commands in a separate virtual console, switching
between consoles to refresh your recollection of the options and arguments of commands as you type
them.
The man command searches the manual pages and displays detailed information about a specified
command. The apropos command also searches the manual pages; however, it displays summary
information about manual pages that contain a specified keyword. (The search is limited to the short
description that appears at the beginning of each manual page). For example, typing the command:
root@desktop:/root#
apropos files
Commands
Terminal
CLI preceded GUI as a computer interface. Since GUIs are common now, why should we still care about
CLI? Some advantages of using the command line are:
1. It can save you time.
2. It can help when you are unable to use the GUI, such as a system crash or a configuration issue.
3. It can enable you to use Linux in ways that using a GUI exclusively can not (such as scripting
repetitive tasks).
For example, you have been called by the systems administrator that you have used too much space.
You want to quickly work out where the most space is used, so using a graphical interface, start your
timer - go. Now, go to a command line and type: du | sort -n (we will describe more later). See? It is faster
to do some things on the command line (and other times, easier for graphical).
How to invoke it
Methods of launching a CLI can vary by desktop environment:
Shell
The shell is a program that takes commands from the keyboard and gives them to the operating system
to perform. In the old days, it was the only user interface available on a Unix-like system such as Linux.
Nowadays, we have graphical user interfaces (GUIs) in addition to command line interfaces (CLIs) such
as the shell.
On most Linux systems a program called bash (which stands for Bourne Again SHell, an enhanced
version of the original Unix shell program, sh, written by Steve Bourne) acts as the shell program.
Besides bash, there are other shell programs that can be installed in a Linux system. These
include: ksh, tcsh and zsh.
What's a "Terminal?"
It's a program called a terminal emulator. This is a program that opens a window and lets you interact with
the shell. There are a bunch of different terminal emulators you can use. Most Linux distributions supply
several, such as: gnome-terminal, konsole, xterm, rxvt, kvt, nxterm, and eterm.
Starting A Terminal
Your window manager probably has a way to launch a terminal from the menu. Look through the list of
programs to see if anything looks like a terminal emulator. If you are a KDE user, the terminal program is
called "konsole," in Gnome it's called "gnome-terminal." You can start up as many of these as you want
and play with them. While there are a number of different terminal emulators, they all do the same thing.
They give you access to a shell session. You will probably develop a preference for one, based on the
different bells and whistles each one provides.
cat
# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
narad:x:500:500::/home/narad:/bin/bash
In below example, it will display contains of test and test1 file in terminal.
Hello everybody
Hi world,
# cat >test2
Awaits input from user, type desired text and press CTRL+D (hold down Ctrl Key and type ‘d‘) to exit. The text will be
written in test2 file. You can see contains of file with following cat command.
# cat test2
If file having large number of contains that won’t fit in output terminal and screen scrolls up very fast, we can use
With -n option you could see the line numbers of a file song.txt in the output terminal.
# cat -n song.txt
In the below, you can see with -e option that ‘$‘ is shows at the end of line and also in space showing ‘$‘ if there is
any gap between paragraphs. This options is useful to squeeze multiple lines in a single line.
# cat -e test
In the below output, we could see TAB space is filled up with ‘^I‘ character.
# cat -T test
In the below example we have three files test, test1 and test2 and able to view the contains of those file as shown
We can redirect standard output of a file into a new file else existing file with ‘>‘ (greater than) symbol. Careful,
Appends in existing file with ‘>>‘ (double greater than) symbol. Here, contains of test file will be appended at the end
of test1 file.
When you use the redirect with standard input ‘<‘ (less than symbol), it use file name test2 as a input for a command
This will create a file called test3 and all output will be redirected in a newly created file.
This will create a file test4 and output of cat command is piped to sort and result will be redirected in a newly created
file.
ls
ls with no option list files and directories in bare format where we won’t be able to view details like file types, size,
# ls
Here, ls -l (-l is character not one) shows file or directory, size, modified date and time, file or folder name and owner
# ls -l
total 176
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
-rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents
drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads
-rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
-rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
-rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos
# ls -a
# ls -lh
total 176K
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
-rw-------. 1 root root 1.6K Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Documents
drwxr-xr-x. 4 root root 4.0K Aug 16 02:55 Downloads
-rw-r--r--. 1 root root 21K Aug 12 12:42 fbcmd_update.php
-rw-r--r--. 1 root root 46K Jul 31 09:58 index.html
-rw-r--r--. 1 root root 48K Jul 31 02:17 install.log
-rw-r--r--. 1 root root 12K Jul 31 02:13 install.log.syslog
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Music
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Pictures
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Public
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Templates
drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Videos
5. List Files and Directories with ‘/’ Character at the end
Using -F option with ls command, will add the ‘/’ Character at the end each directory.
# ls -F
The following command with ls -r option display files and directories in reverse order.
# ls -r
ls -R option will list very long listing directory trees. See an example of output of the command.
# ls -R
total 1384
-rw-------. 1 root root 33408 Aug 8 17:25 anaconda.log
-rw-------. 1 root root 30508 Aug 8 17:25 anaconda.program.log
./httpd:
total 132
-rw-r--r-- 1 root root 0 Aug 19 03:14 access_log
-rw-r--r--. 1 root root 61916 Aug 10 17:55 access_log-20120812
./lighttpd:
total 68
-rw-r--r-- 1 lighttpd lighttpd 7858 Aug 21 15:26 access.log
-rw-r--r--. 1 lighttpd lighttpd 37531 Aug 17 18:21 access.log-20120819
./nginx:
total 12
-rw-r--r--. 1 root root 0 Aug 12 03:17 access.log
-rw-r--r--. 1 root root 390 Aug 12 03:17 access.log-20120812.gz
With combination of -ltr will shows latest modification file or directory date as last.
# ls -ltr
total 176
-rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
-rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents
-rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
-rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
With combination of -lS displays file size in order, will display big in size first.
# ls -lS
total 176
-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
-rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
-rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
-rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents
drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates
drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos
-rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
We can see some number printed before file / directory name. With -i options list file / directory with inode number.
# ls -i
20112 0001.pcap 23610 Documents 23793 index.html 23611 Music 23597 Templates
23564 anaconda-ks.cfg 23595 Downloads 22 install.log 23612 Pictures 23613 Videos
23594 Desktop 23585 fbcmd_update.php 35 install.log.syslog 23601 Public
# ls --version
# ls --help
With ls -l command list files under directory /tmp. Wherein with -ldparameters displays information of /tmp directory.
# ls -l /tmp
total 408
drwx------. 2 narad narad 4096 Aug 2 02:00 CRX_75DAF8CB7768
-r--------. 1 root root 384683 Aug 4 12:28 htop-1.0.1.tar.gz
drwx------. 2 root root 4096 Aug 4 11:20 keyring-6Mfjnk
drwx------. 2 root root 4096 Aug 16 01:33 keyring-pioZJr
drwx------. 2 gdm gdm 4096 Aug 21 11:26 orbit-gdm
drwx------. 2 root root 4096 Aug 19 08:41 pulse-gl6o4ZdxQVrX
drwx------. 2 narad narad 4096 Aug 4 08:16 pulse-UDH76ExwUVoU
drwx------. 2 gdm gdm 4096 Aug 21 11:26 pulse-wJtcweUCtvhn
-rw-------. 1 root root 300 Aug 16 03:34 yum_save_tx-2012-08-16-03-34LJTAa1.yumtx
# ls -ld /tmp/
drwxrwxrwt. 13 root root 4096 Aug 21 12:48 /tmp/
cd
avi@tecmint:~$ cd /usr/local
avi@tecmint:/usr/local$
avi@tecmint:/usr/local$ cd /usr/local/lib
avi@tecmint:/usr/local/lib$
avi@tecmint:/usr/local$ cd lib
avi@tecmint:/usr/local/lib$
4. (a) Move one directory back from where you are now.
avi@tecmint:/usr/local/lib$ cd -
/usr/local
avi@tecmint:/usr/local$
avi@tecmint:/usr/local/lib$ cd ..
avi@tecmint:/usr/local$
5. Show last working directory from where we moved (use ‘–‘ switch) as shown.
avi@tecmint:/usr/local$ cd --
/home/avi
avi@tecmint:/usr/local$ cd ~
avi@tecmint:~$
or
avi@tecmint:/usr/local$ cd
avi@tecmint:~$
avi@tecmint:~/Downloads$ cd .
avi@tecmint:~/Downloads$
or
avi@tecmint:~/Downloads$ cd ./
avi@tecmint:~/Downloads$
one line command, by moving up in the directory till ‘/’ then using absolute path.
avi@tecmint:/usr/local/lib/python3.4/dist-packages$ cd ../../../../../home/avi/Desktop/
avi@tecmint:~/Desktop$
10. Change from current working directory to /var/www/html without typing in full using TAB.
avi@tecmint:/var/www$ cd /v<TAB>/w<TAB>/h<TAB>
avi@tecmint:/var/www/html$
11. Navigate from your current working directory to /etc/v__ _, Oops! You forgot the name of directory and not
avi@tecmint:~$ cd /etc/v*
avi@tecmint:/etc/vbox$
Note: This will move to ‘vbox‘ only if there is only one directory starting with ‘v‘. If more than one directory starting with
‘v‘ exist, and no more criteria is provided in command line, it will move to the first directory starting with ‘v‘,
12. You need to navigate to user ‘av‘ (not sure if it is avi or avt) home directory, without using TAB.
avi@tecmint:/etc$ cd /home/av?
avi@tecmint:~$
Pushd and popd are Linux commands in bash and certain other shell which saves current working directory location
to memory and bring to the directory from memory as current working directory, respectively as well as changes
directory.
/var/www/html ~
avi@tecmint:/var/www/html$
The above command saves the current location to memory and changes to the requested directory. As soon as popd
is fired, it fetch the saved directory location from memory and makes it current working directory.
avi@tecmint:/var/www/html$ popd
avi@tecmint:~$
avi@tecmint:~/test tecmint$
or
avi@tecmint:~/test tecmint$
or
avi@tecmint:~/test tecmint$
15. Change from current working directory to Downloads and list all its settings in one go.
service_locator_in.xls
sources.list
teamviewer_linux_x64.deb
tor-browser-linux64-3.6.3_en-US.tar.xz
...
~/java date
Thu Jul 14 14:31:35 MPST 2011
$ date --date='@2147483647'
Show the time on the west coast of the US (use tzselect(1) to find
TZ)
$ TZ='America/Los_Angeles' date
Show the local time for 9AM next Friday on the west coast of the US
$ date --date='TZ="America/Los_Angeles" 09:00 next Fri'
SYNOPSIS
cal [-smjy13] [[month] year]
DESCRIPTION
Cal displays a simple calendar. If arguments are not specified, the current month is displayed. The options are as
follows:
Tag Description
-s Display Sunday as the first day of the week. (This is the default.)
man is most commonly used without any options and with only one keyword. The keyword is the exact name of the
command or other item for which information is desired. For example, the following provides information about
the ls command (which is used to list the contents of any specified directory):
man ls
As another example, the following displays the man page about the man pages:
man man
man automatically sends its output through a pager, usually the program less. A pager is a program that causes the
output of any program to be displayed one screenful at a time, rather than having a large amount of text scroll down
the screen at high (and generally unreadable) speed.
echo
2. Declare a variable and echo its value. For example, Declare a variable of xand assign its value=10.
$ x=10
3. Using option ‘\b‘ – backspace with backslash interpretor ‘-e‘ which removes all the spaces in between.
TecmintisacommunityofLinuxNerds
4. Using option ‘\n‘ – New line with backspace interpretor ‘-e‘ treats new line from where it is used.
Tecmint
is
community
of
Linux
Nerds
5. Using option ‘\t‘ – horizontal tab with backspace interpretor ‘-e‘ to have horizontal tab spaces.
$ echo -e "Tecmint \tis \ta \tcommunity \tof \tLinux \tNerds"
6. How about using option new Line ‘\n‘ and horizontal tab ‘\t‘ simultaneously.
Tecmint
is
community
of
Linux
Nerds
7. Using option ‘\v‘ – vertical tab with backspace interpretor ‘-e‘ to have vertical tab spaces.
is
community
of
Linux
Nerds
8. How about using option new Line ‘\n‘ and vertical tab ‘\v‘ simultaneously.
Tecmint
is
a
community
of
Linux
Nerds
Note: We can double the vertical tab, horizontal tab and new line spacing using the option two times or as many
times as required.
9. Using option ‘\r‘ – carriage return with backspace interpretor ‘-e‘ to have specified carriage return in output.
10. Using option ‘\c‘ – suppress trailing new line with backspace interpretor ‘-e‘ to continue without emitting new line.
12. Using option ‘\a‘ – alert return with backspace interpretor ‘-e‘ to have sound alert.
13. Print all the files/folder using echo command (ls command alternative).
$ echo *
103.odt 103.pdf 104.odt 104.pdf 105.odt 105.pdf 106.odt 106.pdf 107.odt 107.pdf 108a.odt 108.odt 108.pdf 109.odt
109.pdf 110b.odt 110.odt 110.pdf 111.odt 111.pdf 112.odt 112.pdf 113.odt linux-headers-3.16.0-
customkernel_1_amd64.deb linux-image-3.16.0-customkernel_1_amd64.deb network.jpeg
14. Print files of a specific kind. For example, let’s assume you want to print all ‘.jpeg‘ files, use the following
command.
$ echo *.jpeg
network.jpeg
15. The echo can be used with redirect operator to output to a file and not standard output.
## Check Content
Test Page
echo Options
Options Description
\b backspace
\\ backslash
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
pwd?
‘pwd‘ stands for ‘Print Working Directory‘. As the name states, command ‘pwd‘ prints the current working directory or
simply the directory user is, at present. It prints the current directory name with the complete path starting from root
(/). This command is built in shell command and is available on most of the shell – bash, Bourne shell, ksh,zsh, etc.
Basic syntax of pwd:
# pwd [OPTION]
Options Description
If both ‘-L‘ and ‘-P‘ options are used, option ‘L‘ is taken into priority. If no option is specified at the prompt, pwd will
0 Success
Non-zero Failure
This article aims at providing you a deep insight of Linux command ‘pwd‘ with practical examples.
avi@tecmint:~$ /bin/pwd
/home/avi
2. Create a symbolic link of a folder (say /var/www/html into your home directory as htm). Move to the newly created
directory and print working directory with symbolic links and without symbolic links.
Create a symbolic link of folder /var/www/html as htm in your home directory and move to it.
avi@tecmint:~$ cd htm
avi@tecmint:~$ /bin/pwd -L
/home/avi/htm
4. Print actual physical current working directory by resolving all symbolic links.
avi@tecmint:~$ /bin/pwd -P
/var/www/html
5. Check if the output of command “pwd” and “pwd -P” are same or not i.e., if no options are given at run-time does
avi@tecmint:~$ /bin/pwd
/var/www/html
Result: It’s clear from the above output of example 4 and 5 (both result are same) thus, when no options are specified
This is free software: you are free to change and redistribute it.
Note: A ‘pwd’ command is often used without options and never used with arguments.
Important: You might have noticed that we are executing the above command as “/bin/pwd” and not “pwd”.
So what’s the difference? Well “pwd” alone means shell built-in pwd. Your shell may have different version of pwd.
Please refer manual. When we are using /bin/pwd, we are calling the binary version of that command. Both the shell
and the binary version of command Prints Current Working Directory, though the binary version have more options.
8. Store the value of “pwd” command in variable (say a), and print its value from the variable (important for shell
scripting perspective).
avi@tecmint:~$ a=$(pwd)
9. Change current working directory to anything (say /home) and display it in command line prompt. Execute a
avi@tecmint:~$ cd /home
> ls
10. Set multi-line command line prompt (say something like below).
/home
123#Hello#!
avi@tecmint:~$ PS1='
> $PWD
$ 123#Hello#!
$'
/home
123#Hello#!
11. Check the current working directory and previous working directory in one GO!
/home /home/avi
12. What is the absolute path (starting from /) of the pwd binary file.
/bin/pwd
13. What is the absolute path (starting from /) of the pwd source file.
/usr/include/pwd.h
14. Print the absolute path (starting from /) of the pwd manual pages file.
/usr/share/man/man1/pwd.1.gz
15. Write a shell script analyses current directory (say tecmint) in your home directory. If you are under
directorytecmint it output “Well! You are in tecmint directory” and then print “Good Bye” else create a
Let’s first create a ‘tecmint’ directory, under it create a following shell script file with name ‘pwd.sh’.
mkdir
avi@tecmint:~$ cd tecmint
$ mkdir
By default, running mkdir without any parameter will create directory under the current directory. Here’s a sample of it
:
From screenshot above, we created directory called office. When we run mkdir command, we are
in/home/pungki directory. So then the new directory, which is office, is created under /home/pungkidirectory. If we
put an exact location - for example : /usr/local - , then Linux will create a directory under /usr/local directory.
When Linux found that the directory which suppose to be created is already exist, then Linux will telling us that Linux
can’t cretate it.
Another pre-requisite of creating directory that you must have access to the location where the directory want to be
created. When you don’t have it then mkdir will report an error.
We can also create multiple directories at the same time. Let say we want to create directories namedubuntu, redhat
and slackware. Then the syntax will be like this :
$ mkdir -p letter/important
Using -m parameter, we can also set the access privilege for the new directory on-the-fly. Here’s an example.
The above command will create a directory named letter and give access privilege read-only for thedirectory
owner, directory group owner and anybody.
To run rm command, type rm followed by filename. Please remember, that by default rm will not ask any
confirmation. Here’s an example of rm in action.
Remove a file
$ rm computer.log
If you have multiple file with the same extension and you want to delete them, you can use this syntax :
$ rm *.log
From the above screenshot, all files with .log extension are deleted at the same time.
Remove a directory
Removing directory is a little bit tricky. If you are sure that the directory is empty, then we can use -dparameter to
remove it.
$ rm -d documents/
But when the directory is not empty, you have to empty the directory first, or you can remove them recursively. To
remove recursively, use -r or -R parameter.
$ rm -r movie/
If you feel more comfortable to have interactive confirmation, you can use -i parameter with rm command. Here’s a
sample of deleting directory recursively with interactive confirmation.
$ rm -ri movie/
Using force deletion mean that rm will remove all files without any confirmation, even the file is write-protected or
not. Here’s some samples.
We see that movie.list has acces read-only for owner, group owner and everyone. When we tried to remove it, rm
will ask a confirmation about it, but the file is successfully deleted. Using -f parameter, rm will not ask any
confirmation. Take a look at screenshot below.
But if the directory where the files is located is write-protected, then the file cannot be removed, even the file itself
is not write-protected.
rmdir command syntax
rmdir foo
rmdir -p sales/99/march
Recursive delete
rm -rf /home/data/2000
ps
Run ps without any options
This is a very basic ps usage. Just type ps on your console to see its result.
By default, it will show us 4 columns of information.
To do this, we can use -a options. As we can guess, -a is stand for “all”. While x will show all process even the
current process is not associated with any TTY (terminal)
$ ps -ax
This result might be long result. To make it more easier to read, combine it with less command.
$ ps -ax | less
3. Filter processes by its user
For some situation we may want to filter processes by user. To do this, we can use -u option. Let say we want to see
what processes which run by user pungki. So the command will be like below
$ ps -u pungki
Kill command send a signal, a specified signal to be more perfect to a process. The kill command can be executed in
SIGHUP 1 Hangup
SIGKILL 9 Kill Signal
SIGTERM 15 Terminate
Clearly from the behaviour above SIGTERM is the default and safest way to kill a process. SIGHUP is less secure
way of killing a process as SIGTERM. SIGKILL is the most unsafe way among the above three, to kill a process
In order to kill a process, we need to know the Process ID of a process. A Process is an instance of a program.
Every-time a program starts, automatically an unique PID is generated for that process. Every Process in Linux, have
a pid. The first process that starts when Linux System is booted is – init process, hence it is assigned a value of ‘1‘ in
Init is the master process and can not be killed this way, which insures that the master process don’t gets killed
accidentally. Init decides and allows itself to be killed, where kill is merely a request for a shutdown.
And if you would like to see more details about a package, use a mouse right-click on a package and
choose Properties
Browsing the package database
To browse the (very large) list of available packages by category, section, package status, custom filters, or recent
searches. Click on the corresponding button at the bottom of the left window pane. You can also create your own
filters. See the Filters section for details.
To search for packages by name or description, click on the Search button in the toolbar:
1.
or use the "Quick search" field in the toolbar:
1.
You can examine a number of package details in the lower right window pane such as its size, its dependencies,
recommended or suggested additional packages, and a short description.
Managing Repositories
Either from the top taskbar
System - Administrator - "Software Sources"
or from inside Synaptic using the Settings menu, select Repositories
The first tab of the pop-up is where Cd/Dvds can be added (or removed) as repositories to search. Usually the
various online "repos" are already included so you might be installing a more recent version of something that is listed
on the Cd/dvd and also in the online repos. The package manager will automatically choose the most recent by
default although one of the other tabs in the pop-up can change the priorities, or in thePreferences pop-up from
the Setttings menu.
As a front-end to apt, Synaptic uses the system-wide list of software repositories file located at
1. /etc/apt/sources.list
For more on managing software repositories, see the Repositories wiki page.
Adding Packages
Installing Packages