Linux Filesystem Basics

4 minuti

File system hierarchy

hierarchy

Basic commands

  • pwd displays the directory that you are currently in
$ pwd
 
# Output
/home/carloberd
  • cd change directory.
cd /usr/share
pwd
 
# Output
/usr/share

In this case the change is made to an absolute path. In Linux, every file and directory is under the top-most directory, which is called the “root” directory, but referred to by a single leading slash “/”. An absolute path indicates the location of a directory in relation to this top-level directory. This lets us refer to directories in an unambiguous way from any place in the filesystem. Every absolute path must begin with that slash. Alternatively, the change can be made using a relative path. Relative paths refer to directories in relation to the current directory. from /usr/share you can change to locale:

cd locale

and to go back to the previous folder:

cd ..

You can always return to your home directory by running cd without specifying a directory. You can also use ~ in place of your home directory in any other commands

  • ls list all the elements inside a folder
ls
 
# Output
adduser            groff                          pam-configs
applications       grub                           perl
apport             grub-gfxpayload-lists          perl5
apps               hal                            pixmaps
apt                i18n                           pkgconfig
aptitude           icons                          polkit-1
apt-xapian-index   info                           popularity-contest
. . .

By adding the -l flag, the list is extended

ls -l
 
# Output
total 440
drwxr-xr-x   2 root root  4096 Apr 17  2022 adduser
drwxr-xr-x   2 root root  4096 Sep 24 19:11 applications
drwxr-xr-x   6 root root  4096 Oct  9 18:16 apport
drwxr-xr-x   3 root root  4096 Apr 17  2022 apps
drwxr-xr-x   2 root root  4096 Oct  9 18:15 apt
drwxr-xr-x   2 root root  4096 Apr 17  2022 aptitude
drwxr-xr-x   4 root root  4096 Apr 17  2022 apt-xapian-index
drwxr-xr-x   2 root root  4096 Apr 17  2022 awk
. . .

The first block describes the type (“d” for directory, ”-” for file) and permissions. Each subsequent column, in order, describes the number of hard links to that file elsewhere on the system, the owner, group owner, item size, last modification time, and the name of the item. By adding the -a flag, the list includes the hidden files and directories

Viewing Files

  • less allows to scroll through pages of a file, an application that will continue to run and occupy the screen until you exit.
less /etc/servicesOutput
 
# Output
# Network services, Internet style
#
# Note that it is presently the policy of IANA to assign a single well-known
# port number for both TCP and UDP; hence, officially ports have two entries
# even if the protocol doesn't support UDP operations.
#
# Updated from http://www.iana.org/assignments/port-numbers and other
# sources like http://www.freebsd.org/cgi/cvsweb.cgi/src/etc/services .
# New ports will be added on request if they have been officially assigned
# by IANA and used in the real-world or are needed by a debian package.
# If you need a huge list of used numbers please install the nmap package.
 
tcpmux          1/tcp                           # TCP port service multiplexer
echo            7/tcp
. . .

To scroll, you can use the up and down arrow keys on your keyboard. To page down, you can use either the space bar, the “Page Down” button on your keyboard, or the CTRL-f shortcut. To scroll back up, you can use either the “Page Up” button, or the CTRL-b keyboard shortcut. To search for some text in the document, you can type a forward slash “/” followed by the search term. With n you can go to the next result and N get you to the previous one.

File and Directory Manipulation

  • touch create an empty file using the name and location specified
touch file
ls
 
# Output 
file

If you use the touch command on an existing file, it updates the “last modified” time associated with that file. This can be helpful to keep in mind.

You can also create multiple files at the same time. You can use absolute paths as well.

touch /home/carloberd/file2 /home/carloberd/file3
ls
 
# Output
file  file2  file3
  • mkdir command allows you to create empty directories
mkdir test
ls 
 
# Output 
file  file2  file3  test

With the flag -p you tell the program to create any directories necessary to construct a given directory path:

mkdir -p test/new/nested/folder