4
Working with Files and Folders
4.1 Introduction
If
you’ve
ever
worked
with
computers
for
any
amount
of
time,
you
would
probably
be
familiar
with
the
concept
of
files
and
folders.
They can be thought as the basic unit of data storage we work with a
GUI
. That’s still pretty much the same in the
CLI
as well.
There are two file
and stat
. Both these commands can look at a file and
learn some things about it.
-
The first one,
file
will generally be able to tell what kind of file you’re asking about.-
If a file’s name isn’t clear or if it doesn’t have an extension, sometimes it can be tricky to figure out what exactly it is.
-
Using
file
, will give you some insight into whether something is an archive or an executable file or say, a text file or other kind of document.
-
-
The second one,
stat
, on the other-hand, tells you some extended information about a file.
Information
: The file
Command
Determines the type of a file. It identifies file types by examining their content rather than their file extensions.
Information
: The stat
Command
Provide detailed statistics about files and file systems. It displays crucial information such as file size, permissions, ownership, and timestamps.
To have a quick test, lets have a file called sample.txt
with the contents of the following:
Now while we now what it contains, let’s assume we don’t. To see what kind of formatting this file contains we run the file
command:
As
we
can
see
this
command
tells
us
the
file
has
an
ASCII
formatting
which
means
it
is
generally
supported
by
almost
all
computers
without
the
need
of
additional
text
encoding.
stat
command:
As
we
can
see,
we
have
a
bit
more
information
about
the
file,
regarding
its
user,
the
date
in
which
it
was
modified,
the
size
and
more.
As
we’ll
see
when
we
look
at
the ls
command,
some
of
this
is
available
there.
These
commands
can
be
helpful
to
know
about
if
you
come
across
an
unknown
file.
In
the
graphical
environment
In
the
file
browser,
we
can
navigate
to
the Linux Tutorials
file.
From
the
Home
folder,
you
can
click
on
Desktop.
There’s
the
file.
In
this
graphical
interface,
we
can
see
pretty
easily
what
folder
you
are
working
in.
Over
here
in
the
Terminal,
we
get
a
clue
about
what
folder
we’re
working
in
on
the
prompt.
The
tilda (~
)
the
character,
right
here,
means
your
home
folder.
To
match
up
with
where
the
file
browser
is,
the
Linux
Tutorials
folder,
you’ll
need
to
navigate
into
the
Desktop
and
then
into
Linux
Tutorials.
To
do
that,
use
the cd
command
which
stands
for
change
directory
(for
more
information
try
typing man cd
in
your
terminal
window).
Start
by
typing
the
path
that
we
want
to
go
to.
Type De
and
then
press
Tab
to
auto
complete,
since
Bash
knows
what’s
available.
Right
now,
nothing
else
in
you
Home
folder
should
start
with
De
except
Desktop.
Then
press
Return
to
run
that
command.
Since
we’ve
navigated
to
a
different
folder,
the
prompt
on
your
terminal
window
should
change.
Now,
it
says
tilde
slash
Desktop (~/Desktop
),
indicating
that
the
present
working
directory
is
the
documents
residing
inside
of
the /home
folder.
You
can
also
find
that
out
by
typing pwd
no
your
terminal
window,
for
print
working
directory.
That
shows
the
full
path,
or
absolute
path
of
a
folder
where
you
are
currently
working.
An
absolute
path
starts
from
the
root
of
the
file
system,
the
highest
level
of
the
structure
where
files
are
stored.
Inside
of
the
root,
the
home
folders
for
users
are
stored
in
the /home
folder,
and
then
my
user’s
home
folder
is
represented
by
your
user
name.
Inside
that
is
documents
but
we
need
to
go
one
folder
deeper
to
get
inside
the Linux Tutorials
folder.
Write cd Linux Tutorials
and
press
Return
,
but
we
get
an
error.
You
can
see
here
that
Bash
thinks
that
we’re
trying
to
get
into
the
folder
called
just
Linux.
That’s
because cd
has
interpreted
Linux
Tutorials,
as
two
words,
two
separate
arguments,
because
there’s
a
space
in
between
the
words.
You
have
to
tell
Bash
that
the
space
is
part
of
the
name
,
not
a
separator
between
two
arguments
or
commands.
There are two ways to do this. The first way is to put the string of text inside quotes (" "), but the more common thing you’ll see is to just escape a special characters. In this case the space between Linux and Tutorials. To let Bash know that the space is part of the folder name, not a break in the command, we type a back slash (\) in front of it. Escaping a character means that it’s treated literally instead of having any other special meaning.
That
works
for
one
character
at
a
time.
If
we
had
two
spaces
in
there,
we
need
to
escape
each
space
character
individually.
So,
again
type
[ cd space Linux\ Tutorial
]
and
press
Enter
.
Now
when
we
type
[ pwd
],
we
can
see
where
we
are.
Now
that
we’re
inside
the
tutorial
files
folder,
Type ls
again
to
see
what
we’ve
got.
Let’s
take
a
look
inside
the Books
folder.
Write ls
and
this
time
I’ll
use
the -R
option
to
list
folders
recursively
and
add Books/
here
on
the
end.
Books/: Classics Fantasy Literature Music Poetry Sci-Fi Science Books/Classics: 'Charles Dickens' 'Herman Mellvile' 'Jane Austen' 'Books/Classics/Charles Dickens': 'Books/Classics/Herman Mellvile': 'Books/Classics/Jane Austen': Books/Fantasy: 'Brandon Sanderson' 'G. R. R. Martin' 'J.R.R. Tolkien' 'Robert Jordan' 'Books/Fantasy/Brandon Sanderson': 'Books/Fantasy/G. R. R. Martin': 'Books/Fantasy/J.R.R. Tolkien': Unfinished_LotR_Sequel.txt 'Books/Fantasy/Robert Jordan': Books/Literature: American English Greek Turkish Books/Literature/American: Books/Literature/English: Books/Literature/Greek: Books/Literature/Turkish: Books/Music: Books/Poetry: Books/Sci-Fi: 'Arthur C. Clarke' 'Frank Herbert' 'Isaac Asimov' 'Books/Sci-Fi/Arthur C. Clarke': 'Books/Sci-Fi/Frank Herbert': 'Books/Sci-Fi/Isaac Asimov': Books/Science: Biology Chemistry Physics Books/Science/Biology: Books/Science/Chemistry: Books/Science/Physics:
Now
we
can
see
what’s
inside
all
of
the
folders
inside /Books
.
Your
recursive
options
means
when ls
comes
across
a
folder,
it
steps
inside
and
looks
around,
listing
anything
inside
the
folder.
If
it
comes
across
another
folder
inside
that
folder,
it
does
the
same
thing,
steps
inside,
looks
around
and
reports
back.
This
is
a
helpful
way
of
exploring
a
whole
structure
of
folders.
4.2 A Detailed Look in ls Command
As the ls
command is one of the more useful commands, it is worth looking into it in a bit more
detail. As discussed, ls
is a command in the
CLI
which allows us to lists the contents of a directory.
This command is highly useful as it’s
ls
, just by itself gives a list, and depending on the environment, the items might have some colour or they might not. The colouration is
helpful but it’s not critical to use in ls
. If it is not visible, try running ls
with the dash dash colour equals always option (--color=always
).
Let’s go to the file Linux Tutorials and look around. As we have changed the path in our previous code, and
as long as we are using the same terminal window, our path should still be in Linux Tutorials
. However if it
is not the case, please Write cd Desktop/Linux\ Tutorials/
. Once we are in the directory, write the following:
Here, the -l
is used to see more information about the files within the directory. The first column on the left shows whether an item is:
-
A folder or a directory which will be shown with a
d
, -
A link which will be shown with a
l
, -
A file, which is a dash (
-
). This means that the attribute is missing or offset.
If the output shows colours, folders will generally be blue text. Links will generally be light blue text and files will generally be grey or they’ll be black or white depending on the background colour of your terminal.
The next set of columns show a representation of the permissions on the file. What different kinds of users are allowed to do with
the file. Further to the right, we see the owner of the file, and the group setting of the file. Then we see the size
of the file in bytes, which can be a little bit easier to read with a -h
option. Clear the screen and use ls -lh
.
This calls out the size post fixes. k
for kilo, m
for mega, g
for giga, and t
for terabytes. Then there’s the date and time that the
file was modified. Finally the file name, or in the case of a link, which we’ll explore a bit later. ls
with is wide variety of
options, give you a whole lot of helpful information about folders and files, so it’s a good command to know about.
4.3 Creating and Removing Folders
Sometimes, we’ll need to create folders to organise our files, and sometimes we’ll need to remove folders as well. Sometimes it is
good to know how to do it using the
CLI
. Currently this is the content of Linux Tutorials
. There is a single folder and a text file.
First, create a new folder or directory here in Linux Tutorials
folder with mkdir
which
stands for make directory, and give it a name. Let’s call it Folder
, to keep it simple.
If we observe the file content, we can see the folder has appeared. We can also see the
content of this directory with the command ls -l
. There is the Folder we have just created.
If we put a name after mkdir
, it assumes we want to create a folder inside of the current working directory. In this case, this
would be Linux Tutorials
. We can also specify a path outside the current folder or a folder deeper inside the working folder.
For example, Add a new folder inside our Books folder for Poetry. For that we have to write mkdir Books/Lyrics
.
What if we wanted to create a subfolder within a new folder? For example, let’s say we want to make a Beethoven folder inside a
Music folder. Instead of creating a Music folder and then creating a Beethoven folder inside of it, we can do it all at once using
the -p
option for mkdir
. This option creates any parent folders that are needed, so in this case, it’ll create the
Music folder for us and then create the Beethoven folder after that. Write mkdir -p Books/Music/Beethoven.
Here are the newly created files. This is a versatile command. We can also remove empty directories using the rmdir command for remove directory. Let’s go remove the Beethoven folder that we just created. For that, we have to write write rmdir Books/Music/Beethoven and now if we take a look inside the Music folder, it’s empty. One thing to keep in mind about removing folders this way is that in order to remove a folder, it has to be empty. That means it’s a little more tedious to remove a large folder structure with rmdir.
4.4 Move, Copy and Delete Files and Folders
It’s pretty common to need to move, copy, and delete files in our day to day GUI activities, and this is also true when you are working using a CLI. Some experienced command line users would even argue that it is more efficient and effective to do file management using CLI than GUI as GUI might take more time with mouse movements.
In light of this, the first command for you to learn using in CLI file management to you here is cp
which stands for copy. Remember if you don’t
remember or know how a command options work use the man
command followed by the command you want to know more about (i.e., man cp
).)
Information
: The cp
Command
A command in Unix and Unix-like operating systems for copying files and directories.
Let’s make a duplicate copy of our Poem.txt
file. As we can see, we are at the right directory to do any duplication.
Let’s
write
to
the
terminal
cp Poem.txt poem_Copy.txt
.
The
first
file
name
is
the
file
you
want
to
copy (Poem.txt
),
and
the
second
file
name
is
where
you
want
to
copy
it
to (Poem_Copy.txt
).
Press
Return
,
and
then
take
a
look
at
the
contents
of
this
folder
again
with ls -h
.
Here
we
can
see,
there’s
the
original Poem.txt
file,
and
here’s Poem_Copy.txt
.
You
can
also
copy
a
file
to
a
different
path.
For
example,
we
can
copy
our Poem.txt
file
into
our /Poetry
folder
inside
the /Books
folder.
To
do
that,
write cp Poem.txt
,
and
then
use
Tab
completion
to
get
to Books/Poetry
.
If we list the folder, we can see that the file’s been copied there.
Let’s
take
a
look
at
moving
a
file
rather
than
copying
it.
In
principle,
the
move
command
has
two
- 1.
- You can use it to move files between folders,
- 2.
- You can also use it to rename files.
The
command
for
move
is mv
,
so
using
this
command
let’s
move
the Poem_Copy.txt
to
the /Books
subfolder
inside
the /Linux Tutorial
folder.
And
we
can
check
that
the
file’s
in
that
folder.
And
then
we
can
see
that
it’s
no
longer
in
the
original
folder
by
using ls
here.
It
is
also
possible,
as
we
mentioned
before,
to
rename
files
with
the mv
command.
Information
:
The mv
command
Moves one or more files or directories from one place to another.
Let’s
try
it.
Let’s
rename
the Poem_Copy.txt
to Poem_Duplicate.txt
using
the mv
command.
4.5 Role to Users and sudo
Linux is a multi-user environment. Now, what this means is that multiple users can use an operating system. This is a concept we’re familiar with nowadays but was a new idea decades ago when Unix came on the scene when it was mostly used by specialized engineers and programmers. In principle I can have a user, someone else can have a user in the same operating system, but our files are kept separate in our individual home folders.
We
can
create
files
that
only
one
or
another
user
can
access.
At
the
command
line,
we
can
switch
between
users
with
the su
command,
which
is
variously
referred
to
as
set
user,
switch
user,
or
substitute
user.
To
use su
,
we
write
the
command
followed
by
the
name
of
the
user
we
want
to
switch
to.
Probably
the
most
common
use
of
switching
users
at
the
command
line
is
to
do
some
system
administration
tasks.
There
are
two
basic
user
roles
in
Linux.
There’s
the
normal
user
and
the
superuser
.
The
difference
here
is
one
of
privilege.
The
normal
user
can
modify,
create,
delete,
and
move
their
own
files,
but
they
can’t
make
changes
to
the
system.
They
can’t
install
software,
they
can’t
make
changes
to
system
files,
and
generally
speaking,
they
can’t
browse
other
users’
home
folders.
The
superuser,
which
is
called
root,
can
make
changes
to
the
system.
It
can
install
software,
it
can
start
and
stop
services,
and
so
on.
Normal
users
can
be
granted
the
ability
to
temporarily
use
root’s
power
through
a
command
called sudo
.
It’s
uncommon
and
it’s
really
bad
practice
to
log
into
the
root
user
directly
to
do
normal
work.
In
fact,
on
many
systems,
the
root
user
is
actually
disabled
and
can’t
be
logged
into.
You
only
want
to
borrow
root’s
power
when
you
really
need
it,
so
let’s
take
a
look
at
that.
Let’s
try
to
see
what’s
inside
root’s
home
folder,
which
is
located
at
the
root
of
the
drive.
These
are
two
different
meanings
of
the
word
root,
which
can
be
a
little
confusing.
Remember,
when
we’re
talking
about
a
file
system,
the
root
is
at
the
highest
level
of
the
organizational
structure
and
that’s
represented
by
a
single
slash.
When
we’re
talking
about
accessing
users,
root
is
the
superuser.
You
could
probably
draw
some
parallels
between
levels
in
a
hierarchy,
but
just
keep
in
mind
there’s
two
different
meanings
for
the
word
root
on
Linux.
Let’s
see
what
happens
when
I
write ls /root
,
and
I
see
I’m
denied
permission.
We
need
to
use
the sudo
command
to
gain
root’s
privileges
to
see
inside
there.
This
command
basically
tells
the
system
to
run
whatever
command
is
after
it
with
superuser
privileges
instead
of
the
normal
user’s
privileges.
So,
write sudo ls /root
.
I’m
prompted
for
my
password.
This
is
a
good
sign
that
the
computer
understood
that
I
want
superuser
privileges.
Information : Removing password from the system
While it is not recommended, Linux gives you options to do anything and this includes
removing
user password from
the computer. This would allow you to run sudo
commands without ever be. You start first use the passwd
command to
delete the password of the user (which is you)
After entering you username in place of username
, if you see
4.6 File Permissions
At a first glance, file permissions can seem rather cryptic as these were devised when every key stroke mattered. We’ve seen them before when listing files in a directory but it’s not immediately clear what they mean.
For
example, rwxr-xr-x
might
not
make
any
sense
right
now,
but
hopefully
after
this
section
you
will
have
a
working
understanding
of
the
file
permission
system
in
Linux.
The
sequence
of
letters
breaks
down
into
three
- 1.
- The First represents the user, or the owner of the file,
- 2.
- Second group of three represents the group that owns the file,
- 3.
- Third group represents all other users not in the group that owns the file.
Each of the groups of three breaks down into three individual letters, which stand for
- Read
-
someone can see the contents of a file but not modify it,
- Write
-
someone can make a change to the file, but not read the contents,
- eXecute
-
someone can run the file, for example, a program or script, without loading it into another program first.
There are a couple of other letters you may see and hear, but R, W, and X will take care of what we need to do for now.
We
can
change
the
permissions
of
a
file
using
the chmod
command
which
changes
the
file
mode
bits
on
a
file,
and
there
are
two
- 1.
-
use
octal notation , which uses three values to represent read, write, and execute. - 2.
-
use
symbolic notation , which uses a shorthand for user, group, others, and all, an operator, and a list of permissions to change.
We’ll take a look at both, starting with the octal notation.
Information
:
The chmod
Command
A shell command for changing access permissions and special mode flags of files (including special files such as directories). The name is short for change mode where mode refers to the permissions and flags collectively
Octal Notation
If
you
ever
have
worked
with
Linux
or
macOS
or
any
other
UNIX
based
OS
you
may
have
seen
commands
like chmod 777
,
or chmod 644
,
and
similar
things.
The
way
we
arrive
at
those
numbers
is
by
assigning
This
notation
makes
it
easy
to
represent
various
states
of
these
three
7
),
four
plus
two
plus
one (4+2+1
).
If
the
group
can
only
read
and
execute,
that
comes
out
to
five (5
),
four
plus
one (4+1
).
With this system and a some basic maths, it’s impossible to be ambiguous about the permissions the user, group or others have.
If you don’t feel like doing the maths, you can make up a table like you in Table 4.2 .
Octal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
Binary | 000
|
001
|
010
|
011
|
100
|
101
|
110
|
111
|
Mode | - - -
|
--x
|
-w-
|
-wx
|
r--
|
r-x
|
rw-
|
rwx
|
To view the privileges a file has one simply has to write
Symbolic Notation
The
symbolic
way
of
representing
permissions
is
a
more
approachable
method
to
a
lot
of
people,
because
instead
of
setting
numbers
for
each
value,
you
can
add
or
remove
a
permission
by
letter.
User
is
represented
by
the
letter u
,
group
by g
,
others
by o
,
and
changing
all
of
the
values
is
represented
by a
.
If
you
leave
off
a
prefix, chmod
applies
your
change
to
all
values.
There
are
three
operators
here
you
can
use:
Plus (+ )
|
adds whichever permission you specify to what’s already there |
Minus (- )
|
removes whatever is there |
Equals (= )
|
sign resets the permissions to only whatever value you specify |
For example, to set user permissions to read, write and execute, we need to use:
If
we
wanted
to
set
group (g
)
permissions
to
only
read (r
),
then
we
use:
We
can
line
up
the
octal
and
symbolic
values
and
see
what
the
results
are.
In
octal, 777
is
the
same
as
saying a+rwx
. 755
is
the
same
as
saying u+rwx
, g=rx
, o=rx
.
You
can
see
the
symbolic
notation
is
a
bit
longer,
but
it
contains
more
information
and
context,
so
it’s
a
little
easier
to
work
with.
The
nice
thing
about
symbolic
notation
is
that
it’s
a
little
easier
to
make
changes,
since
you’re
specifying
what
to
change
rather
than
what
octal
value
to
use.
Using
octal
notation
is
kind
of
like
using
the
=
operator
in
symbolic
notation
all
the
time.
Saying
whatever
was
there
before,
now
it’s
this
value,
rather
than
add
read
or
remove
execute.
4.7 Hard and Symbolic Links
It
is
time
to
look
at
a
special
kind
of
file
on
the
Linux
system
called
As you’re learning about the CLI you may not have a need to create links, but it’s important to know what they are when you come across them, and can show their usefulness as you are developing more complex applications.
To
put
it
simply,
there
are
two
- hard links
-
point to data on the disk
- Soft or symbolic links
-
point to a file on the disk.
It’s kind of a subtle difference but it changes how the resulting links work. Let’s take a look at soft links or symbolic links quickly.
4.7.1 Symbolic Links
We
can
create
a
symbolic
link
with
the ln
command
and
a -s
option:
Information
:
The ln
Command
Primarily used to create links for files in Linux, effectively allowing one file to reference another. Doing so allows you to manage files more efficiently without creating duplicates, making this command crucial for optimizing storage and managing files in Unix-like operating systems.
- 1.
-
the
name
of
the
source
file,
(i.e.,
novel.txt
) - 2.
-
the
file
we
want
to
make
a
link
to,
(i.e.,
writing.txt
) - 3.
- the name of the link I want to create.
To
create
a
link
to novel.txt
we
create
a
file
called writing.txt
and
link
it.
Now
the writing.txt
file
is
a
link
to
the novel.txt file
.
If
you
were
to
Look
at
the
contents
of writing.txt
,
you
would
see
the
contents
of
the
original
file,
and
editing
the writing.txt
file
means
editing
the
original
as
well.
Think
of writing.txt
not
as
a
file,
but
a
pointer
It’s important to know that this kind of link is relative , that is if you move the link somewhere else on the file system the system won’t be able to reference the original file any more and if you move the original file, the link will break as well, because the system will be told to look at a particular path for the linked file and it won’t be there any more.
Hard Links
You can create a hard link by leaving off the dash -s
option. If we write ln text.txt
, this will create
a hard link to text.txt
. A hard link appears to be a regular file in a file listing but it’s also just a
pointer to the original file or more specifically it’s a pointer to the data that the original file references.
One of its major advantage is hard links
Hard links and soft links both have their uses depending on the applications you have in mind.
Below is a quick guide to the options the ln command has.
Command | Description |
- - backup
|
make a backup of each existing destination file |
- b
|
like --backup
but
does
not
accept
an
argument
|
- d
|
allow the superuser to attempt to hard link directories (note: will probably fail due to system restrictions, even for the superuser) |
- f
|
remove existing destination files |
- i
|
prompt whether to remove destinations |
- L
|
dereference TARGETs
that
are
symbolic
links
|
- n
|
treat LINK_NAME
as
a
normal
file
if
it
is
a
symbolic
link
to
a
directory
|
- P
|
make hard links directly to symbolic links |
- r
|
create symbolic links relative to link location |
- s
|
make symbolic links instead of hard links |
- S
|
override the usual backup suffix |
- t
|
specify
the DIRECTORY
in
which
to
create
the
links
|
- T
|
treat LINK_NAME
as
a
normal
file
always
|
- v
|
print name of each linked file |
4.8 The Linux File System
It makes sense to explore the Linux file system from a terminal window (i.e., CLI) as it has better tools to show the map of Linux’s directory tree.
From top to bottom, the directories you are:
- /bin
-
contains binaries, that is, some of the applications and programs, such as
bash
,cat
,chmod
…. (For more information on binary files, please have a look here .) You will find thels
program mentioned above in this directory, as well as other basic tools for making and removing files and directories, moving them around, etc.. There are more bin directories in other parts of the file system tree, but we’ll be talking about those in a minute. - /boot
-
contains files required for starting the OS. Messing up one of the files here, may cause Linux to malfunction. Superuser privileges are needed to edit/change files here.
- /dev
-
contains device files. Many of these are generated at boot time or even on the fly. For example, plugging a new webcam or a USB drive into the computer, will create a new device entry in this directory.
- /etc
-
comes from the UNIX operating system, meaning “
et cetera ” (meaning "and other similar things") as it was a 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. - /lib
-
stores libraries which are files containing code that applications use. They contain code snippets applications use to control peripherals, or send files to the hard disk for example. 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, contains the all-important kernel modules. The kernel modules are drivers that make things like the video card, sound card, Wi-Fi, printer, etc. - /home
-
contains users’ personal directories.
- /media
-
where external storage will be automatically mounted when it is plugged in and being accessed. As opposed to most of the other items on this list,
/media
did not originate in 1970s, mainly because inserting and detecting storage (USB hard disks, SD cards, external SSDs, etc.) while a computer is running, is relatively new. - /mnt
-
where mount storage devices or partitions are manually mounted which is not used often nowadays.
- /opt
-
here compiled programs (i.e., non-system) are stored. Applications will end up in the
/opt/bin
directory and libraries in the/opt/lib
directory. - /proc
-
virtual like
/dev
, contains information about the computer, such as information about the CPU and the kernel Linux is running on. As with/dev
, the files and directories are generated when needed as the system is running and things change therefore don’t save your documents here. - /root
-
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 and it is not meant to be tampered.
- /run
-
System processes use it to store temporary data for their own reasons. Similar to
/root
and/boot
, it is best this folder is left alone. - /sbin
-
similar to
/bin
, but contains applications only the superuser (hence the initial s) needs. Application here can be used with thesudo
command./sbin
contains tools that can install stuff, delete stuff and format stuff. - /usr
-
Originally where users’ home directories were kept. 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 contains: 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 /bin, /sbin and /lib directories in /usr.
Information
: /usr/bin
v. /bin
Not much nowadays. Originally, the /bin
directory would contain basic commands, like ls
, mv
and rm
; the bare
minimum to run and maintain a system whereas /usr/bin
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. Many modern
Linux distributions put everything into /usr/bin
and have /bin
point to /usr/bin
just in case.
- /srv
-
contains data for servers. When running a web server, HTML files for sites would go into
/srv/http
(or/srv/www
), or running an FTP (File Transfer Protocol) server, files would go into/srv/ftp
. - /sys
-
virtual directory like
/proc
and/dev
, containing information from connected devices. - /tmp
-
contains temporary files, usually placed there by running applications. The files and directories often (not always) contain data that an application doesn’t need right now, but may need later on.
/tmp
also can store users’ temporary files as it is one of the few directories hanging off/
that can be used without superuser. - /var
-
originally named 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. Be that as it may,
/var
contains things like logs in the/var/log
sub-directories. 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 the computer from outside, the firewall will also log the attempt here.
4.9 Common Command-Line Tools and Tasks
4.9.1 The UNIX Philosophy
Starting exploring command line tools, it’s important to understand the principle behind many of the programs we’ll be looking at. That principle, often called the UNIX philosophy , originated by Ken Thompson, is a set of cultural norms and philosophical approaches to minimalist, modular software development.
Generally, these are:
-
Small is beautiful,
-
Make each program do one thing well,
-
Build a prototype as soon as possible,
-
Choose portability over efficiency,
-
Store data in flat text files,
-
Use software leverage to your advantage,
-
Use shell scripts to increase leverage and portability,
-
Avoid captive user interfaces,
-
Make every program a filter.
In a nutshell, this philosophy emphasizes tools
Of course, there are many applications that include many features and that’s fine. However, those applications are beyond the scope of this lecture. We’re talking about the standard set of command line tools that can be configured to work together in an incredible number of ways.
Jack of all trades, master of none is not encouraged in programming.
To get real work done, quality is needed; tools dedicated to a specific task working together easily. Think of an assembly line where one machine does one task and then passes the product onto the next specialized machine, rather than one complicated robot doing many different tasks on the same item.
The point here is not that we have one multifunction generalist program. We want to be able to incorporate the right tools into doing a task as flexibly as possible, and as we’ll see in a little bit, this philosophy underlies a lot of how you work at the command line.
You will use one program to read text from a file, then send it to a program that filters certain text. Then the output of that program gets processed, so that it doesn’t have duplicate lines and then the result of that will get written back to a file.
Modularity and flexibility are features, not limitations, of working at the command line. However, just because programmers strive for simplicity in their programming, it doesn’t mean they can’t have Easter eggs in them. In the terminal just type in:
APT (Advanced Packaging Tools) is used to install updates and utilities and has Super Cow Powers.
apt 2.7.14 (arm64) Usage: apt [options] command apt is a commandline package manager and provides commands for searching and managing as well as querying information about packages. It provides the same functionality as the specialized APT tools, like apt-get and apt-cache, but enables options more suitable for interactive use by default. Most used commands: list - list packages based on package names search - search in package descriptions show - show package details install - install packages reinstall - reinstall packages remove - remove packages autoremove - automatically remove all unused packages update - update list of available packages upgrade - upgrade the system by installing/upgrading packages full-upgrade - upgrade the system by removing/installing/upgrading packages edit-sources - edit the source information file satisfy - satisfy dependency strings See apt(8) for more information about the available commands. Configuration options and syntax is detailed in apt.conf(5). Information about how to configure sources can be found in sources.list(5). Package and version choices can be expressed via apt_preferences(5). Security details are available in apt-secure(8). This APT has Super Cow Powers.
4.9.2 Connecting Commands with Pipes
At the command line, we use pipes (|
) to take the output of one command and send it to another. Think of commands as little
processing nodes which do one particular thing and pipes as connections between those nodes. Searching on the internet should
give you a good idea of where to find it on your keyboard depending on the type, if you need to. We type this character in
between commands that we want to be
echo
, which prints out whatever you give it. For example, write echo "hello"
, and that works as promised.
Now, write that command again and this time add a pipe character to send the output to the command wc
for word count. And
here, instead of the output from echo
, we see the output of the wc
program responding to the input from the echo
command.
What wc
is telling here is that there is one line of text, one word, and six characters. To change the output type echo "hello, world! from the command-line interface"
and pipe that to wc
. That’s one line, six words, 45 characters.
As can be seen the sentence contain 45 characters but 46 is printed as wc
counts an invisible character at the end of the string called a new line
in addition to the characters we sent. A command can be piped to any other command, and usually it’ll do something whether it is useful or not.
4.9.3 Viewing Text Files with cat, head, tail, and less
The majority of tasks we will be working with at the command line will involve text files or text output. Therefore, it’s important
to know the following commands to check out the contents of text files. The first is called cat
, stands for
concatenate
.
Information
: The cat
Command
A standard utility which reads files sequentially, writing them to standard output.
Basically when programmers talk about concatenation, they mean sticking two or more things together, and cat
can do exactly
do just that. But more often than not, it is used to print the contents of a file to the screen. It’s also helpful to get
the contents of a text file into a series of piped commands. Depending on the operating system, there will be
different files available to you. Normally, as an administrator we tend to use cat
to look at a log file or something
similar. But here, for the sake of simplicity, we will use a simple text file inside the Linux Tutorial Folder. The Poem.txt
file. For the curious, the poem is “
4.10 Advanced Topics
4.10.1 Find Linux Distribution and Kernel Information
Up until now, almost everything we’ve done has been distribution independent.
That is, it hasn’t mattered if you’re running CentOS, Fedora, Ubuntu, or another distribution of Linux. But it’s good to know what environment you’re working with, in case you need to make some changes to the system or to install software. If you find yourself in an environment that you don’t know about, it’s pretty easy to figure out what distribution you’re using. This information is kept in files inside the /etc folder. What it’s called specifically varies by distro, but we can use a wildcard to target the names of these files and see what’s inside them. First, let’s take a look at what these files are.
Let’s write the following
In my case, I have two
Let’s see what information’s in there.
To do that, we’ll type cat /etc/*release.
DISTRIB_ID=Ubuntu DISTRIB_RELEASE=24.04 DISTRIB_CODENAME=noble DISTRIB_DESCRIPTION="Ubuntu 24.04.2 LTS" PRETTY_NAME="Ubuntu 24.04.2 LTS" NAME="Ubuntu" VERSION_ID="24.04" VERSION="24.04.2 LTS (Noble Numbat)" VERSION_CODENAME=noble ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" UBUNTU_CODENAME=noble LOGO=ubuntu-logo
Which lists the contents of all of the files in the /etc folder that end with the word release.
On different distributions, there’ll be different numbers and names of files that match this wildcard, but they’ll contain the information we need.
Here I can see that I’m using Ubuntu, version 20.04 LTS (Long Term Service), Focal Fossa.
On other systems, we’d see slightly different information here.
Another important piece of information to know about a system is what version of the kernel you’re using.
You can find that information with the uname command, Using the dash a (-a) option to show all the information.
This shows the type of system, the host name, the version of the kernel, when it was built, the architecture of the system and so on.
This kind of information can be helpful if you’re troubleshooting something.
Again, if you’ve set up a system, chances are good you know what kind of software it’s running.
But if for some reason you don’t, now we’ve seen how to figure it out.
4.10.2 Find System Hardware and Disk Information
It is important to finding out some information about the system you’re working with. If you’re using a physical computer, or a virtual machine you set up for yourself, you have some knowledge about the hardware it has, like how much RAM (Random Access Memory) it has, what kind of CPU (Central Processing Unit) it has, and how much hard drive space there is. But if you’re working on a remote system or you are an administrator of a machine you have yet to have working knowledge of, it can be helpful to get a sense of what your resources are and what hardware system has.
First, let’s find out how much RAM this machine has. To do this, use the free
command with the -h
option, which gives us values in human readable numbers.
Here, under total memory, we can see that this machine has two gigabytes of memory. Next, let’s take a look at what our processor resources
are. There is a file in the /proc directory called cpuinfo, so let’s take a look at that. To access this information write cat /proc/cpuinfo
.
processor : 0 BogoMIPS : 48.00 Features : fp asimd evtstrm aes pmull sha1 sha2 crc32 atomics fphp asimdhp cpuid asimdrdm jscvt fcma lrcpc dcpop sha3 asimddp sha512 asimdfhm dit uscat ilrcpc flagm ssbs sb paca pacg dcpodp flagm2 frint CPU implementer : 0x61 CPU architecture: 8 CPU variant : 0x0 CPU part : 0x000 CPU revision : 0
There is a lot of information here. Scroll up a little bit and I can see that I’m using an Intel Xeon processor at 3.5 gigahertz (GHz). And under CPU cores, I can see that this machine has four (4) CPU core. I can also find out how much space is taken up and how much is available on the system’s hard drive. For that, use the df command with the -h option, again, to show human readable sizes.
This shows space across a few different volumes, but the most interesting one to me is slash (/) or root, since that’s where my user data is, and it’s where you are likely to be taking up space if I installed software. The rest of these are managed by the system, so you don’t need to worry about those. You can also use the du command to see how much space files and folders take up on your system. Let’s have a look at how much space is taken up across my whole system. I’ll write sudo du / -hd1 I have to use sudo, because my user can’t see into all of the folders at the root of the drive. Then there is the du command for disk usage, and then slash (/), which is the level I want to start from, right at the root. The dash h (-h) option gives me sizes in human readable formats, kilobytes, megabytes, gigabytes, and so on, and the d option shows the du command what level of detail to show. In this case, I’m giving it the argument of one (1), meaning just show me one level d, the first level away from the root, adding everything up within each of those folders. Let’s take a look