working with directories

working with directories

ยท

5 min read

This module is a brief overview of the most common commands to work with directories: pwd, cd, ls, mkdir and rmdir. These commands are available on any Linux (or Unix) system. This module also discusses absolute and relative paths and path completion in the bash shell.

  1. pwd

The you are here sign can be displayed with the pwd command (Print Working Directory). Go ahead, try it: Open a command line interface (also called a terminal, console or xterm) and type pwd. The tool displays your current directory.

2. cd

You can change your current directory with the cd command (Change Directory).

3. cd~

The cd is also a shortcut to get back into your home directory. Just typing cd without a target directory, will put you in your home directory. Typing cd ~ has the same effect

  1. cd ..

To go to the parent directory (the one just above your current directory in the directory tree), type cd .. .

To stay in the current directory, type cd . ;-) We will see useful use of the . character representing the current directory later.

5. cd -

Another useful shortcut with cd is to just type cd โ€” to go to the previous directory.

absolute and relative paths

You should be aware of absolute and relative paths in the file tree. When you type a path starting with a slash (/), then the root of the file tree is assumed. If you donโ€™t start your path with a slash, then the current directory is the assumed starting point.

The screenshot below first shows the current directory /root. From within this directory, you have to type cd /home instead of cd home to go to the /home directory.

When inside /home, you have to type cd paul instead of cd /root to enter the subdirectory root of the current directory /home.

This was the last screenshot with pwd statements. From now on, the current directory will often be displayed in the prompt. Later we will explain how the shell variable $PS1 can be configured to show this.

๐–๐ก๐š๐ญ ๐ข๐ฌ ๐ฅ๐ฌ?

The first command we learn is ๐ฅ๐ฌ. This is the list command. It allows usโ€ฆ list the content of the directory.
๐ฅ๐ฌ is the command we can encounter in all ๐‹๐ข๐ง๐ฎ๐ฑ and ๐”๐ง๐ข๐ฑ ๐ฌ๐ฒ๐ฌ๐ญ๐ž๐ฆ๐ฌ. We can see it as one of the โ€˜๐œ๐จ๐ซ๐žโ€™ commands in the system.

๐Ž๐ค, ๐ฅ๐ž๐ญโ€™๐ฌ ๐ฉ๐ฅ๐š๐ฒ ๐ฐ๐ข๐ญ๐ก ๐ข๐ญ ๐š ๐ฅ๐ข๐ญ๐ญ๐ฅ๐ž.
Run ๐ฅ๐ฌ

What we see on the screen is a ๐ฅ๐ข๐ฌ๐ญ of the content of the current directory.
What is visible immediately, that we have different colors. We can imagine what it means, but letโ€™s forget it for a second. What many waves of โ€œ๐œ๐ฅ๐š๐ฌ๐ฌ๐ข๐œโ€ sys admins saw in their days:

๐ฅ๐ฌ โ€” ๐œ๐จ๐ฅ๐จ๐ซ=๐ง๐จ

So, this leads us to the first output, the colorized list of content. In order to have it, we need to run

๐ฅ๐ฌ โ€” ๐œ๐จ๐ฅ๐จ๐ซ=๐ฒ๐ž๐ฌ

or, alternatively for now

๐ฅ๐ฌ โ€” ๐œ๐จ๐ฅ๐จ๐ซ=๐š๐ฎ๐ญ๐จ .

For now, it will be enough to say, this colorized option is added as default in one place in the system, we will go there in one of the future lessons.

Man Command

This chapter will explain the use of man pages (also called manual pages) on your Unix or Linux computer.

Most Unix files and commands have pretty good man pages to explain their use. Man pages also come in handy when you are using multiple flavours of Unix or several Linux distributions since options and parameters sometimes vary.

1. 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).

2. man $configfile

Most configuration files have their own manual.

[root@ip-172โ€“31โ€“35โ€“176 ~]# man syslog.conf

Reformatting syslog.conf(5), please waitโ€ฆ

3. man $daemon

This is also true for most daemons (background programs) on your system..

[root@ip-172โ€“31โ€“35โ€“176 ~]# man syslogd

Reformatting syslogd(8), please waitโ€ฆ

4. man -k (apropos)

man -k (or apropos) shows a list of man pages containing a string.

[root@ip-172โ€“31โ€“35โ€“176 ~]# man -k syslog

lm-syslog-setup (8) โ€” configure laptop mode to switch syslog.conf โ€ฆ logger (1) โ€” a shell command interface to the syslog(3) โ€ฆ syslog-facility (8) โ€” Setup and remove LOCALx facility for sysklogd syslog.conf (5) โ€” syslogd(8) configuration file syslogd (8) โ€” Linux system logging utilities. syslogd-listfiles (8) โ€” list system logfiles

5. whatis

To see just the description of a manual page, use whatis followed by a string.

[root@ip-172โ€“31โ€“35โ€“176 ~]# whatis route

route (8) โ€” show / manipulate the IP routing table.

6. whereis

The location of a manpage can be revealed with whereis.

[root@ip-172โ€“31โ€“35โ€“176 ~]# whereis -m whois

whois: /usr/share/man/man1/whois.1.gz

This file is directly readable by man.

[root@ip-172โ€“31โ€“35โ€“176 ~]# man /usr/share/man/man1/whois.1.gz

7. man sections

By now you will have noticed the numbers between the round brackets. man man will explain to you that these are section numbers. Executable programs and shell commands reside in section one.

8. man $section $file

Therefor, 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). The screenshot explains how to open the man page in the correct section.

[root@ip-172โ€“31โ€“35โ€“176 ~]# man passwd

# opens the first manul found

[root@ip-172โ€“31โ€“35โ€“176 ~]# man 5 passwd

# opens a page from section 5

9. man man

If you want to know more about man, then Read The Fantastic Manual (RTFM). Unfortunately, manual pages do not have the answer to everythingโ€ฆ

10. mandb

Should you be convinced that a man page exists, but you canโ€™t access it, then try running mandb on Debian/Mint.

Or run makewhatis on CentOS/Redhat.

Did you find this article valuable?

Support Megha Sharma's Blog by becoming a sponsor. Any amount is appreciated!

ย