File System Organization
Like Windows, the files on a Linux system are arranged in what is called a hierarchical directory structure. This means that they are organized in a tree-like pattern of directories (called folders in other systems), which may contain files and subdirectories. The first directory in the file system is called the root directory. The root directory contains files and subdirectories, which contain more files and subdirectories and so on and so on.
The basic three commands include:
pwd (print working directory)
cd (change directory)
ls (list files and directories).
pwd
The directory we are standing in is called the working directory. To see the name of the working directory, we use the pwd command.
[me@linuxbox me]$ pwd
/home/me
When we first log on to our Linux system, the working directory is set to our home directory.
cd
To change the working directory (where we are standing in the maze) we use the cd command. To do this, we type cd followed by the pathname of the desired working directory. A pathname is the route we take along the branches of the tree to get to the directory we want.
me@linuxbox me]$ cd /usr/bin
me@linuxbox bin]$ pwd
/usr/bin
If we type cd
followed by nothing, cd will change the working directory to our home directory.
me@linuxbox me]$ cd
me@linuxbox bin]$ pwd
/home/me
A related shortcut is to type cd ~user_name
. In this case, cd will change the working directory to the home directory of the specified user.
me@linuxbox me]$ cd ~me
me@linuxbox bin]$ pwd
/home/me
Typing cd -
or cd ..
changes the working directory to the previous one.
me@linuxbox me]$ cd /usr/bin
me@linuxbox bin]$ pwd
/usr/bin
me@linuxbox me]$ cd ..
me@linuxbox bin]$ pwd
/usr
ls
It is used to list the files in the current working directory.
[me@linuxbox me]$ ls
Desktop Download Pictures Music Templates Documents examples.desktop Public Videos
File names that begin with a period character are hidden. This only means that ls will not list them unless we say ls -a
.
[me@linuxbox me]$ ls -la
.git/ .ssh/ .ipython/ Desktop Download Pictures Music Templates
ls -l
List the files in the working directory in long format
[me@linuxbox me]$ ls -l
drwxr-xr-x 1 me 197121 0 Oct 17 2023 OneDrive/
drwxr-xr-x 1 me 197121 0 Jan 17 2023 Pictures/
drwxr-xr-x 1 me 197121 0 Mar 3 2023 Saved Games/
drwxr-xr-x 1 me 197121 0 Apr 27 2023 Searches/
Displaying file contents
There are several commands to display the content of a file in Linux.
- Using
cat
command
$ cat filename
- Using
head
andtail
commands
The head
command displays the first 10 lines of a file, while the tail
command displays the last 10 lines of a file.
$ head filename # displays the first 10 lines of a file
$ tail filename # displays the last 10 lines of a file
You can modify the number of lines displayed by using the -n
option, for example:
$ head -n 5 filename # displays the first 5 lines of a file
$ tail -n 5 filename # displays the last 5 lines of a file
- Using
less
command
The less
command allows you to view a file one page at a time. It allows you navigate through the file using the arrow keys or page up/down keys.
$ less filename
- Using
awk
command
This command uses awk
to print each line of the file.
$ awk '1' filename
Creating files and directories
Create a file:
- Using the
touch
command:
$ touch filename
This will create a new empty file with the specified name.
- Using a text editor:
$ nano filename # using the nano editor.
$ vi filename # using vim editor.
$ code filename # using vscode editor.
This will open a text editor where you can create and edit the file. Once you’re done, save and exit the editor.
- Using the
echo
command:
$ echo "Hello World!" > filename
This will create a new file with the specified name and add the text “Hello World!” to it.
Create a directory:
- Using the
mkdir
command:
$ mkdir directoryname
This will create a new directory with the specified name.
Removing a file or directory
Removing a file:
To remove a file, use the rm
command followed by the name of the file you want to remove:
$ rm filename
If the file is write-protected, rm
will ask you to confirm the deletion. To remove the file without prompting, use the -f
option:
$ rm -f filename
Removing a directory:
To remove an empty directory, use the rmdir
command followed by the name of the directory:
$ rmdir directoryname
If the directory is not empty, you will get an error message. To remove a non-empty directory and all its contents, use the rm
command with the -r
option:
$ rm -r directoryname
Moving or Copying a file or directory
Moving a file or directory:
To move a file or directory, use the mv
command followed by the source file or directory and the destination:
$ mv source destination
Renaming a file or directory:
To rename a file or directory, use the mv
command with the source file or directory and the new name:
$ mv oldname newname
Copying a file or directory:
To copy a file or directory, use the cp command followed by the source file or directory and the destination:
$ cp source destination
To copy a directory and all its contents, use the -r
option with the cp command:
$ cp -r source destination
This will copy the entire directory source and all its contents to destination.
Using rsync
command:
The rsync
command is a powerful tool for copying and synchronizing files and directories. It can be used to copy files and directories while preserving permissions, timestamps, and other attributes:
$ rsync -avz sourceDir destinationDir
This will copy the entire directory sourceDir and all its contents to the specified destination, preserving permissions, timestamps, and other attributes.
Thanks for your time! Please leave a comment and any suggestions are welcome. Follow me to get updates.
Discover more from Coursity
Subscribe to get the latest posts sent to your email.