The Big Picture and Basic Commands
The Big Picture
Question
Well, you are using Linux for N+ years. But you still don't know How Linux works?
The most effective way to understand how an operating system works is through an abstraction. We can divide Linux Operating System into 3 layered abstractions:
- User Processes
- Kernel
- Hardware
Main Memory
Main memory is just a huge storage for numerous 0s and 1s, called bits. This is where all programs, including the kernel itself, are stored. A CPU is just an operator on memory - it reads instructions from memory, performs computations (using its registers) and writes the result back to the memory.
State. A state is a particular arrangement of 0s and 1s at specific time. This means, 0110, 0010 and 0001 are different states even if they are in the same time frames. A process is also a state consisting millions of bits. So, it is very hard to memorize what state it is and was, so we again make it abstract:
Kernel
The kernel has a number of definitions. Here's 2 of them:
- Kernel is an interface between hardware and processes.
- Kernel is a software that tells CPU where to look for the next task.
Kernel has a number of responsibilities:
- Process scheduling: Which process is allowed to use CPU at any given time.
- Memory Management: which memory is of which process, which memory is shared, which are being allocated and freed, etc.
- Device drivers: Managing specific hardware interfaces.
- System call support: User processes can call kernel functions using syscalls.
User Space
User space is a memory area allocated by kernel for user processes. Recall that a process is simply a state of memory.
Linux supports User management. But why it needs a user? Users exists primarily to support permissions and boundaries. Every user-space process has owner, and any user can only access and modify the process owned by them. Root user has unrestricted access however. Root user can easily modify any process, no matter who owns that. System designers always try root access as less required as possible. For instance, what if simply turning on Wi-Fi would require root access (as it is a hardware feature). That would be a security hole!
Basic Commands and Directory Structure
Standard Input and Standard Output
Unix processes use I/O streams to read and write data. Streams are very flexible: a source of an input stream can be a file, a device, a terminal window or even the output stream of the another process. To observe this, simply type cat and press ENTER. The program starts to wait for your input from input stream to do its work, as you didn't specify from which file it should read. So, in this case Terminal's output stream is being cat's input stream.
Basic Commands
cat /etc/passwdcat file1.conf file2.txtls -lcp file1.txt file2.txt- file1 is copied into file2cp file dir- file is copied into dircp file1 file2 file3 dir- multiple files are copied into dirmv file1 file2- renames file1 as file2touch file- creates a filels -l file- shows file information with permissionsrm file- removes the filewc file- counts lines, words, and bytes (-l -m -cflags respectively)
Navigating Directories
cd dir- changes current directory to dir (opens dir)mkdir dir- makes a new dirrmdir dir- removes the dir
Shell Globbing (Wildcards)
Shell can match simple patterns to file and directory names.
echo *- any number of arbitrary characters (lists files in a current directory)("msg*", "*msg", "*msg*")-[0]expands to all filenames that start with msg,[1]expands to all filenames end with msg,[3]expands to all filenames contain msgbr?t-?glob expands to exactly 1 character - matchingbratandbrotin this example
Intermediate Commands
grep root /etc/passwd- prints the lines from a file or input stream that match an expression. Here, it prints lines in/etc/passwdfile that contain text rootgrep -i- matches case-insensitivegrep -v- inverts the search (lists not matching)- grep understands regex:
grep root /etc/.*- matches any number of chars, including none.grep root /etc/.+- matches any one or more chars.
grep root /etc/.- matches exactly one arbitrary char.
less file- opens file with scroll option. Handy if a file or output stream is too big. For example,grep ie /usr/share/dict/words | lessis giving all of the words containingieas input stream to less, and it shows the result with scrolling option.pwd- print working directorydiff file1 file2- see differences between 2 text filesfile file- let system guess the type of filefind dir -name file -print- finds a file in directory dir and prints the locationhead file.txt- shows first 10 lines of the file. Use-noption to specify sizetail file.txt- shows last 10 lines of the file. Use-noption to specify sizesort file- sorts the file in alphanumeric order.-rfor reverse,-nfor sorting by numerical order only.
User Management
In Unix-based systems you can create a new user as follows:
sudo useradd <user> # adds user without /home/<user> directory
sudo useradd -m <user> # adds user with /home/<user> directory
To add a new group to the system:
To add a user to the group: To login as a new user:Environment and Shell Variables
The shell can store temporary variables, called shell variables. For example:
An environment variable is a bit different. All processes on Unix systems have envvar storage. The primary difference is, evironment variables are accessible from any program ran by that shell (process), whereas shell variables can't be used with processes ran by that shell. To assign an envvar:
The Command Path
PATH is a special envvar that contains a list of system dirs that shell searches when we type. For example, if we write ls on terminal, it searches the following dirs by default to find ls program:
ubuntu@ubuntu2404server:/etc$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
Warning
Even if you wipe out your PATH accidentally, this change is local. Because envvars are stored locally in each process' address space, simply exiting the shell and opening a new one solves the problem.
Input and Output
There are 3 types of streams in Unix: standard input, standard output and standard error. We can redirect the streams as we defined. For example:
The former will redirect the output ofcommand to file, strictly writing the file. The latter will also redirect, but appending to the existing contents of the file.
To send the standard output of a command to the standard input of another command, use the pipe character (|). The following example redirects the result of processes list to tail, showing only last 10 entries of the list:
Manipulating Processes
The ps command we emphasized earlier have several options to execute:
ps x # show all of your running processes
ps ax # show all processes not just yours
ps u # include more detailed information about processes
ps w # show full command names
File Modes and Permissions
One of the most suffered topics for me. I just couldn't understand octal, hex notations of the permissions, based on user, group and other. Now, its time to reveal whats inside. Let's run ls -l:
ls -l
# drwxr-xr-x. 1 voidp voidp 74 May 6 01:34 samandar
# -rw-r--r--. 1 voidp voidp 201 Apr 22 19:11 exam.txt
[file_type][user_permissions x3][group_permissions x3][other_permissions x3] for example -rwxrw-r-- means this is a regular file, the user can read, write, and execute, the group can read and execute, and other can read.
drwxr-xr-x means this is a directory, the user can read, write, and execute, the group can read and execute, and other can read.
Modifying Permissions.
To change permissions on a file or directory, use the chmod command. We can change permissions for user, group and other:
chmod u+w file # add write permission for user
chmod g+x file # add execute permission for group
chmod o+r file # add read permission for other
We can also change file permissions using numbers, in octal form. This might seem strange, and no need to remember every single one, but here are some examples:
chmod 744 file # rwxr-xr-x
chmod 777 file # rwxrwxrwx
chmod 644 file # rw-r--r--
chmod 555 file # rwxr-xr-x
chmod 755 file # rwxr-xr-x
chmod 700 file # rwx------
chmod 600 file # rw-------
chmod 400 file # r--------
chmod 200 file # -w-------
chmod 100 file # --x------
chmod 000 file # ---------
Symbolic Links.
A symbolic link is a file that points to another file or a directory, effectively creating an alias:
⚠️ I need further explanation of this, couldn't get so much.
Archiving and Compressing Files
When it comes to compressing files in Unix, GNU Zip or gzip comes to mind firstly:
gzip does not pack multiple files into single compress, namely does not archive them. To do so, we use tar:
tar cvf archive.tar file1 file2 file3
# c - create mode, v - verbose diagnostic output, f - file option (.tar file name)
tar xvf archive.tar
# x - extract, v - verbose diagnostic output
Before unpacking, it’s usually a good idea to check the contents of a
.tar file with the table-of-contents mode by using the t flag instead of the x flag. This mode verifies the archive’s basic integrity and prints the names of all files inside. If you don’t test an archive before unpacking it, you can end up dumping a huge mess of files into the current directory, which can be really difficult to clean up.
Compressed Archives.Compressed archives are just archives with a
.gz extension:
tar zcvf archive.tar.gz file1 file2 file3
# z - gzip compression, c - create mode, v - verbose diagnostic output, f - file option (.tar file name)
tar zxvf archive.tar.gz
# z - gzip compression, x - extract, v - verbose diagnostic output
# or 2 separate commands:
gunzip archive.tar.gz
tar xvf archive.tar
Exercises
- Map Your Filesystem: Use
tree,ls,cd, andpwdto draw (on paper or a text file) a hierarchy map of your home directory and/etc. - Breadcrumb Trail: Navigate deep into
/usr, then usepwdto echo your current path. Exit back step-by-step usingcd ..only. - Hidden Treasures: List all hidden files in your home directory. What do
.bashrcand.profiledo?
Answer:.bashrcis a configuration applied when we open a new bash shell..profileis a configuration applied while logging into the system, either using SSH or other. It is a global configuration for all later opened shells. - Find Me If You Can: Use
findto search for all.conffiles under/etc(e.g.,find /etc -name "*.conf"). Count them. - Directory Lab: Create a directory structure:
projects/linux/basics/{scripts,notes,logs}using one command. - Log the Logs: Use
touchto create 5 fake log files namedlog1.log,log2.log... etc., in thelogsfolder. - Bulk Renamer: Rename all
.logfiles to.txtusing aforloop. - Copycat: Copy
/etc/hostsand/etc/resolv.confinto your notes folder. Don’t overwrite existing ones. - Clean Sweep: Delete all
.txtfiles in logs, but only after confirming their count. - Who’s Talking? Use
echoto write a sentence to a file calledhello.txt, then view it withcat,less, andhead. - Copy & Compare: Duplicate a file and use
diffto show they’re the same. Modify the copy slightly and compare again. - Combiner: Create 3 small
.txtfiles with different content. Usecatto combine them into a new file. - Line Count Challenge: Create a file with 20 lines. Use
wc,head, andtailto count and extract specific lines. - Output Redirect Mastery: Use
>to write and>>to append to a file. Create a log tracking what you learn each day. - Man Page Explorer: Read the
manpage forls,grep,find, andbash. Summarize one cool option for each. - Reverse Search Detective: Use
reverse-i-search (Ctrl+R)to find a command you ran earlier. Re-run it. - Your Command Diary: Use
historyto see your command list. Filter it usinggrepfor commands likecd,mkdir, etc. - Get Me Help Fast: Use
--helpandmanto learn the difference betweenls --helpandman ls. Answer: I concluded that--helpis for usage details, command options.manis for full documentation. - Create a new user and group:
- Create a new user named
linuxuserwith the commandsudo useradd linuxuser - Create a new group named
linuxgroupwith the commandsudo groupadd linuxgroup - Add the
linuxuserto thelinuxgroupwith the commandsudo usermod -aG linuxgroup linuxuser
- Create a new user named
- Create files and directories as the new user:
- Switch to the
linuxuseraccount with the commandsu - linuxuser - Create a new directory named
mydirin the/home/linuxuserdirectory - Create a new file named
myfile.txtin themydirdirectory - Create a new file named
anotherfile.txtin the/home/linuxuserdirectory
- Switch to the
- Change ownership and permissions
- Switch back to your original user account with the command
exit - Change the ownership of the
mydirdirectory to the root user with the commandsudo chown root:root /home/linuxuser/mydir - Change the permissions of the
myfile.txtfile to read-only for the owner with the commandsudo chmod 400 /home/linuxuser/mydir/myfile.txt - Try to edit the
myfile.txtfile as thelinuxuseraccount with the commandsu - linuxuser; nano mydir/myfile.txt(you should get a permission denied error)
- Switch back to your original user account with the command
- Change group ownership and permissions
- Change the group ownership of the
anotherfile.txtfile to thelinuxgroupgroup with the commandsudo chgrp linuxgroup /home/linuxuser/anotherfile.txt - Change the permissions of the
anotherfile.txtfile to read-write for the group with the commandsudo chmod 660 /home/linuxuser/anotherfile.txt - Try to edit the
anotherfile.txtfile as a member of thelinuxgroupgroup with the commandsu - linuxuser; nano anotherfile.txt(you should be able to edit the file)
- Change the group ownership of the
- Use sudo to change ownership and permissions
- Use
sudoto change the ownership of themydirdirectory back to thelinuxuseraccount with the commandsudo chown linuxuser:linuxuser /home/linuxuser/mydir - Use
sudoto change the permissions of themyfile.txtfile to read-write for the owner with the commandsudo chmod 600 /home/linuxuser/mydir/myfile.txt - Try to edit the
myfile.txtfile as thelinuxuseraccount with the commandsu - linuxuser; nano mydir/myfile.txt(you should be able to edit the file)
- Use