Getting Started with Linux#
Information |
Details |
---|---|
Lead Author |
Hantao Cui |
Learning Objectives |
• Navigate the Linux file system |
Prerequisites |
WSL installation |
Estimated Time |
90 minutes |
Topics |
Linux basics, file navigation, directories, command line |
Landing in the Linux Shell#
When you open a new Linux terminal, you will see a prompt like
User@Machine:~$
In this prompt, User
is the username, Machine
is the machine name, and ~
stands for the home directory, which is the current working directory. The colon
:
is a separator between the machine name and the working directory.
The prompt character is $
for a regular user and #
for a superuser.
Working with Files and Folders#
To navigate the file system in the shell, the three most essential commands to use are:
cd
: Change directoryls
: List files and directoriespwd
: Print working directory
Change Directory#
The cd
command is used to change the current working directory. When a single
argument is passed, namely, the path to the directory, the current working
directory is changed to the specified directory.
User@Machine:~$ cd /tmp
User@Machine:/tmp$ cd ..
User@Machine:/$ cd
User@Machine:~$
The first command
cd /tmp
specifies an absolute path /tmp
, so the working directory is changed
accordingly, as you see in the second command.
The second command cd ..
changes the directory to the parent directory. ..
is
a relative path to the parent directory. As a result, the directory changed from
/tmp
to /
. In Linux, /
is known as the root directory. Every directory has
an absolute path that starts from the root directory.
The third command cd
without any argument changes the directory to the home
directory. The home directory is the directory where the user is located. The
directory change can be confirmed by the pwd
command.
Case Sensitivity
Linux is case-sensitive, following the convention from UNIX. It is case sensitive, meaning that all the command and file names are case sensitive.
In the beginning, this might feel a bit confusing when you are used to Windows.
Understanding Paths#
There are two ways to tell the computer where to find files:
Absolute paths - Start from the root
/
cd /home/username/Documents
Relative paths - Start from current location
cd Documents # Go to Documents from current folder cd ../Downloads # Go up one, then to Downloads
Special path shortcuts:
.
- Current directory..
- Parent directory~
- Home directory
Print Working Directory#
The pwd
command prints the absolute path of the current directory.
User@Machine:~$ pwd
/home/User
It is very common to use relative paths when changing directories. For example,
User@Machine:/home$ cd User
User@Machine:~$ pwd
/home/User
confirms that changing to the folder User
relative to the current directory
/home
is successful.
Exercise
Use the cd
command to navigate to the /etc
directory.
Once in there, use the pwd
command to confirm your current directory.
List Files and Directories#
The ls
command is used to list the files and directories in the current
directory.
ls
can accept a path to a directory as an argument. It will then list the
items in the specified directory.
ls
can take arguments. A few commonly used arguments are
-a
: Show all files and directories, including hidden ones-l
: Show detailed information about the files and directories-t
: Sort the files and directories by time-r
: Reverse the order of the files and directories
These options can be combined. For example, ls -lrt
is equivalent to
ls -l -r -t
, which will list the files and directories in the current
directory in reverse order of time. The combination is agnostic to the option
order.
To list files that match a pattern, one can use the wildcard *
. For example,
User@Machine:~$ ls f*.txt
file.txt
This command lists all files that start with f
and end with .txt
.
Hidden files and directories
Hidden files and directories are named to start with a dot .
.
They are not displayed by ls
but are shown by ls -a
.
Exercise
Use the ls
command to list files and directories in the /var
directory. Include hidden files in your listing. Then, sort the files by modification time.
Basic File Operations#
Make a Folder#
The mkdir
command is used to make a folder. It can take a path to the
folder as an argument.
Remove#
Two commands are used to remove files and directories. rm
is used to remove
files or directories.
rmdir
is used to remove empty directories.
Create a directory and a file in it.
User@Machine:~$ mkdir test
User@Machine:~$ ls
test
User@Machine:~$ touch test/file.txt
User@Machine:~$ ls test
file.txt
The touch
command creates a new & empty file.
Since the directory test
is not empty, rmdir
will not work.
User@Machine:~$ rmdir test
rmdir: test: Directory not empty
One can remove the directory test
with rm -r test
. The option -r
stands
for recursive, meaning that the folder and everything (files and subfolders)
inside will be removed.
User@Machine:~$ rm -r test
User@Machine:~$ ls
No Recycle Bin on Linux
Linux bash does not have a built-in recycle bin.
Files deleted with rm
are gone forever.
If unsure, use mv
to rename the file or move it to a temporary location.
Exercise
In your home directory, create a new directory named power_logs
.
Inside power_logs
, create an empty file named session1.log
.
Verify that the directory and file were created correctly.
Copy and Move#
The commands for copy and move are cp
and mv
.
The format for calling both commands is command SOURCE DESTINATION
.
The example below makes a copy of file.txt
to file2.txt
.
User@Machine:~$ cp file.txt file2.txt
User@Machine:~$ ls
file.txt file2.txt
The example below moves file2.txt
to the directory test
.
User@Machine:~$ mv file2.txt test
User@Machine:~$ ls test
file2.txt
User@Machine:~$ ls
file.txt
Exercise
Back to the power_logs
folder, copy session1.log
to a new file named session1_backup.log
in the same directory.
Move session1_backup.log
to a new directory under power_logs
named backup
.
File Types and Extensions#
Unlike Windows, Linux doesn’t strictly require file extensions to determine file types. However, extensions are commonly used for clarity:
.txt
for text files.sh
for shell scripts.py
for Python scripts.log
for log files.tar.gz
or.zip
for compressed archives
You can use the file
command to determine a file’s type regardless of its extension:
file document.txt
Linux File System#
The Linux file system has a hierarchical structure. Unlike Windows, which uses
letters (C:, D:, etc.) to represent different drives, Linux uses a single
directory /
as the root of the entire file system. All files and directories
are logically stored in the /
directory.
Structure and Common Directories#
The File System Hierarchy Standard (FHS) defines the directory structure and directory contents in Unix-like operating systems. The FHS is maintained by the Linux Foundation.
The following are some common directories in the Linux file system:
/home
: This directory contains the home directories of all users. That includes your home directory./root
: This is the home directory of the root user. It is only accessible by the superuser./bin
: This directory contains essential system binaries. Common commands likels
,cp
, andmv
are stored here./sbin
: This directory contains essential system binaries that are used by the superuser./usr
: This directory contains user binaries, libraries, and documentation. This is not to be confused with the/home
directory./etc
: This directory contains system-wide configuration files./mnt
: Mount point for mounting a filesystem temporarily.
Understanding Permissions#
In Linux, every file and directory has an owner and associated permissions that control who can read, write, or execute it. When you list files with the ls -l
command, you can see the permissions:
User@Machine:~$ ls -l file.txt
-rw-r--r-- 1 User staff 13 Jun 10 12:34 file.txt
The permission string -rw-r--r--
has 10 characters:
First character: file type (
-
for regular file,d
for directory)Next 3 characters: owner permissions (
rw-
means read and write)Next 3 characters: group permissions (
r--
means read only)Last 3 characters: others permissions (
r--
means read only)
The three basic permission types are:
r
: Read permissionw
: Write permissione
: Execute permission (for files) or Access permission (for directories)
Changing Permissions with chmod#
The chmod
command changes the permissions of a file or directory. There are two ways to specify permissions:
Symbolic mode (using letters):
User@Machine:~$ chmod u+x file.sh # Add execute permission for the user
User@Machine:~$ chmod g+w file.txt # Add write permission for the group
User@Machine:~$ chmod o-r file.txt # Remove read permission for others
Symbols used:
u
: User (owner)g
: Groupo
: Othersa
: All (equivalent to ugo)+
: Add permission-
: Remove permission=
: Set permission exactly
Numeric mode (using octal numbers):
# Set rwx for owner, rx for group and others
User@Machine:~$ chmod 755 script.sh
# Set rw for owner, r for group and others
User@Machine:~$ chmod 644 file.txt
Common numeric permissions:
755
(rwxr-xr-x): Standard for executable scripts644
(rw-r–r–): Standard for regular files700
(rwx——): Private file, accessible only by owner
Directory Permissions
For directories, the permissions have slightly different meanings:
r
: List directory contentsw
: Create or delete files within the directoryx
: Access the directory (cd into it)
A directory needs execute (x
) permission to be accessed!
Changing Ownership#
There may be times you run into permission issues. This often happens when you
use the root
user (superuser) to run a command. For example, you probably have
used sudo
to install a package.
If you accidentally run a command as the superuser, all the files and directories created by the command will be owned by the superuser. A regular user will then not be able to modify or remove the files.
The chown
command is used to change the ownership of a file or directory. The
format is chown OWNER:GROUP FILES
. For example,
User@Machine:~$ chown User:staff file.txt
changes the ownership of file.txt
to the User
user and the staff
group.
If you need to change the ownership from root
to yourself, you will need to
use sudo
, because only the superuser can relinquish its ownership.
User@Machine:~$ sudo chown User:staff file.txt
There are options like -R
to apply the change recursively.
Caution
Caution with sudo
Always be careful when using sudo
. It can easily mess up the permissions.
Exercise
Create a shell script called
hello.sh
in your home directory with the command:echo '#!/bin/bash\necho "Hello, World!"' > hello.sh
Try to execute it with
./hello.sh
. What happens?Use
chmod
to make it executable and try again.