This repository contains my personal notes on Unix and Linux command line essentials. It serves as a reference guide and tutorial to help me navigate and work effectively in a Unix-based environment.
- Introduction to Unix and Linux π§
- Understanding the Shell π
- Navigating the File System π§
- check the current directory with "pwd" π
- change directories with "cd" πΆββοΈ
- Listing Files with "ls" π
- Creating and Manipulating Files with "touch" βοΈ
- File Type Identification using "file" π
- Working with Wildcards π
- Creating Directories with "mkdir" πποΈ
- Copying Files and Directories with "cp" ππ
- Moving and Renaming with "mv" ππ
- Removing Files and Directories with "rm" and "rmdir" ποΈπ
Each topic is explained in a beginner-friendly manner, with clear examples and explanations. These notes serve as a handy reference for me to quickly recall and reinforce my understanding of these essential Unix and Linux commands.
Feel free to explore the repository, refer to the notes, and use them as your own reference. Keep in mind that these notes are personalized and tailored to my learning journey, but I hope they prove helpful to others as well.
Please note that these notes are constantly evolving as I continue to learn and discover more about Unix and Linux. Contributions and suggestions are always welcome!
Unix and Linux are operating systems that provide the foundation for running programs on computers. Unix, developed in the 1970s, introduced important concepts and features that are still used today. Linux, created in the 1990s, is a Unix-like open-source operating system known for its stability, security, and versatility. Both Unix and Linux are widely used in various domains, from businesses to government institutions.
βΉοΈ Click for a detailed explanation
Unix is an operating system that was developed in the 1970s. It was designed to be powerful, reliable, and secure. Unix introduced many important concepts and features that are still used in modern operating systems today. It became popular in universities, businesses, and government institutions. Unix uses a hierarchical file system and provides a command-line interface for interacting with the system. It has a rich history and has influenced the development of numerous operating systems.
Linux is an operating system that is based on the Unix design principles. It was created in the 1990s by Linus Torvalds, and it quickly gained popularity. Linux is often referred to as a "Unix-like" operating system because it shares many similarities with Unix in terms of its design philosophy and command-line interface. One key difference is that Linux is open-source, which means its source code is freely available for anyone to view, modify, and distribute. This openness has led to a large community of developers and contributors who collaborate to improve and expand Linux. Linux is known for its stability, security, and versatility. It is used in a wide range of devices, from servers and mainframes to smartphones and embedded systems. Popular Linux distributions (or versions) include Ubuntu, Fedora, CentOS, Debian, and many others. Each distribution offers its own set of features and software packages.
In summary, Unix and Linux are operating systems that provide the foundation for running programs on computers. Unix is an older operating system with a rich history, while Linux is a newer, open-source operating system that is based on Unix principles. Linux has gained popularity for its stability, security, and flexibility, and it has a large and active community of developers.
File system organization refers to the way files and directories (folders) are structured and arranged within a computer's storage system. It determines how files are stored, accessed, and managed.
βΉοΈ Click for a detailed explanation
File system organization provides a way to organize and manage files, allowing for efficient storage, retrieval, and manipulation of data. To understand file system organization, let's compare it to organizing physical items, such as books in a library or documents in a filing cabinet.
In a Unix-based file system, such as Linux, the file system organization can be compared to organizing books in a hierarchical structure. The root of the file system represents the top-level directory ("/"), which is similar to the library itself. Different sections and shelves within the library correspond to directories and subdirectories. Each book represents a file. For example, the directory "/usr/bin" represents the "bin" directory within the "usr" directory.
In a Windows file system, the organization can be likened to organizing books within multiple filing cabinets. Each filing cabinet represents a drive (e.g., C:, D:, etc.), and each cabinet drawer corresponds to a directory. The organization within the drawer is similar to that in the Unix-based example.
Regardless of the specific file system, the organization helps users locate and manage files efficiently, just as library systems aid in finding specific books in real life.
The shell is a program that provides a way to interact with the Linux operating system. It acts as a command-line interface where users can give instructions to the computer by typing commands. The shell allows users to navigate the file system, run programs, manipulate files, and perform various tasks on a Linux system.
βΉοΈ Click for a detailed explanation
Think of the shell as a translator between you and the computer. You tell the shell what you want to do, and it communicates with the operating system to get the job done. You can use the shell to navigate the file system, run programs, manipulate files, and perform various tasks on a Linux system.
π The shell makes it possible to control the computer using text commands rather than relying on graphical interfaces with buttons and menus. It's a powerful tool for advanced users and programmers to work efficiently and automate tasks. The shell allows you to execute commands, access system utilities, and write scripts to automate complex operations.
Understanding how to use the shell is fundamental to working with Unix-based systems like Linux. It provides a flexible and powerful interface to interact with the operating system and perform a wide range of tasks.
Imagine a Unix system as a big city with numerous streets and buildings. To move around in the Unix system, you need to know your current location and where you want to go.
Navigating in a Unix system involves understanding the file system structure and using commands like "pwd" to check the current directory, "cd" to change directories, and "ls" to list the contents of a directory. Absolute and relative pathnames are used to specify the location of files and directories.
pwd stands for "print working directory", It's like asking the computer, "Where am I right now?" When you type pwd and press Enter,and It will shows the current directory(the folder) you are in.
The cd
command stands for "change directory."
The cd
command allows you to change your current working directory as if you are entering a different room or opening a different folder to explore its contents.
βΉοΈ Click for a detailed explanation
Imagine you are exploring a maze filled with different rooms. Each room represents a directory or folder on your computer. To navigate through the maze and move to a different room, you use the cd
command.
The cd
command allows you to change your current working directory, which is like your current position in the maze. You can use the cd
command followed by the name or path of the directory you want to go to.
There are two types of pathnames you can use: absolute pathnames and relative pathnames.
-
Absolute Pathnames: An absolute pathname starts from the root directory, represented by a forward slash ("/"), and follows the branches of the directory tree until you reach the desired directory. It provides the complete path from the root to the destination directory. For example, if you want to go to a directory called "bin" located within the "usr" directory, you would use the absolute pathname "/usr/bin".
-
Relative Pathnames: A relative pathname starts from your current working directory and navigates through the directory structure. It uses special notations to represent positions within the file system tree. The dot (.) notation refers to the current working directory itself, and the dot dot (..) notation refers to the parent directory of the current working directory. For instance, if you are in the "/usr/bin" directory and want to go to its parent directory, which is "/usr", you can use the relative pathname "..".
You can also combine the dot notation with the slash (/) to specify a subdirectory within the current working directory. For example, if you are in the "/usr" directory and want to go to the "bin" directory within it, you can use the relative pathname "./bin".
Additionally, there are a few shortcuts you can use with the cd
command:
- Typing
cd
alone takes you to your home directory, which is like your base starting point in the maze. - Typing
cd ~user_name
changes the working directory to the home directory of the specified user. - Typing
cd -
changes the working directory to the previous one you were in.
Remember, the cd
command helps you move between directories in the file system, just like navigating through rooms in a maze. By understanding absolute and relative pathnames, as well as using shortcuts, you can easily change directories and explore different parts of your computer's file system.
The ls
command stands for "list."
It's like looking inside a folder to see what's in it.
βΉοΈ Click for a detailed explanation
When you run ls
without any options, it lists the files and directories in your current working directory.
You can also specify a directory by adding its path after ls
to list its contents instead. For example, ls /bin
lists the files in the "/bin" directory.
Adding the "-l" option to ls
(like ls -l
) displays the files in a long format, providing more information about each file.
The long format includes details such as file permissions, owner, group, file size, and modification time.
File permissions are represented by a series of characters. The first character indicates the file type: "-" for a regular file and "d" for a directory.
The following three characters represent the read, write, and execute permissions for the file's owner, the next three for the group, and the final three for others.
The file name is displayed at the end of each line in the long format.
By using ls
and its options, you can conveniently view and explore the files and directories in a directory, obtaining information about them such as permissions and sizes.
The less
command allows you to view the contents of a file one page at a time.
When you type less
followed by the name of a file and press Enter, it shows you the contents of the file that you can scroll through.
It's like reading a book or a document on your computer.
βΉοΈ Click for a detailed explanation
Text files contain human-readable information and are represented using characters and numbers.
The less
program is used to view these text files in a compact and organized manner.
To use less
, simply type less
followed by the name of the text file you want to view. For example, less my_file.txt
will display the contents of the file "my_file.txt".
Once you're viewing the file with less
, you can navigate through it using the following commands:
-
Page Up: Press the
Page Up
key or theb
key to scroll up one page. -
Page Down: Press the
Page Down
key or theSpace
key to scroll down one page. -
Scroll Up: Press the
Up
arrow key to scroll up one line at a time. -
Scroll Down: Press the
Down
arrow key to scroll down one line at a time. -
Search: Press the
/
key followed by a search term to search for a specific word or phrase in the file. Pressn
to find the next occurrence of the search term. -
Quit: Press the
q
key to exitless
and return to the command prompt.
That's it! With less
and its navigation commands, you can easily view and read the contents of text files.
The touch
command is used to create a new, empty file.
When you type touch
followed by the name of a file and press Enter, it creates a file with that name.
For example, if you want to create a file called "newFile.txt," you can type touch newFile.txt
and press Enter.
It's like creating a blank piece of paper to write or draw on.
- Note: If you use "touch" with a file that already exists, it will only modify the timestamp of that file. It does not change the content or overwrite the file.
The file
command is used to determine the type of a file. It provides information about the file's format, content, and characteristics.
βΉοΈ Click for a detailed explanation
The file
command is a helpful tool in Linux that tells you what type of file you're dealing with. It provides information about the format, content, and characteristics of a file without needing to open it. This can be particularly useful when you come across files that you're not familiar with.
To use the file
command, simply type file
followed by the name of the file you want to check. For example, if you have a file called "my_file.txt," you would type file my_file.txt
. The file
command recognizes different types of files and provides descriptions for each type.
Here are some examples of file types and their descriptions:
-
ASCII text: This means the file contains plain text that can be easily read and understood.
-
Bourne-Again shell script text: It indicates that the file is a script written in the Bash programming language.
-
ELF 64-bit LSB executable: This refers to an executable file, which means it's a program that your computer can run.
-
ELF 64-bit LSB shared object: It denotes a shared library file used by executable programs.
-
GNU tar archive: This type of file is an archive that stores a group of files together. You'll often encounter it when working with compressed files or backups.
-
gzip compressed data: It indicates that the file is compressed using the gzip utility.
-
HTML document text: It means the file contains a web page written in HTML, which is used to create websites.
-
JPEG image data: This tells you that the file is an image in the JPEG format.
-
PostScript document text: It refers to a file containing commands in the PostScript language, often used for printing or graphical tasks.
-
Zip archive data: This denotes a compressed archive file created with the zip utility.
By using the file
command, you can quickly find out what kind of file you have and get a better understanding of its contents. It's especially helpful when you come across files that you're not familiar with, allowing you to determine how to work with them or which programs to use.
Wildcards are special characters in the shell that allow you to select groups of filenames based on patterns. They enhance the power and efficiency of commands by enabling you to work with multiple files at once.
βΉοΈ Click for a detailed explanation
Here are some commonly used wildcards:
-
Asterisk (*) Wildcard: It matches any characters (including none or multiple characters). For example, "*" selects all filenames in a directory.
-
Question Mark (?) Wildcard: It matches any single character. For example, "?" selects filenames with just one character.
-
Bracket ([]) Wildcard: It matches any character that is listed inside the brackets. For example, "[abc]" selects filenames starting with "a", "b", or "c".
-
POSIX Character Classes: These are special notations to represent sets of characters within brackets. For example,
[:digit:]
matches any numeral (0-9), and[:lower:]
matches lowercase letters. -
Exclamation Mark (!) in Brackets: It matches any character that is not listed inside the brackets. For example, "[![:lower:]]" selects filenames that don't end with a lowercase letter.
By using these wildcards, you can create simple patterns to select specific filenames or groups of filenames that match your criteria. Wildcards can be used with any command that works with filenames, making it easy to perform actions on multiple files at once.
For example, if you want to delete all files starting with "report" and ending with ".txt", you can use the wildcard pattern "report*.txt" to select them all.
Wildcards are a beginner-friendly way to work with files in the shell, helping you quickly manipulate or find specific files without manually typing each filename. They are a handy tool to have in your command-line arsenal!
The mkdir
command stands for "make directory." It's like making a new folder to store things.
For example, if you want to create a directory called "newFolder," you can type mkdir newFolder
and press Enter.
βΉοΈ Click for a detailed explanation
The mkdir
command is used to create directories (folders) in Linux. It allows you to create new directories in the file system. Here's how you can use it:
To create a directory, simply type mkdir
followed by the name of the directory you want to create. For example, mkdir directory_name
will create a new directory with the specified name in your current location.
You can also create multiple directories at once by listing their names separated by spaces. For example, mkdir directory1 directory2 directory3
will create three directories with the specified names.
The mkdir
command is a simple yet useful tool for creating directories. It allows you to organize your files and directories efficiently by creating new directories as needed.
To copy a file or directory, you can use the cp
command. It's like making a duplicate of something.
For example, if you want to copy a file called "myFile.txt" to a directory called "Backup," you can type cp myFile.txt Backup
and press Enter.
βΉοΈ Click for a detailed explanation
The cp
command is used to copy files and directories in Linux. It allows you to duplicate files or transfer them to different locations. Here's how you can use it:
-
Copying Files: To copy one or more files, use the
cp
command followed by the filenames you want to copy and the destination directory. For example,cp file1.txt file2.txt destination_directory
will make copies of "file1.txt" and "file2.txt" in the specified destination directory. -
Copying Directories: To copy directories and their contents, use the
-r
option (recursive) with thecp
command. This option ensures that all files and subdirectories within the directory are copied as well. For example,cp -r source_directory destination_directory
will copy the entire "source_directory" and its contents to the specified "destination_directory".
The cp
command provides flexibility for copying files and directories. It allows you to create backups, duplicate files for different purposes, or transfer files between different locations. Whether you're working with individual files or entire directory structures, the cp
command is a powerful tool for managing file operations.
The mv
command stands for "move." It's like picking up something and placing it somewhere else.
For example, if you want to move a file called "myFile.txt" to a directory called "Documents," you can type mv myFile.txt Documents
and press Enter.
βΉοΈ Click for a detailed explanation
The mv
command is used to move or rename files and directories in Linux. It allows you to change the location or name of a file or directory. Here's how you can use it:
-
Moving Files: To move a file to a different directory, use the
mv
command followed by the name of the file and the destination directory. For example,mv file.txt destination_directory
will move the file "file.txt" to the specified destination directory. The file is essentially cut from its current location and pasted into the new location. -
Renaming Files: To rename a file, use the
mv
command followed by the current name of the file and the desired new name. For example,mv old_name.txt new_name.txt
will rename the file from "old_name.txt" to "new_name.txt" in the same directory. -
Moving and Renaming Directories: The
mv
command can also be used to move directories or rename them. The syntax is similar to moving or renaming files.
The mv
command is a versatile tool for organizing files and directories. Whether you need to change their location, rename them, or both, the mv
command provides a straightforward and efficient way to perform these operations.
The rm
command stands for "remove.", It's like throwing something away in the trash, so be careful with this command. When you type rm
followed by the name of a file or directory and press Enter, it permanently deletes the file or directory.
For example, if you want to remove a file called "oldFile.txt," you can type rm oldFile.txt
and press Enter.
βΉοΈ Click for a detailed explanation
The rm
command is used to remove (delete) files and directories in Linux. It permanently deletes them, so caution should be exercised when using this command. Here's how you can use it:
-
Deleting Files: To delete one or more files, use the
rm
command followed by the filenames you want to remove. For example,rm file1.txt file2.txt
will delete both "file1.txt" and "file2.txt". You can specify multiple files separated by spaces to delete them all in one command. -
Deleting Directories: To delete directories and their contents, use the
-r
option (recursive) with therm
command. This option allows you to delete directories and all the files and subdirectories inside them. For example,rm -r directory
will delete the directory and everything inside it.
The rm
command can be a powerful tool for cleaning up files and directories. However, it is irreversible, so it's important to exercise caution and double-check the files and directories you are deleting to avoid accidental data loss. If you're unsure about the effect of your command, you can use the ls
command to preview the files and directories that will be affected before proceeding with the deletion.
Please be mindful when using the rm
command, as it permanently removes files and directories. Double-check your command and make sure you are deleting the intended files or directories.
The "rmdir" command is used to remove empty directories in Linux. It stands for "remove directory". It is specifically designed to remove directories that do not contain any files or subdirectories. Here's how you can use it:
To remove an empty directory, type "rmdir" followed by the name of the directory you want to remove. For example, "rmdir directory_name" will delete the specified empty directory.
Please note that the "rmdir" command will only work if the directory is empty. If the directory contains files or subdirectories, the command will fail.
I hope you find these Unix and Linux command line essentials helpful in your journey. Remember to experiment, practice, and explore more to become proficient in working with Unix-based systems. Let's embrace the power and flexibility of the command line! If you have any questions or suggestions, feel free to reach out. Enjoy your Unix and Linux adventures!