Linux Fundamentals: Chapter 3: The Command Line
Linux Fundamentals: Chapter 3: The Command Line
FUNDAMENTALS
Lessons
• man Pages
• Working with Directories
• Working with Files
• Working with File Contents
• The Linux File Tree
COURSE OUTLINE
Chapter 3: THE COMMAND LINE
Lessons
• man pages
man $command
man $configfile
man $daemon
man -k (apropos)
whatis
whereis
man sections
man $section $file
man man
mandb
Chapter 3: THE COMMAND LINE
man Pages
o man $command
Type man followed by a command (for which you want help) and start
reading. Press q to quit the manpage
Some man pages contain examples (near the end).
root@gkcollege:~$ man whois
Reformatting whois(1), please wait...
o man $configfile
o whereis
When referring to the man page of the passwd command, you will see it
written as passwd(1)
When referring to the passwd file, you will see it written as passwd(5)
[root@gkcollege ~]$ man passwd # opens the first manual found
[root@gkcollege ~]$ man 5 passwd # opens a page from section 5
Chapter 3: THE COMMAND LINE
man Pages
o man man
To know more about man, then Read The Fantastic Manual (RTFM)
Unfortunately, manual pages do not have the answer to everything
root@gkcollege:~$ man woman
No manual entry for woman
Chapter 3: THE COMMAND LINE
man Pages
o mandb
Should you be convinced that a man page exists, but you can't access it,
then try running mandb on Debian/Mint
root@gkcollege:~$ whatis route
route (8) - show / manipulate the IP routing table
root@gkcollege:~$ mandb
man subdirectories contained newer manual pages
manual pages were added
stray cats were added
old database entries were purged
Chapter 3: THE COMMAND LINE
man Pages
o mandb
Lessons
• Working with Directories
pwd
cd
absolute and relative paths
path completion
ls
mkdir
rmdir
Chapter 3: THE COMMAND LINE
Working with Directories
o pwd
The “you are here” sign can be displayed with the pwd command
Print Working Directory
root@gkcollege:~$ pwd
/root
Chapter 3: THE COMMAND LINE
Working with Directories
o cd ~
To go to the parent directory (the one just above your current directory
in the directory tree), type cd ..
root@gkcollege$ pwd
/usr/share/games
root@gkcollege$ cd ..
root@gkcollege$ pwd
/usr/share
Many times you will be using options with ls to display the contents of
the directory in different formats or to display different parts of the
directory
Typing just ls gives you a list of files in the directory
Typing ls -l (that is a letter L, not the number 1) gives you a long listing
root@gkcollege:~$ ls -l
total 17296
-rw-r--r-- 1 user1 user1 17584442 Sep 17 00:03 allfiles.txt
-rw-r--r-- 1 user1 user1 96650 Sep 17 00:03 dmesg.txt
-rw-r--r-- 1 user1 user1 19558 Sep 17 00:04 services
drwxr-xr-x 2 user1 user1 4096 Sep 17 00:04 stuff
-rw-r--r-- 1 user1 user1 0 Sep 17 00:04 summer.txt
Chapter 3: THE COMMAND LINE
Working with Directories
o ls -lh
o ls -hl
root@gkcollege:~$ ls -hl
total 17M
-rw-r--r-- 1 user1 user1 17M Sep 17 00:03 allfiles.txt
-rw-r--r-- 1 user1 user1 95K Sep 17 00:03 dmesg.txt
-rw-r--r-- 1 user1 user1 20K Sep 17 00:04 services
drwxr-xr-x 2 user1 user1 4.0K Sep 17 00:04 stuff
-rw-r--r-- 1 user1 user1 0 Sep 17 00:04 summer.txt
Chapter 3: THE COMMAND LINE
Working with Directories
o ls –l -h
root@gkcollege:~$ ls -h -l
total 17M
-rw-r--r-- 1 user1 user1 17M Sep 17 00:03 allfiles.txt
-rw-r--r-- 1 user1 user1 95K Sep 17 00:03 dmesg.txt
-rw-r--r-- 1 user1 user1 20K Sep 17 00:04 services
drwxr-xr-x 2 user1 user1 4.0K Sep 17 00:04 stuff
-rw-r--r-- 1 user1 user1 0 Sep 17 00:04 summer.txt
root@gkcollege:~$
Chapter 3: THE COMMAND LINE
Working with Directories
o mkdir
When a directory is empty, you can use rmdir to remove the directory
root@gkcollege:~/mydir$ ls -l
total 8
drwxr-xr-x 2 user1 user1 4096 Sep 17 00:08 otherstuff
drwxr-xr-x 2 user1 user1 4096 Sep 17 00:08 stuff
root@gkcollege:~/mydir$ rmdir otherstuff
root@gkcollege:~/mydir$ cd ..
root@gkcollege:~$ rmdir mydir
rmdir: failed to remove ‘mydir’: Directory not empty
root@gkcollege:~$ rmdir mydir/stuff
root@gkcollege:~$ rmdir mydir
root@gkcollege:~$
Chapter 3: THE COMMAND LINE
Working with Directories
o rmdir -p
Lessons
• Working with Files
all files are case sensitive
everything is a file
file
touch
rm
cp
mv
rename
Chapter 3: THE COMMAND LINE
Working with Files
o All files are case sensitive
The file command uses a magic file that contains patterns to recognize
file types
The magic file is located in /usr/share/file/magic
Type man 5 magic for more information
It is interesting to point out file -s for special files like those in /dev
and /proc
Chapter 3: THE COMMAND LINE
Working with Files
o file
The rename command is one of the rare occasions where the Linux
Fundamentals books has to make a distinction between Linux
distributions
Almost every command in the Fundamentals part of this book works on
almost every Linux computer
But rename is different
Use mv whenever you need to rename only a couple of files
Chapter 3: THE COMMAND LINE
Working with Files
o rename on Debian/Ubuntu
The second example renames all (*) files replacing one with ONE
[root@centos7 ~]$ ls
one.backup three.backup two.backup
[root@centos7 ~]$ rename one ONE *
[root@centos7 ~]$ ls
ONE.backup three.backup two.backup
[root@centos7 ~]$
COURSE OUTLINE
Chapter 3: THE COMMAND LINE
Lessons
• Working with File Contents
head
tail
cat
tac
more and less
strings
Chapter 3: THE COMMAND LINE
Working with File Contents
o head
Similar to head, the tail command will display the last ten lines of a file
root@gkcollege~$ tail /etc/services
vboxd 20012/udp
binkp 24554/tcp # binkp fidonet protocol
asp 27374/tcp # Address Search Protocol
asp 27374/udp
csync2 30865/tcp # cluster synchronization tool
dircproxy 57000/tcp # Detachable IRC Proxy
tfido 60177/tcp # fidonet EMSI over telnet
fido 60179/tcp # fidonet EMSI over TCP
# Local services
root@gkcollege~$
Chapter 3: THE COMMAND LINE
Working with File Contents
o tail
You can give tail the number of lines you want to see
root@gkcollege~$ tail -3 /etc/services
fido 60179/tcp # fidonet EMSI over TCP
# Local services
root@gkcollege~$
Chapter 3: THE COMMAND LINE
Working with File Contents
o cat
Create files
Use cat to create flat text files
Type cat > winter.txt
Then type one or more lines, finishing each line with the enter key. After
the last line, type and hold the Control (Ctrl) key and press d
The Ctrl + d key combination will send an EOF (End of File) to the
running process ending the cat command
Chapter 3: THE COMMAND LINE
Working with File Contents
o cat
root@gkcollege:~$ cat > winter.txt
It is very cold today!
root@gkcollege:~$ cat winter.txt
It is very cold today!
root@gkcollege:~$
Chapter 3: THE COMMAND LINE
Working with File Contents
o cat
Copy files
root@gkcollege:~$ cat winter.txt
It is very cold today!
root@gkcollege:~$ cat winter.txt > cold.txt
root@gkcollege:~$ cat cold.txt
It is very cold today!
root@gkcollege:~$
Chapter 3: THE COMMAND LINE
Working with File Contents
o tac
The more command is useful for displaying files that take up more than
one screen
more will allow you to see the contents of the file page by page
Use the space bar to see the next page, or q to quit
Some people prefer the less command to more
Chapter 3: THE COMMAND LINE
Working with File Contents
o strings
The strings command can display readable ascii strings found in (binary)
files
locates the ls binary then display readable strings in the binary file
(output is truncated)
root@gkcollege:~$ which ls
/bin/ls
root@gkcollege:~$ strings /bin/ls
/lib/ld-linux.so.2
librt.so.1
__gmon_start__
_Jv_RegisterClasses
clock_gettime
libacl.so.1
...
COURSE OUTLINE
Chapter 3: THE COMMAND LINE
Lessons
• The Linux File Tree
filesystem hierarchy standard
man hier
the root directory / binary directories
configuration directories
data directories
in memory directories 109
/usr Unix System Resources
/var variable data
Chapter 3: THE COMMAND LINE
filesystem hierarchy standard
o Many Linux distributions partially follow the Filesystem Hierarchy Standard
o The FHS may help make more Unix/Linux file system trees conform better in
the future
o The FHS is available online at http://www.pathname.com/fhs/ :
[root@gkcollege ~]$ ls /
bin dev home media mnt proc sbin srv tftpboot usr
boot etc lib misc opt root selinux sys tmp var
Chapter 3: THE COMMAND LINE
the root directory / binary directories
o binary directories
Binaries are files that contain compiled source code (or machine code)
Binaries can be executed on the computer
Sometimes binaries are called executables
Chapter 3: THE COMMAND LINE
the root directory / binary directories
o bin
o /sbin
o /lib
Binaries found in /bin and /sbin often use shared libraries located in /lib. Below is a
sample of the partial contents of /lib
root@gkcollege:~$ ls /lib/libc*
/lib/libc-2.5.so /lib/libcfont.so.0.0.0 /lib/libcom_err.so.2.1
/lib/libcap.so.1 /lib/libcidn-2.5.so /lib/libconsole.so.0
/lib/libcap.so.1.10 /lib/libcidn.so.1 /lib/libconsole.so.0.0.0
/lib/libcfont.so.0 /lib/libcom_err.so.2 /lib/libcrypt-2.5.so
/lib/modules
o /lib
/lib32 and /lib64
o /lib
The ELF (Executable and Linkable Format) is used in almost every Unix-like operating
system since System V
Chapter 3: THE COMMAND LINE
the root directory / binary directories
o /opt
The purpose of /opt is to store optional software
In many cases this is software from outside the distribution repository
You may find an empty /opt directory on many systems
A large package can install all its files in /bin, /lib, /etc subdirectories within
/opt/$packagename/
If for example the package is called wp, then it installs in /opt/wp, putting binaries in
/opt/wp/bin and manpages in /opt/wp/man
Chapter 3: THE COMMAND LINE
configuration directories
o /boot
The /boot directory contains all files needed to boot the computer
On Linux systems you typically find the /boot/grub directory here
/boot/grub contains /boot/grub/grub.cfg (older systems may still have
/boot/grub/grub.conf) which defines the boot menu that is displayed before the
kernel starts
Chapter 3: THE COMMAND LINE
configuration directories
o /etc
o /etc
root@gkcollege:~$ ls /etc/*.conf
/etc/adduser.conf /etc/ld.so.conf /etc/scrollkeeper.conf
/etc/brltty.conf /etc/lftp.conf /etc/sysctl.conf
/etc/ccertificates.conf /etc/libao.conf /etc/syslog.conf
/etc/cvs-cron.conf /etc/logrotate.conf /etc/ucf.conf
/etc/ddclient.conf /etc/ltrace.conf /etc/uniconf.conf
/etc/debconf.conf /etc/mke2fs.conf /etc/updatedb.conf
/etc/deluser.conf /etc/netscsid.conf /etc/usplash.conf
/etc/fdmount.conf /etc/nsswitch.conf /etc/uswsusp.conf
/etc/hdparm.conf /etc/pam.conf /etc/vnc.conf
/etc/host.conf /etc/pnm2ppa.conf /etc/wodim.conf
/etc/inetd.conf /etc/povray.conf /etc/wvdial.conf
/etc/kernel-img.conf /etc/resolv.conf
root@gkcollege:~$
Chapter 3: THE COMMAND LINE
configuration directories
o /etc
/etc/init.d/
/etc/X11/
- Graphical display (aka X Window System or just X) from the X.org foundation
- The configuration file for your graphical display is /etc/X11/xorg.conf
Chapter 3: THE COMMAND LINE
configuration directories
o /etc
/etc/skel/
/etc/sysconfig/
- This directory, not mentioned in the FHS, contains a lot of RHEL config files
- The example below is the /etc/sysconfig directory from RHELv4u4 with
everything installed
Chapter 3: THE COMMAND LINE
configuration directories
o /etc
root@gkcollege:~$ ls /etc/sysconfig/
apmd firstboot irda network saslauthd
apm-scripts grub irqbalance networking selinux
authconfig hidd keyboard ntpd spamassassin
autofs httpd kudzu openib.conf squid
bluetooth hwconf lm_sensors pand syslog
clock i18n mouse pcmcia sys-config-sec
console init mouse.B pgsql sys-config-users
crond installinfo named prelink sys-logviewer
desktop ipmi netdump rawdevices tux
diskdump iptables netdump_id_dsa rhn vncservers
dund iptables-cfg netdump_id_dsa.p samba xinetd
root@gkcollege:~$
Chapter 3: THE COMMAND LINE
configuration directories
o /etc
/etc/sysconfig/
- The file /etc/sysconfig/firstboot tells the Red Hat Setup Agent not to run at
boot time
- To run the Red Hat Setup Agent at the next reboot, simply remove this file, and
run chkconfig --level 5 firstboot on
- The Red Hat Setup Agent allows installation of the latest updates, create a user
account, join the Red Hat Network and more
- It will then create the /etc/sysconfig/firstboot file again
root@gkcollege:~$ cat /etc/sysconfig/firstboot
RUN_FIRSTBOOT=NO
Chapter 3: THE COMMAND LINE
configuration directories
o /etc
/etc/sysconfig/
root@gkcollege:~$ ls /home
default location for personal data and profile of the root user
If it does not exist by default, then some administrators create it
o /srv
root@gkcollege:~$ ls /media/
cdrom cdrom0 usbdisk
Chapter 3: THE COMMAND LINE
data directories
o /mnt
The /mnt directory should be empty and should only be used for
temporary mount points (according to the FHS)
Unix and Linux administrators used to create many directories here, like
/mnt/something/
You likely will encounter many systems with more than one directory
created and/or mounted inside /mnt to be used for various local and
remote filesystems
Chapter 3: THE COMMAND LINE
data directories
o /tmp
Applications and users should use /tmp to store temporary data when
needed
Data stored in /tmp may use either disk space or RAM. Both of which are
managed by the operating system
Never use /tmp to store data that is important or which you wish to
archive
Chapter 3: THE COMMAND LINE
in memory directories
o /dev
Device files in /dev appear to be ordinary files, but are not actually
located on the hard disk
The /dev directory is populated with files as the kernel is recognizing
hardware
- /dev/null
can be considered a black hole
has unlimited storage, but nothing can be retrieved from it
Technically speaking, anything written to /dev/null will be
discarded
/dev/null can be useful to discard unwanted output from
commands
/dev/null is not a good location to store your backups
Chapter 3: THE COMMAND LINE
in memory directories
o /proc conversation with the kernel
When listing the /proc directory you will see many numbers (on any
Unix) and some interesting files (on Linux)
Chapter 3: THE COMMAND LINE
in memory directories
root@gkcollege:~$ ls /proc
1 2339 4724 5418 6587 7201 cmdline mounts
10175 2523 4729 5421 6596 7204 cpuinfo mtrr
10211 2783 4741 5658 6599 7206 crypto net
10239 2975 4873 5661 6638 7214 devices pagetypeinfo
141 29775 4874 5665 6652 7216 diskstats partitions
15045 29792 4878 5927 6719 7218 dma sched_debug
1519 2997 4879 6 6736 7223 driver scsi
1548 3 4881 6032 6737 7224 execdomains self
1551 30228 4882 6033 6755 7227 fb slabinfo
1554 3069 5 6145 6762 7260 filesystems stat
1557 31422 5073 6298 6774 7267 fs swaps
1606 3149 5147 6414 6816 7275 ide sys
180 31507 5203 6418 6991 7282 interrupts sysrq-trigger
181 3189 5206 6419 6993 7298 iomem sysvipc
182 3193 5228 6420 6996 7319 ioports timer_list
18898 3246 5272 6421 7157 7330 irq timer_stats
19799 3248 5291 6422 7163 7345 kallsyms tty
Chapter 3: THE COMMAND LINE
in memory directories
19803 3253 5294 6423 7164 7513 kcore uptime
19804 3372 5356 6424 7171 7525 key-users version
1987 4 5370 6425 7175 7529 kmsg version_signature
1989 42 5379 6426 7188 9964 loadavg vmcore
2 45 5380 6430 7189 acpi locks vmnet
20845 4542 5412 6450 7191 asound meminfo vmstat
221 46 5414 6551 7192 buddyinfo misc zoneinfo
2338 4704 5416 6568 7199 bus modules
Chapter 3: THE COMMAND LINE
in memory directories
Investigating the file properties inside /proc
Looking at the date and time will display the current date and time
showing the files are constantly updated (a view on the kernel)
root@gkcollege:~$ date
Mon Jan 29 18:06:32 EST 2007
root@gkcollege:~$ ls -al /proc/cpuinfo
-r--r--r-- 1 root root 0 Jan 29 18:06 /proc/cpuinfo
root@gkcollege:~$
root@gkcollege:~$ ...time passes...
root@gkcollege:~$
root@gkcollege:~$ date
Mon Jan 29 18:10:00 EST 2007
root@gkcollege:~$ ls -al /proc/cpuinfo
-r--r--r-- 1 root root 0 Jan 29 18:10 /proc/cpuinfo
Chapter 3: THE COMMAND LINE
in memory directories
Most files in /proc are 0 bytes, yet they contain data--sometimes a lot
of data
Try executing cat on files like /proc/cpuinfo, which contains
information about the CPU
root@gkcollege:~$ file /proc/cpuinfo
/proc/cpuinfo: empty
root@gkcollege:~$ cat /proc/cpuinfo
processor : 0
vendor_id : AuthenticAMD
cpu family : 15
model name : AMD Athlon(tm) 64 X2 Dual Core Processor 4600+
stepping : 1
cpu MHz : 2398.628
cache size : 512 KB
Chapter 3: THE COMMAND LINE
in memory directories
fdiv_bug : no
hlt_bug : no
f00f_bug : no
coma_bug : no
fpu : yes
fpu_exception : yes
cpuid level : 1
wp : yes
flags : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge...
bogomips : 4803.54
Chapter 3: THE COMMAND LINE
in memory directories
Most of the files in /proc are read only, some require root privileges,
some files are writable and many files in /proc/sys are writable
- /proc/interrupts
On the x86 architecture, /proc/interrupts displays the
interrupts
root@gkcollege:~$ cat /proc/interrupts
CPU0
0: 13876877 IO-APIC-edge timer
1: 15 IO-APIC-edge i8042
8: 1 IO-APIC-edge rtc
9: 0 IO-APIC-level acpi
12: 67 IO-APIC-edge i8042
14: 128 IO-APIC-edge ide0
Chapter 3: THE COMMAND LINE
in memory directories
15: 124320 IO-APIC-edge ide1
169: 111993 IO-APIC-level ioc0
177: 2428 IO-APIC-level eth0
NMI: 0
LOC: 13878037
ERR: 0
MIS: 0
Chapter 3: THE COMMAND LINE
in memory directories
On a machine with two CPU's, the file looks like this
root@gkcollege:~$ cat /proc/interrupts
CPU0 CPU1
0: 860013 0 IO-APIC-edge timer
1: 4533 0 IO-APIC-edge i8042
7: 0 0 IO-APIC-edge parport0
8: 6588227 0 IO-APIC-edge rtc
10: 2314 0 IO-APIC-fasteoi acpi
12: 133 0 IO-APIC-edge i8042
14: 0 0 IO-APIC-edge libata
15: 72269 0 IO-APIC-edge libata
18: 1 0 IO-APIC-fasteoi yenta
19: 115036 0 IO-APIC-fasteoi eth0
20: 126871 0 IO-APIC-fasteoi libata, ohci1394
21: 30204 0 IO-APIC-fasteoi ehci_hcd:usb1, uhci_hcd:usb2
Chapter 3: THE COMMAND LINE
in memory directories
On a machine with two CPU's, the file looks like this
root@gkcollege:~$ cat /proc/interrupts
22: 1334 0 IO-APIC-fasteoi saa7133[0], saa7133[0]
24: 234739 0 IO-APIC-fasteoi nvidia
NMI: 72 42
LOC: 860000 859994
ERR: 0
Chapter 3: THE COMMAND LINE
in memory directories
On a machine with two CPU's, the file looks like this
- /proc/kcore
The /sys directory was created for the Linux 2.6 kernel
Since 2.6, Linux uses sysfs to support usb and IEEE 1394 (FireWire)
hot plug devices
See the manual pages of udev(8) (the successor of devfs) and
hotplug(8)
for more info http://linuxhotplug.sourceforge.net/
Basically the /sys directory contains kernel information about
hardware
Chapter 3: THE COMMAND LINE
/usr Unix System Resources
o Although /usr is pronounced like user, remember that it stands for Unix
System Resources
o The /usr hierarchy should contain shareable, read only data
o Some people choose to mount /usr as read only
o This can be done from its own partition or from a read only NFS share
Chapter 3: THE COMMAND LINE
/usr Unix System Resources
o /usr/bin
root@gkcollege:~$ ls /usr/bin | wc -l
1395
Chapter 3: THE COMMAND LINE
/usr Unix System Resources
o /usr/include
root@gkcollege:~$ ls /usr/include/
aalib.h expat_config.h math.h search.h
af_vfs.h expat_external.h mcheck.h semaphore.h
aio.h expat.h memory.h setjmp.h
AL fcntl.h menu.h sgtty.h
aliases.h features.h mntent.h shadow.h
...
Chapter 3: THE COMMAND LINE
/usr Unix System Resources
o /usr/lib
The /usr/local directory serves as a central point to contain all log files
root@gkcollege:~$ ls /var/log
acpid cron.2 maillog.2 quagga secure.4
amanda cron.3 maillog.3 radius spooler
anaconda.log cron.4 maillog.4 rpmpkgs spooler.1
anaconda.syslog cups mailman rpmpkgs.1 spooler.2
anaconda.xlog dmesg messages rpmpkgs.2 spooler.3
audit exim messages.1 rpmpkgs.3 spooler.4
boot.log gdm messages.2 rpmpkgs.4 squid
boot.log.1 httpd messages.3 sa uucp
boot.log.2 iiim messages.4 samba vbox
boot.log.3 iptraf mysqld.log scrollkeeper.log vmware-tools-guestd
boot.log.4 lastlog news secure wtmp
canna mail pgsql secure.1 wtmp.1
cron maillog ppp secure.2 Xorg.0.log
cron.1 maillog.1 prelink.log secure.3 Xorg.0.log.old
Chapter 3: THE COMMAND LINE
/var variable data
o /var/log/messages
The /var/cache directory can contain cache data for several applications
root@gkcollege:~$ ls /var/cache/
apt dictionaries-common gdm man software-center
binfmts flashplugin-installer hald pm-utils
cups fontconfig jockey pppconfig
debconf fonts ldconfig samba
Chapter 3: THE COMMAND LINE
/var variable data
o /var/spool
The /var/spool directory typically contains spool directories for mail and
cron
Also serves as a parent directory for other spool files (for example print
spool files)
o /var/lib